Saturday, 31 August 2013

String - class, related classes, operations

Q1. Tell us Hashcode function of string class?
Ans. Hash code h of a string s of length n is calculated as 
Q2. If you want to extend the “java.lang.String” class, what methods will you override in your extending class?
Ans. You would be tempted to say equals(), hashCode() and toString() but the “java.lang.String” class is declared final and therefore it cannot be extended


Q3. What is an intern() method in the String class?
Ans. A pool of Strings is maintained by the String class. When the intern() method is invoked, equals() method is invoked to determine if the String already exist in the pool. If it does, then the String from the pool is returned. Otherwise, this String object is added to the pool and a reference to this object is returned. For any two Strings s1 & s2, s1.intern() == s2.intern() only if s1.equals(s2) is true.

Q4. What is the difference between StringBuffer and StringWriter?

Ans. StringWriter is a Writer, it is nothing like StringBuffer and the purpose of each is so far from the other that it would be easier to explain the similarities which would be relegated to similarities that exist between all Objects. You should use a StringWriter when you want a Writer and a StringBuffer when you need a mutable buffer for constructing Strings.

So StringWriter is basically an adapter that adapts a StringBuffer to the Writer interface. A StringWriter is simply an implementation of TextWriter that uses a StringBuilder internally for storage. So you use StringWriter with APIs that operate on TextWriters.


Q5. What is the difference between String, StringBuilder and StringBuffer?

Ans. String objects are immutable and thus can’t be changed. If you try to modify and existing String object, a new String object will be created and its reference will be assigned to existing object.

StringBuilder objects are like String objects, except that they can be modified. String builder gives an additional capacity of 16 (16 empty elements), to each object by default.Strings should always be used unless string builders offer an advantage in terms of simpler code or better performance. For example, if you need to concatenate a large number of strings, appending to a StringBuilder object is more efficient.


A number of operations (for example, append(), insert(), or setLength()) can increase the length of the character sequence in the string builder so that the resultant length() would be greater than the current capacity(). When this happens, the capacity is automatically increased.


There is also a StringBuffer class that is exactly the same as the StringBuilder class, except that it is thread-safe by virtue of having its methods synchronized.


Q6. What is difference between String and StringTokenizer?

Ans. A StringTokenizer is utility class used to break up string. For example:
              StringTokenizer st = new StringTokenizer(”Hello World”);
              while (st.hasMoreTokens()) {
                    System.out.println(st.nextToken());
              }

Output:

Hello
World

Q7. Which design pattern is used by String class?

Ans. Flyweight. When you create a String constant as shown below:
              String s3 = “A”;
              s4= “A”;

It will check to see if it is already in the String pool. If it is in the pool, it will be picked up from the pool instead of creating a new one. Flyweights are shared objects and using them can result in substantial performance gains.


Q8. What happens when you use String concatenation operator?
Ans. When you are using String concatenation operator i.e "+", compiler creates a StringBuilder object to build the String s, and calls append() for each of the pieces. Finally, it calls toString() to produce the result. From this, it looks like we need not worry about using String or StringBuilder since compiler automatically takes care of that, but that's not true.

Ex: Say you are using "+" operator inside a loop; compiler will create a StringBuilder object every time inside a loop. However, as an efficient practice, you should create a StringBuilder outside the class.


Q9. Talk about Formatter class

Ans. Formatter is a translator that converts your format string and data into the desired result using format() method. When you create a Formatter object, you tell it where you want this result to go by passing that information to the constructor as below:
              Formatter f = new Formatter(System.out);

Q10. What is difference b/w printf() and format()?

Ans. Both are same and produce a single format string.

Q11. What is the difference b/w format() method in String and Formatter class.

Ans. String.format() is a static method which takes all the same arguments as Formatter’s format() but returns a String. It can come in handy when you only need to call format() once.

Interfaces - Basics & Marker Interfaces

Q1. Is Iterator a Class or Interface?
Ans: Iterator is an interface

