Monday, November 30, 2015

Java Tricky Output Questions and Concepts

Java Tricky Output Questions and Concepts:

1) Static Method

public class Test {
       public static void show() {
              System.out.println("Static method called");
       }
       public static void main(String[] args) {
              Test obj = null;
              obj.show();
       }
}
Output:

Static method called

We can call static methods using reference variable which is pointing to null because static methods are class level so we can either call using class name and reference variable which is pointing to null.

2) Static block

public class Test {               
               static int a = 1111;
               {  
                      a = a-- - --a;
               }
               {
                      a = a++ + ++a;
               }
               
               public static void main(String[] args)  {
                          System.out.println(a);    
                  }
              }

Output:
2

Static block initializes and change a to 2. Simple block scope won’t affect the value of a

3) Compilers not able to understand which overloaded method to call...

public class Test {
       public static void main(String[] args) {
              Integer i = new Integer(null);
              String s = new String(null);
       }
}

public class Test {
           public static void main(String[] args) {
            System.out.println(null);
           }
}

Output :
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
 The constructor String(String) is ambiguous

 at com.staticMethod.Test.main(Test.java:15)

Explanation :

Suppose you have two overloaded methods, like this:
public void method(Object o) {
    // ...
}

public void method(String s) {
    // ...
}

When you call method(null), both these methods apply. Java chooses the most specific one, which is in this case the second method, that takes a String - because String is a more specific type than Object.
However, sometimes the most specific constructor or method cannot be determined. If we look at the constructors of class String that take one argument:

String(byte[] bytes)
String(char[] value)
String(String original)
String(StringBuffer buffer)
String(StringBuilder builder)
Note that there is no hierarchy between the types byte[]char[]StringStringBuffer and StringBuilder, so it's not possible to say that one of these constructors is more specific than the others. So, the Java compiler doesn't know which constructor to choose and will give you an error.

4) Wrapper Class

public class StringEquals {
         
          private Integer integer;
          public static void main(String[] args) {
                   StringEquals st = new StringEquals();
                   System.out.println(st.getInteger());
          }

          public int getInteger() {
                   return integer;
          }
}


Output :

Exception in thread "main" java.lang.NullPointerException

          at com.tutorials.string.StringEquals.getInteger(StringEquals.java:12)

No comments:

Post a Comment