Wednesday 9 July 2014

Abstraction Vs Encapsulation

 Difference  between Abstraction and encapsulation 

  • Abstraction focuses on the outside view of an object (i.e. the interface) Encapsulation (information hiding) prevents clients from seeing it’s inside view, where the behavior of the abstraction is implemented.
  • Abstraction solves the problem in the design side while Encapsulation is the Implementation.
  • Encapsulation is the deliverable of Abstraction. Encapsulation barely talks about grouping up your abstraction to suit the developer needs. 
eg . Interface and the class implementing the interface 

eg . public interface ABC {
             void display();
}

Here abstraction is provided by the interface meaning it will be the face of the class implementing the interface ABC

Encapsulation can be done as 

public class DEF implements ABC{
             void display(){
                    System.out.println("DEF");
             }
}

In the above example 

Inteface ABC provides abstraction , while class DEF provides encapsulation i.e. binding the data and the code together.

What happens when a static method is invoked using a null object reference

No we can't.

It will throw NullPointerException because null is not an object in java. If method is static it will run & if method is non static it will through an NPEnull does not represent some "base" object, it represents a reference which does not point to any object at all.

For Example:-
class TestClass {
    static void myMethod() {
        System.out.println("Hello World...");
    }
}

class Test {
    public static void main(String[] args) {
        TestClass tc = null;        tc.myMethod();    }
}

Above code will print "Hello World..." as myMethod() is static method.

Sunday 6 July 2014

JVM parameters in Java

On the basis of how we specify JVM option it can be divided into two parts, JVM Options which starts with –X and those which starts with -XX:

1) JVM Options that begin with -X are non-standard (thy are not guaranteed to be supported on all JVM implementations), and are subject to change without notice in subsequent releases of the JDK.

2) JVM Options or parameters which are specified with -XX are not stable and are not recommended for casual use. These options are subject to change without notice also.



Key Points about JVM Options:

1) Boolean JVM options can be turned on with -XX:+ and can be turned off with -XX:-.
2) Numeric JVM Options can be set with -XX:=. Numbers can include 'm' or 'M' for megabytes, 'k' or 'K' for kilobytes, and 'g' or 'G' for gigabytes (for example, 32k is the same as 32768).
3) String JVM options can be set by using -XX:=, and usually used to specify a file, a path, or a list of commands.

The command java -help lists the standard options (standard across different JVM implementations) for the Java application launcher. The command java -X can be used to see the Java application launcher's non-standard (X for extension specific to that JVM) arguments.The -X options are non-standard and subject to change without notice. If you wish to detect which JVM arguments your currently running Java application is using, you can try below code.

Read JVM Parameters Runtime

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.List;

public void runtimeParameters() {
          RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();
          List<String> aList = bean.getInputArguments();
          for (int i = 0; i < aList.size(); i++) {
                    System.out.println( aList.get( i ) );
          }
}