Q2. Can we instantiate an object of an interface?
Ans. No, we can't. However, a class can contain an interface object inside it. 
Ex: If Window is an interface and there is a class WindowDecorator (irrespective of fact that whether class implements the interface or not), following is allowed:
class WindowDecorator {
    protected Window decoratedWindow; // the Window being decorated
  .........
  .........
However, one can't declare an object of Window, using new method i.e. following is not allowed
   Window w = new Window();

Q3. Can interfaces be nested or inherited?

Ans Yes. Like classes, interfaces can be nested or inherited to any level. However, there is no practical use of nested interfaces.

Q4. Is it possible to define a class inside interface?
Ans. Yes. It’s convenient to nest a class inside an interface when you want it.

Use:

1. This technique is sometimes used where the class is a constant type, return value or method argument in the interface.
2. When a class is closely associated with the use of an interface it is convenient to declare it in the same compilation unit. This proximity also helps ensure that implementation changes to either are mutually compatible.
3. One more reason is to have modifiable data within an interface (As you may know all fields in an interface are implicitly final). By declaring a class inside the interface you can have common MODIFIABLE data.

NOTES:

1. The class is always public and static and operates as a top level class. 
2. The static modifier does NOT have the same effect on a nested class as it does with class variables and methods.
3. The class methods cannot call the methods declared in the interface.

Q5. Can interface methods be made as protected or private, when a class implements these methods?

Ans. You can choose to explicitly declare the methods in an interface as public, but they are public even if you don’t say it. So when you implement an interface, the methods from the interface must be defined as public. Otherwise, they would default to package access, and you’d be reducing the accessibility of a method during inheritance, which is not allowed by Java compiler.

Q6. Does Java allow multiple inheritance?

Ans. Java does not allow multiple inheritance in classes, but it’s allowed in interfaces.

Q7. Can an anonymous class be declared as implementing an interface and extending a class?
Ans. An anonymous class may implement an interface or extend a superclass, but may not be declared to do both.

Q8. Can a class implement two interfaces which has got methods with same name and signatures or two interfaces with same variable names?
Ans. Yes, a class implement two interfaces which have got methods with same name and signatures. However, in case of same variable names, if variable is not declared in implementing class, the compiler will throw "field ambiguous" error.

Q9. What happens when a class is implementing two or more interfaces (i.e multiple inheritance) having a method with same names?
Ans. The difficulty occurs because overriding, implementation, and overloading get unpleasantly mixed together. However this works, as long as methods differ in the signature. But, overloaded methods cannot differ only by return type. In such a case, compiler gives an error.

interface I1 { void f(); }
interface I2 { int f(int i); }
interface I3 { int f(); }

class C {
     public int f() { return 1; }
}

class C2 implements I1, I2 {
     public void f() {}
     public int f(int i) { return 1; } // overloaded
}

class C3 extends C implements I2 {
     public int f(int i) { return 1; } // overloaded
}

class C4 extends C implements I3 {
     // Identical, no problem:
     public int f() { return 1; }
}

// Methods differ only by return type:
//! class C5 extends C implements I1 {}
//! interface I4 extends I1, I3 {}


Q10. Explain marker interface.
Ans. An interface contains method declarations (no implementation). Actual declaration and implementation is in Object class. A class can implement this interface simply by naming it in its implements clause without having to implement any methods.

A marker interface is just empty. It doesn’t contain any methods or variables. It’s just to tag the classes implementing the given interface. Hence, they are also known as tagged interfaces.

Ex: Serializable and Cloneable interface

In marker interfaces, Java code checks whether an object is an instance of the interface using the instanceof operator. For instance, if you call clone() method on an object without implementing cloneable interface, it throws CloneNotSupportedException.


Q11. How JVM will behave when it find some class is implementing Marker interface? Do JVM need to put some extra code in that class to handle marker interface implementation?

Ans. JVM does NOTHING at all here. It is the responsibility of the programmer to check with “objects instanceof Interface" and find out whether or not interface is implemented. As in the case of Cloneable interface, clone() method takes care of it.

protected Object clone () {

      if (this instanceOf Cloneable) {
            ....
      }
      else
      {
            throws CloneNotSupportedException;
      }
}

Q12. Explain clone() method.

Ans. clone() is a method in the Java programming language for object duplication. Since objects in Java are manipulated through reference variables, there is no direct way to copy an object.

Default implementation of clone() method throws a CloneNotSupportedException. Classes that want to allow cloning must implement the marker interface Cloneable and should override implementation of the clone() method. Remember that you must make it public and inside the method your first action must be super.clone(). Since the default implementation of Object.clone only performs a shallow copy, classes must also override clone to provide a custom implementation when a deep copy is desired.


Clone() is method in Object class, like wait(), sleep(), notify() and notifyall().


Q12. What happens if I don’t implement Cloneable interface & provide clone method on my own.

Ans. As a standard practice, we should call super.clone() in our clone implementation, which ends up calling Object’s clone() method and throws an exception if I don’t implement Cloneable interface. Now, if I forget to call super.clone() or don’t do it intentionally, in other words, there is no call to Object’s clone method, my program will compile and run successfully even if I don’t implement Cloneable interface. This is because; in this case it’s treated as a normal method and not a marker clone method.

Java Enums

Q1. What do you know about Java enums.
Ans.
1. This is automatically inherited from java.lang.Enum and hence, this can't extend any other class. However, Enum can implement interfaces.
2. Enum class is made as final implicitly and hence can't be inherited further.
3. Since it inherits java.lang.Enum, which implements interfaces Comparable and Serializable, enum class has compareTo() method and can be serialized.
4. Also, methods like equals(), hashcode() are automatically created for you.
5. Method ordinal() produces an int indicating the declaration order of each constant.
6. Enum constants are the first thing defined in Enum. Java forces you for same, as compiler gives an error otherwise.
7. Enum class can have methods. And if you have methods, you MUST end the sequence of enum instances with a semicolon.
8. Usually enums have to be qualified with type (i.e should be Color.Red and not Red). However, in Switch case statements, you can use them directly w/o qualifying.

public class Weather {

      public enum Season {WINTER, SPRING, SUMMER, FALL};
      private final Season season;
      private static final List<Weather> listWeather = new ArrayList<Weather> ();
      private Weather (Season season) { this.season = season;}
      public Season getSeason () { return season;}

      static {

            for (Season season : Season.values()) { //using J2SE 5.0 for each loop
                  listWeather.add(new Weather(season));
            }
      }

      public static ArrayList<Weather> getWeatherList () { return listWeather; }

      public String toString(){ return season;} 
             //takes advantage of toString() method of Season.
}

Q2. What are the different types of Enum?
Ans.
1. Simple Enum: 
public enum Color { WHITE, BLACK, RED, YELLOW, BLUE }

Usage:
Color c = Color.WHITE;
System.out.println(c); -> Prints "WHITE"
System.out.println(c.ordinal()); -> Prints "0"

2. Enum that overrides toString method: A semicolon after the last element is required to be able to compile it.
public enum Color { WHITE, BLACK, RED, YELLOW, BLUE;
       //Semicolon (;) is required here.

      @Override
      public String toString() {            //only capitalize the first letter
            String s = super.toString();
            return s.substring(0, 1) + s.substring(1).toLowerCase();
      }
}

3. Enum with additional fields and custom constructor: Enum constructors must be either private or package default, and protected or public access modifier is not allowed. When custom constructor is declared, all elements declaration must match that constructor.

public enum Color {
      WHITE(21, 0), BLACK(22, 5), RED(23, 3), YELLOW(24, 5), BLUE(25, 7);
      private int code;
      private int intensity;
      private Color(int c) {
            code = c;
            intensity = i;
      }

      public int getCode() {
            return code;
      }

      public int getIntensity() {
            return intensity;
      } 
}

4. Enum that implements interfaces: Enum can implement any interfaces. All enum types implicitly implements java.io.Serializable, and java.lang.Comparable.

public enum Color implements Runnable {
      WHITE, BLACK, RED, YELLOW, BLUE; 

