Tuesday, April 7, 2015

Life Cycle of Servlet

The following figure depicts a typical servlet life-cycle scenario.
  • First the HTTP requests coming to the server are delegated to the servlet container.
  • The servlet container loads the servlet before invoking the service() method.
  • Then the servlet container handles multiple requests by spawning multiple threads, each thread executing the service() method of a single instance of the servlet.

  • The servlet is initialized by calling the init () method.
  • The servlet calls service() method to process a client's request.
  • The servlet is terminated by calling the destroy() method.
  • Finally, servlet is garbage collected by the garbage collector of the JVM

Monday, April 6, 2015

JVM, Java Processor

Every Java program is first compiled into an intermediate language called Java bytecode. 
The JVM is used primarily for 2 things:

1. The first is to translate the bytecode into the machine language for a particular computer,
2.The second thing is to actually execute the corresponding machine-language instructions as well. 

The JVM and bytecode combined give Java its status as a "portable" language – this is because Java bytecode can be transferred from one machine to another.

Machine language is OS dependent

Given the previous information, it should be easier to figure out an answer to the original question. Since the JVM must translate the bytecode into machine language, and since the machine language depends on the operating system being used, it is clear that the JVM is platform (operating system) dependent – in other words, the JVM is not platform independent.

Almost all JVMs are implemented in software. However, a JVM is anything that interprets Java bytecode in a manner that complies with the JVM specification, and there are some hardware-based JVMs as well.
Java processor is the implementation of the Java Virtual Machine (JVM) in hardware. In other words the bytecodes that make up the instruction set of the abstract machine become the instruction set of a concrete machine. These are today the most popular form of a high-level language computer architecture.

Creating a thread in Java; Two ways; Why

To define a class that is a derived from the Thread class which is built into Java. An object of this derived class will be a thread. Thread class has a method called run, which you must override in order to have your thread do whatever you want it to do.

Java does not support multiple inheritance, which means that we can not derive from multiple classes at once. So, there may be a situation in which we want to create a thread, but we also want to derive from a class that is not the Thread class. For this reason, Java provides an alternative way of creating a thread – so that you don’t have to create a class that derives from the Thread class. Java provides an interface called Runnable that our class can implement in order to become a thread. The Runnable interface has only one method heading that we must implement .

Sunday, April 5, 2015

Difference between equals() and == in Java

Before discussing the difference between “==” and the equals() method, it’s important to understand that an object has both a location in memory and a specific state depending on the values that are inside the object.

In Java, when the “==” operator is used to compare 2 objects, it checks to see if the objects refer to the same place in memory.

The equals method is defined in the Object class, from which every class is either a direct or indirect descendant.By default equals() will behave the same as the “==” operator and compare object locations. But, when overriding the equals() method, you should compare the values of the object instead.

In Java, == always just compares two references (for non-primitives, that is) - i.e. it tests whether the two operands refer to the same object.
However, the equals method can be overridden - so two distinct objects can still be equal.
For example:
String x = "hello";
String y = new String(new char[] { 'h', 'e', 'l', 'l', 'o' });

System.out.println(x == y); // false
System.out.println(x.equals(y)); // true
Additionally, it's worth being aware that any two equal string constants (primarily string literals, but also combinations of string constants via concatenation) will end up referring to the same string. For example:
String x = "hello";
String y = "he" + "llo";
System.out.println(x == y); // true!
Here x and y are references to the same string, because y is a compile-time constant equal to "hello".

What’s the point of having a private constructor in Java?

There are two possible reasons why one would want to use a private constructor – the first is that you don’t want any objects of a class to be created at all, and the second is that we want objects to be created internally – as in only created in our class.

Defining a constructor with the private modifier says that only the native class (as in the class in which the private constructor is defined) is allowed to create an instance of the class, and no other caller is permitted to do so.

To implement a singleton design pattern that allows only one instance of a class to be created, and this can be accomplished by using a private constructor.

Java always provides a default, no-argument, public constructor if no programmer-defined constructor exists. Creating a private no-argument constructor essentially prevents the usage of that default constructor, thereby preventing a caller from creating an instance of the class. Note that the private constructor may even be empty.