      public void run() {
            System.out.println("name()=" + name() + ", toString()=" + toString());
      }
}

A sample test program to invoke this run() method:
for(Color c : Color.values()) { c.run(); }
OR
for(Runnable r : Color.values()) { r.run(); }

5. Enum that override methods inside enum variables or enum method/Constant Specific methods:

public enum Element{
      EARTH, WIND, FIRE {
            public String info() {
                  return "HOT";
            }
      };

      public String info() {
            return "element";
      }
}

Here enum method info() is overridden inside enum variable FIRE.

Q3. Why enum constants can't be used as method arguments.
Ans. Since each enum constant can have its own behaviour, thus treating Enum class as an interface type, one can implement polymorphic behaviour. However, enum instances can't be treated as class types. This is because each enum constant is a static final instance.
Ex: void foo (Color.RED r) => Having enum instance as an argument is not allowed.

Q4. Explain enum valueOf() method.
Ans. This is a static member of Enum class and produces the Enum instance corresponding to the string name passed to it. This throws an exception, if there is no match.

// Throws IllegalArgumentException if text is not known to enum type :
try {
      Color c = Enum.valueOf(Color.class, "WHITE");
}
catch (IllegalArgumentException ex) {
      log("WHITE is not a valid Color.");
}

Q5. What do you know about Enum values() method.

Ans. I won't define values() method in my enum class. Neither, it is defined in java.lang.Enum. Then from where does it come?

This is a static method added by compiler. Now, since it is a static method, it's not available to parent class i.e you can't type cast to Enum and then use values() method.


However, if you want to get enum instances from Enum class, please use the Class method getEnumConstants as below:

Enum c = Color.Red;
Set <String> methods;
// methods = c.values(); // Not allowed
methods = c.getClass().getEnumConstants(); //allowed

NOTE: If you are already in a Enum class, you can call values() method directly, i.e. without associating with an enum instance.Ex:

public enum Color
{
      RED, WHITE, BLACK;
      foreach(Color c: values())
      {
            System.out.println(c);
      }
}

Q6. Define EnumSet.
Ans. The EnumSet is designed for speed, because it must compete effectively with bit flags. Internally, it is represented by (if possible) a single long that is treated as a bit-vector, so it’s extremely fast and efficient. The benefit is that you now have a much more expressive way to indicate the presence or absence of a binary feature, without having to worry about performance. The elements of an EnumSet must come from a single enum.

Ex:
EnumSet<Color.class> set = EnumSet.noneOf(Color.class)
     // Here since enumSet is empty, all bits in long would be zero.
set.addAll(EnumSet.of(RED, WHITE));
    // Now since, 2 enum constants have been added to set, their corresponding bits will be 1 and rest bits will be zero.

EnumSets are built on top of longs, a long is 64 bits, and each enum instance requires one bit to indicate presence or absence. This means you can have an EnumSet for an enum of up to 64 elements without going beyond the use of a single long. If you have more than 64 elements in your enum, it adds another long.

Q7. What are the various methods of EnumSet.
Ans.
1. EnumSet.noneOf(Enum Class) -> Returns an empty EnumSet
2. EnumSet.allOf(Enum Class) -> Returns a EnumSet with all elements from EnumClass
3. EnumSet.of(Enum constant1, Enum constant2, ......) -> Returns selected Enum constants
4. EnumSet.range(Enum constant1, Enum constantN) -> Returns Enum constants whose ordinal value lies b/w constant1 and constantN
5. EnumSet.complementOf(Enum constant1, Enum constant2, ......) -> Returns all constants from Enum class except those specified

Q8. Explain "of" method of Enum class.
Ans. Interesting — the of() method has been overloaded both with varargs and with individual methods taking two through five explicit arguments. This is an indication of the concern for performance with EnumSet, because a single of( ) method using varargs could have solved the problem, but it’s slightly less efficient than having explicit arguments. Thus, if you call of() with two through five arguments you will get the explicit (slightly faster) method calls, but if you call it with one argument or more than five, you will get the varargs version of of(). Notice that if you call it with one argument, the compiler will not construct the varargs array and so there is no extra overhead for calling that version with a single argument.

Q9. Explain EnumMap.
Ans. An EnumMap is a specialized Map that requires that its keys be from a single enum. Because of the constraints on an enum, an EnumMap can be implemented internally as an array. Thus they are extremely fast, so you can freely use EnumMaps for enum-based lookups. You can only call put() for keys that are in your enum, but other than that it’s like using an ordinary Map.
EnumMap<Color,Integer> em = new EnumMap<Color,Command>(Color.class);
There is always a key entry for each of the enums, but the value is null unless you have called put() for that key.

Q10. Explain the order of elements in EnumSet and EnumMap.
Ans. Both EnumSet and EnumMap, the order of elements is determined by their order of definition in the enum and not in the order in which they are added.


Tuesday, 27 August 2013

Singleton, Serialization, RMI, Proxy Design Pattern

Singleton

Q1. What is a singleton pattern? How do you code it in Java?
Ans. A singleton is a class that can be instantiated only one time in a JVM per class loader. Repeated calls always return the same instance. Ensures that a class has only one instance, and provide a global point of access. It can be an issue if singleton class gets loaded by multiple class loaders or JVMs.

public class OnlyOne {
      private static OnlyOne one = new OnlyOne();
            // private constructor. This class cannot be instantiated from outside and prevents subclassing.
      private OnlyOne(){}
      public static OnlyOne getInstance() {
            return one;
      }
}

Q2. What is the difference b/w Singleton and Static class?
Ans. Static class is one approach to make a class singleton by declaring all the methods as static so that you can’t create any instance of that class and can call the static methods directly.

A static class is one that has only static methods. The design style embodied in a static class is purely procedural.

Singleton, on the other hand, is a pattern specific to OO design. It is an instance of an object (with all the possibilities inherent in that, such as polymorphism), with a creation procedure that ensures that there is only ever one instance of that particular role over its entire lifetime.

Singleton pattern has several advantages over static classes. First, a singleton can extend classes and implement interfaces, while a static class cannot (it can extend classes, but it does not inherit their instance members). A singleton can be initialized lazily or asynchronously while a static class is generally initialized when it is first loaded, leading to potential class loader issues. However the most important advantage, though, is that singletons can be handled polymorphically without forcing their users to assume that there is only one instance.

Q3. Difference between Singleton and Flyweight Design pattern?
Ans.
Singleton pattern is a CREATIONAL pattern in which only one instance is created and the same instance is reused multiple times by different users.

Flyweight pattern is a STRUCTURAL pattern which is useful in scenarios where there are lot of objects which share some common intrinsic information. Instead of storing the common intrinsic information n times for n objects, it is stored only once in one object and is referenced by all the objects which want to use it.

(This object which contains all the common intrinsic information is called the flyweight object)

Q4. How to share a Singleton across multiple JVM's.
Ans. For Serializable and Externalizable classes, readResolve method allows a class to replace/resolve the object read from the stream before it is returned to the caller. By implementing the readResolve method, a class can directly control the types and instances of its own instances being deserialized. The method is defined as follows:
        ANY-ACCESS-MODIFIER Object readResolve() throws ObjectStreamException;

Ex:
public class Singleton {       public static final Singleton INSTANCE = new Singleton(); 
      private Singleton () { }
      private Object readResolve() { 
            return INSTANCE; 
      } 
}

The readResolve method is called when ObjectInputStream has read an object from the stream and is preparing to return it to the caller. readResolve is called after readObject has returned (conversely writeResolve is called before writeObject and probably on a different object). ObjectInputStream checks whether the class of the object defines the readResolve method. If the method is defined, the readResolve method is called to substitute another instance in the place of the one created by readObject (i.e to allow the object in the stream to designate the object, INSTANCE here, to be returned). The object reference returned by this method is then replaced in place of newly created object.

And before the object is written, it is written to object output stream. writeReplace() method on the other side takes care of the same. The writeReplace() method allows a class of an object to nominate its own replacement in the stream before the object is written. By implementing the writeReplace method, a class can directly control the types and instances of its own instances being serialized.

The writeReplace method is called when ObjectOutputStream is preparing to write the object to the stream. The ObjectOutputStream checks whether the class defines the writeReplace method. If the method is defined, the writeReplace method is called to allow the object to designate its replacement in the stream. The object returned should be either of the same type as the object passed in or an object that when read and resolved will result in an object of a type that is compatible with all references to the object. If it is not, a ClassCastException will occur when the type mismatch is discovered.

For example, a Singleton class could be created for which only a single instance of each symbol binding existed within a virtual machine. The readResolve method would be implemented to determine if that singleton was already defined and substitute the preexisting equivalent Singleton object to maintain the identity constraint. In this way the uniqueness of Singleton objects can be maintained across serialization.

Methods are defined as follows:
            ANY-ACCESS-MODIFIER Object writeReplace()/readResolve() throws ObjectStreamException;


Serialization

Q5. What is Java Serial Version ID?
Ans. Say you create a “Car” class, instantiate it, and write it out to an object stream. The flattened car object sits in the file system for some time. Meanwhile, if the “Car” class is modified by adding a new field. Later on, when you try to read (i.e. deserialize) the flattened “Car” object, you get the java.io.InvalidClassException – because all serializable classes are automatically given a unique identifier. This exception is thrown when the identifier of the class is not equal to the identifier of the flattened object. If you really think about it, the exception is thrown because of the addition of the new field. You can avoid this exception being thrown by controlling the versioning yourself by declaring an explicit serialVersionUID. There is also a small performance benefit in explicitly declaring your serialVersionUID (because does not have to be calculated). So, it is best practice to add your own serialVersionUID to your Serializable classes as soon as you create them as shown below:

public class Car {
      static final long serialVersionUID = 1L; //assign a long value
}

Q6. Is it possible that class fields/members are not written to stream in the serialization process?
Ans. There are three exceptions in which serialization doesn't necessarily read and write to the stream. These are
1. Serialization ignores static fields, because they are not part of any particular state.
2. Base class fields are only handled if the base class itself is serializable.
3. Transient fields.

Q7. What happens to the object references included in the object during serialization process?
Ans. The serialization mechanism generates an object graph for serialization. Thus it determines whether the included object references are serializable or not. This is a recursive process. Thus when an object is serialized, all the included objects are also serialized along with the original object.

NOTE: One should make sure that all the included objects are also serializable. If any of the objects is not serializable then it throws a NotSerializableException.

Q8. What is Externalizable interface?
Ans. Basically SERIALIZABLE uses default implementation for reading and writing the object you want to persist.  You just have to implement SERIALIZABLE interface for your class and rest will be taken care. Java runtime will use reflection to figure out how to marshal and unmarshal your objects.

Thus, if you want to have control over how to read and write objects, you can use Externalizable interface. This contains two methods readExternal and writeExternal
which you will need to implement with your own way of storing the information and retrieving the information of the object. These methods give you a control over the serialization mechanism. Thus if your class implements this interface, you can customize the serialization process by implementing these methods.

Q9. Why do we need Externalizable interface? Can't we override writeObject method?
Ans. writeObject() method is defined in ObjectOutputStream class and is declared as final. Hence, you can't override writeObject() method. Similar is the case with readObject() method, which is defined in ObjectInputStream class and also declared as final. Thus, to have control over the serialization process, you need to implement Externalizable interface and define the writeExternal() and readExternal() methods.

Q10. When should we use Externalizable interface?
Ans.  We should use it when it is required to have control over serialization process. Unless you have very specific requirements one wouldn't use EXTERNALIZABLE. 

In earlier version of Java, reflection was very slow, and so serializing large object graphs (e.g. in client-server RMI applications) was a bit of a performance problem. To handle this situation, thejava.io.Externalizable interface was provided, which is like java.io.Serilizable but with custom-written mechanisms to perform the marshalling and unmarshalling functions. This gives you the means to get around the reflection performance bottleneck.

In recent versions of java (1.3 onwards, certainly) the performance of reflection is vastly better than it used to be, and so this is much less of a problem. 

Biggest drawback of Externalizable is that you have to maintain this logic yourself - if you add, remove or change a field in your class, you have to change your writeExternal/readExternal methods to account for it.


RMI & Proxy Design Pattern

Q11. Explain the RMI architecture.
Ans: Java Remote Method Invocation (RMI) provides a way for a Java program on one machine to communicate with objects residing in different JVMs (i.e. different processes or address spaces). The important parts of the RMI architecture are the stub class, object serialization and the skeleton class. RMI uses a layered architecture where each of the layers can be enhanced without affecting the other layers. The layers can be summarized as follows:
1. Application Layer: The client and server program.
2. Stub & Skeleton Layer: Intercepts method calls made by the client. It redirects these calls to a remote RMI service.
3. Remote Reference Layer: Sets up connections to remote address spaces, manages connections, and understands how to interpret and manage references made from clients to the remote service objects.
4. Transport layer: Based on TCP/IP connections between machines in a network. It provides basic connectivity, as well as some firewall penetration strategies.

Design pattern: RMI stub classes provide a reference to a skeleton object located in a different address space on the same or different machine. This is a typical example of a proxy design pattern (i.e. remote proxy), which makes an object executing in another JVM appear like a local object. In JDK 5.0 and later, the RMI facility uses dynamic proxies instead of generated stubs, which makes RMI easier to use.

RMI runtime steps (as shown in the diagram above) involved are:
Step 1: Start RMI registry and then RMI server. Bind the remote objects to the RMI registry.
Step 2: The client process will look up the remote object from the RMI registry.
Step 3: The lookup will return the stub to the client process from the server process.
Step 4: The client process will invoke method calls on the stub. The stub calls the skeleton on the server process through the RMI reference manager.
Step 5: The skeleton will execute the actual method call on the remote object and return the result or an exception to the client process via the RMI reference manager and the stub.

Q12. What are stubs and skeletons?
Ans.  STUB: Stub is a client side proxy; the purpose of which is marshalling the data. It acts as a proxy for remote object. The caller invokes a method on the local stub which is responsible for carrying out the method call on the remote object. In RMI, a stub for a remote object implements the same set of remote interfaces that a remote object implements.
Marshalling: It is the process of converting the java code (source code) into network oriented stream (bit-blobs stream)

SKELETON: Skeleton is a server side proxy; the purpose of which is converting the network oriented stream into java program (human readable format) i.e unmarshalling.

RMI Process
All network-related code is put in the stub and skeleton so that the client and server won't have to deal with the network and sockets in their code. The stub implements the same Remote interface (as in an interface that extends java.rmi.Remote) that the server implements. So, the client can call the same methods in the stub that it wants to call in this server. But, the functions in the stub are filled with network-related code, not the actual implementation of the required function. For example, if a server implements an add(int,int) function, the stub also will have an add(int,int) function, but it won't contain the actual implementation of the addition function; instead, it will contain the code to connect to the remote skeleton, to send details about the function to be invoked, to send the parameters, and to get the results back.
So, this will be the situation:
Client <--->stub <---> [NETWORK] <---> skeleton <---> Server

The client speaks to the stub, the stub speaks to the skeleton through the network, the skeleton speaks to the server, the server executes the required function, and the results are also passed in the same way. So, you will have four separate programs corresponding to each of these entities (this means four class files).

Later, in JDK 1.2, the skeletons were incorporated into the server itself so that there are no separate entities as skeletons. In other words, the skeletons's functionality was incorporated into the server itself. So, the scenario became like this:
Client <---> stub  <--->  [NETWORK]  <---> Server_with_skeleton_functionality

Q13. What design pattern does RMI use?
Ans. RMI. Stubs and skeletons work as proxy objects and thus RMI is based on proxy design pattern.

Q14. What is a remote object and what is RMI server?
Ans: A remote object is one whose methods can be invoked from another JVM (i.e. another process). A remote object class MUST implement the Remote interface i.e java.rmi.Remote.

A RMI Server is an application that creates a number of remote objects. An RMI Server is responsible for
1. Creating an instance of the remote object (e.g. CarImpl instance = new CarImpl()).
2. Exporting the remote object.
3. Binding the instance of the remote object to the RMI registry.

Q15. How will you pass parameters in RMI?
Ans:
1. Primitive types are passed by value (e.g. int, char, boolean etc).
2. References to remote objects (i.e. objects which implement the Remote interface) are passed as remote references that allow the client process to invoke methods on the remote objects.
3. Non-remote objects are passed by value using object serialization. These objects should allow them to be serialized by implementing the java.io.Serializable interface

The client process initiates the invocation of the remote method by calling the method on the stub. The stub (client side proxy of the remote object) has a reference to the remote object and forwards the call to the skeleton (server side proxy of the remote object) through the reference manager by marshalling the method arguments. During marshalling, each object is checked to determine whether it implements java.rmi.Remote interface. If it does then the remote reference is used as the marshalled data otherwise the object is serialized into byte streams and sent to the remote process where it is de-serialized into a copy of the local object. The skeleton converts this request from the stub into the appropriate method call on the actual remote object by un-marshalling the method arguments into local stubs on the server (if they are remote reference) or into local copy (if they are sent as serialized objects).

Q16. What are the services provided by the RMI Object?
Ans: In addition to its remote object architecture, RMI provides some basic object services, which can be used in a distributed application. These services are
1. Object naming/registry service: RMI servers can provide services to clients by registering one or more remote objects with its local RMI registry.
2. Object activation service: It provides a way for server (i.e. remote) objects to be started on an as-needed basis. Without the remote activation service, a server object has to be registered with the RMI registry service.
3. Distributed garbage collection: It is an automatic process where an object, which has no further remote references, becomes a candidate for garbage collection.


Mix topics - AOP, Ajax & WebServices

Q1. What is AOP i.e. aspect oriented programming and what is the need for same?
Ans. AOP is a new technology for separating crosscutting concerns into single units called aspects. It encapsulates behaviours that affect multiple classes into reusable modules. It entails breaking down program logic into distinct parts known as crosscutting concerns that are usually hard to do in object-oriented programming. These units/concerns are termed aspects; hence the name aspect oriented programming.

For ex: Logging. It crosscuts all logged classes and methods. Suppose we do logging at both the beginning and the end of each function body. This will result in crosscutting all classes that have at least one function. Other typical crosscutting concerns include context-sensitive error handling, performance optimization, and design patterns.

With AOP, we start by implementing our project using our OO language (for example, Java), and then we deal separately with crosscutting concerns in our code by implementing aspects. Finally, both the code and aspects are combined into a final executable form using an aspect weaver.



Above figure explains the weaving process. You should note that the original code doesn't need to know about any functionality the aspect has added; it needs only to be recompiled without the aspect to regain the original functionality. In that way, AOP complements object-oriented programming and doesn't replace it.

Q2. What are the differences between OOP and AOP?
Ans:

OOP
AOP
OOP looks at an application as a set of collaborating objects. OOP code scatters system level code like logging, security etc with the business logic code.
AOP looks at the complex software system as combined implementation of multiple concerns like business logic, data persistence,   logging, security, and so on. Separates business logic code from the system level code. In fact one concern remains unaware of other concerns.
OOP nomenclature has classes, objects, interfaces etc.
AOP nomenclature has join points, point cuts, advice, and aspects.
Provides benefits such as code reuse, flexibility, improved maintainability, modular architecture, reduced development time etc with the help of polymorphism, inheritance and encapsulation.
AOP implementation coexists with the OOP by choosing OOP as the base language.
Ex: AspectJ uses Java as the base language.

AOP provides benefits provided by OOP plus some additional benefits.

Q3. How many different types of JDBC drivers are present? Discuss them.
Ans. There are four JDBC driver types. 

Type 1: JDBC-ODBC Bridge plus ODBC Driver: The first type of JDBC driver is the JDBC-ODBC Bridge. It is a driver that provides JDBC access to databases through ODBC drivers. The ODBC driver must be configured on the client for the bridge to work. This driver type is commonly used for prototyping or when there is no JDBC driver available for a particular DBMS.

Type 2: Native-API partly-Java Driver: The Native to API driver converts JDBC commands to DBMS-specific native calls. This is much like the restriction of Type 1 drivers. The client must have some binary code loaded on its machine. These drivers do have an advantage over Type 1 drivers because they interface directly with the database.

Type 3: JDBC-Net Pure Java Driver: The JDBC-Net drivers are a three-tier solution. This type of driver translates JDBC calls into a database-independent network protocol that is sent to a middleware server. This server then translates this DBMS-independent protocol into a DBMS-specific protocol, which is sent to a particular database. The results are then routed back through the middleware server and sent back to the client. This type of solution makes it possible to implement a pure Java client. It also makes it possible to swap databases without affecting the client.

Type 4: Native-Protocol Pure Java Driver: These are pure Java drivers that communicate directly with the vendor’s database. They do this by converting JDBC commands directly into the database engine’s native protocol. This driver has no additional translation or middleware layer, which improves performance tremendously. 

Q4. What is Ajax.
Ans: There is a lot of hype surrounding the latest Web development Ajax (Asynchronous JavaScript And XML). The intent of Ajax is to make Web pages more responsive and interactive by exchanging small amounts of data with the server behind the scenes without refreshing the page, so that the entire Web page does not have to be reloaded each time the user makes a change. Ajax technique uses a combination of JavaScript, XHTML (or HTML) and XMLHttp.

Q5. Web services
Ans. Web service is an implementation technology and one of the ways to implement SOA (Service Oriented Architecture). You can build SOA based applications without using Web services – for example by using other traditional technologies like Java RMI, EJB, JMS based messaging, etc. But what Web services offer is the standards based and platform independent service via HTTP, XML, SOAP, WSDL and UDDI, thus allowing interoperability between heterogeneous technologies such as J2EE and .NET. 

Web services are language and platform independent. Web service uses language neutral protocols such as HTTP and communicates between disparate applications by passing XML messages to each other via a Web API (Messages must be in XML and binary data attachments). Interfaces must be based on Internet protocols such as HTTP, FTP and SMTP. There are two main styles of Web services: SOAP and REST.

A service is an application that exposes its functionality through an API (Application Programming Interface). A service is a component that can be used remotely through a remote interface either synchronously or asynchronously. The term service also implies something special about the application design, which is called a service-oriented architecture (SOA). One of the most important features of SOA is the separation of interface from implementation. A service exposes its functionality through interface and interface hides the inner workings of the implementation.

For ex: Google also provides a Web service interface through the Google API to query their search engine from an application rather than a browser. 

Q6. SOAP
Ans: SOAP stands for Simple Object Access Protocol. It is an XML based lightweight protocol, which allows software components and application components to communicate, mostly using HTTP (can use SMTP etc). SOAP sits on top of the HTTP protocol. SOAP is nothing but XML message based document with pre-defined format. SOAP is designed to communicate via the Internet in a platform and language neutral manner and allows you to get around firewalls as well. Let’s look at thr structure of a SOAP message:

  • A SOAP message MUST be encoded using XML
  • A SOAP message MUST use the SOAP Envelope namespace
  • A SOAP message MUST use the SOAP Encoding namespace
  • A SOAP message must NOT contain a DTD reference
  • A SOAP message must NOT contain XML Processing Instructions
Q7. WSDL
Ans: WSDL stands for Web Services Description Language. A WSDL document is an XML document that describes how the messages are exchanged. Let’s say we have created a Web service. Who is going to use that and how does the client know which method to invoke and what parameters to pass? There are tools that can generate WSDL from the Web service. Also there are tools that can read a WSDL document and create the necessary code to invoke the Web service. So the WSDL is the Interface Definition Language (IDL) for Web services. 

Q8. UDDI
Ans: UDDI stands for Universal Description Discovery and Integration. UDDI provides a way to publish and discover information about Web services. UDDI is like a registry rather than a repository. A registry contains only reference information like JNDI etc.

So far we have looked at some open standards/protocols relating to Web services, which enable interoperability between disparate systems (e.g. Between .Net and J2EE etc). These standards provide a common and interoperable approach for defining (WSDL), publishing (UDDI) and using (SOAP) Web services. 

Q9. SAX Vs DOM Parser
Ans. Main differences between SAX (Simple API for XML) and DOM (Document Object Model), which are the two most popular APIs for processing XML documents in Java, are:-
  • Read v/s Read/Write: SAX can be used only for reading XML documents and not for the manipulation of the underlying XML data whereas DOM can be used for both read and write of the data in an XML document.
  • Sequential Access v/s Random Access: SAX can be used only for a sequential processing of an XML document whereas DOM can be used for a random processing of XML docs. So what to do if you want a random access to the underlying XML data while using SAX? You got to store and manage that information so that you can retrieve it when you need.
  • Call back v/s Tree: SAX uses call back mechanism and uses event-streams to read chunks of XML data into the memory in a sequential manner. A SAX parser does not create any internal structure. Instead, it takes the occurrences of components of an input document as events (i.e., event driven), and tells the client what it reads as it reads through the input document, whereas a DOM parser creates a tree structure in memory from an input document and then waits for requests from client and facilitates random access/manipulation of the underlying XML data.
  • API: From functionality point of view, SAX provides a fewer functions which means that the users themselves have to take care of more, such as creating their own data structures. A DOM parser is rich in functionality. It creates a DOM tree in memory and allows you to access any part of the document repeatedly and allows you to modify the DOM tree. 
  • XML-Dev mailing list v/s W3C: SAX was developed by the XML-Dev mailing list whereas DOM was developed by W3C (World Wide Web Consortium).
  • Information Set: SAX doesn't retain all the info of the underlying XML document such as comments whereas DOM retains almost all the info. New versions of SAX are trying to extend their coverage of information.
Usual Misconceptions
SAX is always faster: this is a very common misunderstanding and one should be aware that SAX may not always be faster because it might not enjoy the storage-size advantage in every case due to the cost of call backs depending upon the particular situation, SAX is being used in.
DOM always keeps the whole XML doc in memory: it's not always true. DOM implementations not only vary in their code size and performance, but also in their memory requirements and few of them don't keep the entire XML doc in memory all the time. Otherwise, processing/manipulation of very large XML docs may virtually become impossible using DOM, which is of course not the case.

Q10. How to choose one between SAX & DOM?
Ans. It primarily depends upon the requirement. If the underlying XML data requires manipulation then almost always DOM will be used as SAX doesn't allow that. Similarly if the nature of access is random (for example, if you need contextual info at every stage) then DOM will be the way to go in most of the cases. But, if the XML document is only required to be read and that too sequentially, then SAX will probably be a better alternative in most of the cases. SAX was developed mainly for pasring XML documents and it's certainly good at it. Use DOM when your application has to access various parts of the document and using your own structure is just as complicated as the DOM tree. If your application has to change the tree very frequently and data has to be stored for a significant amount of time.

Q11. What is a socket? How do you facilitate inter process communication in Java?
Ans: A socket is a communication channel, which facilitates inter-process communication (ex: communicating between two JVMs). A socket is an endpoint for communication. There are two kinds of sockets, depending on whether one wishes to use a connectionless or a connection-oriented protocol. 
1. The connectionless communication protocol of the Internet is called UDP
2. The connection-oriented communication protocol of the Internet is called TCP. 

UDP sockets are also called datagram sockets. Each socket is uniquely identified on the entire Internet with two numbers. First number is a 32-bit (IPV4 or 128-bit is IPV6) integer called the IP address is the location of the machine, which you are trying to connect to. Second number is a 16-bit integer called the port of the socket, port on which the server you are trying to connect is running. The port numbers 0 to 1023 are reserved for standard services such as e-mail, FTP, HTTP etc.

Q12. Memory map file.
Ans. Memory-mapping a file uses the OS virtual memory to access the data on the file system directly, instead of using normal I/O functions. Most modern OS that support virtual memory also run each process in its own dedicated address space, allowing a program to be designed as though it has sole access to the virtual memory. Use mmap to make a connection between your address space and the file on the disk. Memory mapped files are loaded into memory one entire page at a time. The page size is selected by the operating system for maximum performance.While memory mapped files offer a way to read and write directly to a file at specific locations, the actual action of reading/writing to the disk is handled at a lower level. Consequently, data is not actually transferred at the time the above instructions are executed. Instead, much of the file input/output (I/O) is cached to improve general system performance. You can override this behavior and force the system to perform disk transactions immediately by using the memory-mapped file function FlushViewOfFile.

Note: jmap prints shared object memory maps or heap memory details of a given process or core file or a remote debug server.

Benefits:

  1. Increased I/O Performance: Especially when used on large files. For small files, memory-mapped files can result in a waste of slack space as memory maps are always aligned to the page size, which is mostly 4 KB. Therefore a 5 KB file will allocate 8 KB and thus 3 KB are wasted. Accessing memory mapped files is faster for two reasons. Firstly, it does not involve a separate system call for each access. Secondly, in most OS the memory region mapped actually is the kernel's page cache (file cache), meaning that no copies need to be created in user space. It does not require copying data between buffers – the memory is accessed directly.
  2. Faster read/write operations: Applications can access and update data in the file directly and in-place, as opposed to seeking from the start of the file or rewriting the entire edited contents to a temporary location. Since the memory-mapped file is handled internally in pages, linear file access requires disk access only when a new page boundary is crossed, and can write larger sections of the file to disk in a single operation.
  3. Lazy Loading: It uses small amounts of RAM even for a very large file. Trying to load the entire contents of a file that is significantly larger than the amount of memory available can cause severe thrashing.


Drawbacks:

  1. The memory mapped approach has its cost in minor page faults - when a block of data is loaded in page cache, but is not yet mapped into the process's virtual memory space. In some circumstances, memory mapped file I/O can be substantially slower than standard file I/O.
  2. Another drawback relates to a given architecture's address space - a file larger than the addressable space can have only portions mapped at a time, complicating reading it. For ex: a 32-bit architecture such as Intel's IA-32 can only directly address 4 GB or smaller portions of files.
Common uses

  1. Most common use is the process loader in most modern OS (including Windows & Unix). When a process is started, OS uses a memory mapped file to bring the executable file, along with any loadable modules, into memory for execution. 
  2. Another common use is to share memory between multiple processes. In modern OS, processes are generally not permitted to access memory space that is allocated for use by another process. There are a number of techniques available to safely share memory, and memory-mapped file I/O is one of the most popular. Two or more applications can simultaneously map a single physical file into memory and access this memory.
Platform support
Most modern OS or runtime environments support some form of memory mapped file access. The function mmap(), which creates a mapping of a file given a file descriptor, starting location in the file, and a length, is part of the POSIX specification. So, POSIX-compliant systems, such as Unix, Linux, Mac OS etc. support a common mechanism for memory mapping files. The mmap() function establishes a mapping between a process' address space and a stream file.
The Microsoft Windows operating systems also support a group of API functions for this purpose, such as CreateFileMapping(). Java provides classes and methods to access memory mapped files, such as FileChannel.

Q13. MemoryMapFile Usage example.
Ans. We used the FileChannel class along with the ByteBuffer class to perform memory-mapped IO for data of type byte. These byte is then retrieved by using get() method of ByteBuffer class.
FileChannel: An abstract class used for reading, writing, mapping, and manipulating a file.
ByteBuffer: An abstract class which provides methods for reading and writing values of all primitive types except Boolean.
map() method: This method maps the region of the channel's file directly into memory.
size() method: This method returns the current size of this channel's file.

Usage Modes:
FileChannel.MapMode.PRIVATE: Mode for a private (copy-on-write) mapping.
FileChannel.MapMode.READ_ONLY: Mode for a read-only mapping.
FileChannel.MapMode.READ_WRITE: Mode for a read/write mapping.

Usage example:
File file = new File("filename");

// Create a read-only memory-mapped file
FileChannel roChannel = new RandomAccessFile(file, "r").getChannel();
ByteBuffer roBuf = roChannel.map(FileChannel.MapMode.READ_ONLY, 0,  (int)roChannel.size());

// Create a read-write memory-mapped file
FileChannel rwChannel = new RandomAccessFile(file, "rw").getChannel();
ByteBuffer wrBuf = rwChannel.map(FileChannel.MapMode.READ_WRITE,0,(int)rwChannel.size());

// Create a private (copy-on-write) memory-mapped file.
// Any write to this channel results in a private copy of the data.
FileChannel pvChannel = new RandomAccessFile(file, "rw").getChannel();
ByteBuffer pvBuf = roChannel.map(FileChannel.MapMode.READ_WRITE,0,(int)rwChannel.size());
Although the return value from map() is assigned to a ByteBuffer variable, it's actually a MappedByteBuffer. Most of the time there's no reason to differentiate, but the latter class has two methods that some programs may find useful - load() and force().
The load() method will attempt to load all of the file's data into RAM, trading an increase in startup time for a potential decrease in page faults later. This is a form of premature optimization. Unless your program constantly accesses those pages, OS may choose to use them for something else, meaning that you'll have to fault them in. To flush dirty pages to disk, call the buffer's force() method.

buf.putInt(0, 0x87654321);
buf.force();Above two lines of code are actually an anti-pattern: you don't want to flush dirty pages after every write. Take a lesson from database developers, and group your changes into atomic units.

Q14. Mapping Files Bigger than 2 GB
Ans. Depending on your filesystem, you can create files larger than 2GB. But ByteBuffer uses an int for all indexes, which means that buffers are limited to 2GB, which means that you need to create multiple buffers to work with large files.

Sol1: Create those buffers as needed. The same underlying FileChannel can support as many buffers as you can create, limited only by the OS and available virtual memory; simply pass a different starting offset each time. The problem with this approach is that creating a mapping is expensive, because it's a kernel call (and you're using mapped files to avoid kernel calls). In addition, a page table full of mappings will mean more expensive context switches. As a result, as-needed buffers aren't a good approach unless you can divide the file into large chunks that are processed as a unit.

Sol2: Create a “super buffer” that maps the entire file and presents an API that uses long offsets. Internally, it maintains an array of mappings with a known size, so that you can easily translate the original index into a buffer and an offset within that buffer:
public int getInt(long index) {
      return buffer(index).getInt();
}

private ByteBuffer buffer(long index) {

      ByteBuffer buf = _buffers[(int)(index / _segmentSize)];
      buf.position((int)(index % _segmentSize));
      return buf;
}


What's a good value for _segmentSize? Your first thought might be Integer.MAX_VALUE, since this is the maximum index value for a buffer. While that would result in the fewest number of buffers to cover the file, it has one big flaw - you won't be able to access multi-byte values at segment boundaries. Instead, you should overlap buffers, with the size of the overlap being the maximum sub-buffer that you need to access.

NOTE: Buffer will persist after the channel is closed, it's removed by the garbage collector (and this explains the reason that MappedByteBuffer doesn't have its own close() method).

Q15. Garbage Collection of Direct/Mapped Buffers
Ans. How does the non-heap i.e. virtual memory for direct buffers and mapped files get released? After all, there's no method to explicitly close or release them. The answer is that they get garbage collected like any other object, but with one twist: if you don't have enough virtual memory space, that will trigger a full collection even if there's plenty of heap memory available. Normally, this won't be an issue: you probably won't be allocating and releasing direct buffers more often than heap-resident objects. If, however, you see full GC's appearing when you don't think they should, take a look at your program's use of buffers.