Posts

SCJP 6 Questions: Pass by reference with example

Image
See the code: class GFC304 { static void m1(int[] i1, int[] i2) { int[] i3 = i1; i1 = i2; i2 = i3; } public static void main (String[] args) { int[] i1 = {1}, i2 = {3}; m1(i1, i2); System.out.print(i1[0] + "," + i2[0]); }} What will be the output? Answer: 1,3 Explanation: Here we are passing the copy of reference variables  i1 and i2 from main() method to method m1(). Now m1 has it's own  local reference variables i1 and i2 referring to same array as main() method's i1 and i2. So you can see that we are only passing the  reference variables not exactly the value they are refering. See this figure... Now see the second code....... class GFC305 { static void m1(int[] i1, int[] i2) { int i = i1[0]; i1[0] = i2[0]; i2[0] = i; } public static void main (String[] args) { int[] i1 = {1}, i2 = {3}; m1(i1, i2); System.out.print(i1[0] + "," + i2[0]); }} What will be the output? Answer: 3,1 Exp...

SCJP 6 Questions: Widening, autoboxing and varargs in overloading

The code is given: 3.  public class Bertha { 4.    static String s = ""; 5.    public static void main(String[] args) { 6.        int x = 4; Boolean y = true; short[] sa = {1,2,3}; 7.        doStuff(x, y); 8.        doStuff(x); 9.        doStuff(sa, sa); 10.       System.out.println(s); 11.   } 12.   static void doStuff(Object o) { s += "1"; } 13.   static void doStuff(Object... o) { s += "2"; } 14.   static void doStuff(Integer... i) { s += "3"; } 15.   static void doStuff(Long L) { s += "4"; } 16. } What is the result? A. 212 B. 232 C. 234 D. 312 E. 332 F. 334 G. Compilation fails Answer:  A is correct.  Explanation: It's legal to autobox and then widen. The first call to doStuff(), x is boxed from int to an Integer then passes these two objects (Integer,Boolean) to varargs (Object.... 0). The second...

Overloading with widening, boxing and Varargs

Overloading with Boxing and Var-args class AddBoxing {   static void go(Integer x) { System.out.println("Integer"); }     public static void main(String [] args) {      int i = 5;      go(i);    } } Answer: Integer class AddBoxing {    static void go(long x) { System.out.println("long"); }   public static void main(String [] args) {      int i = 5;      go(i);    } } Answer: long Now see... class AddBoxing {   static void go(Integer x) { System.out.println("Integer"); }   static void go(long x) { System.out.println("long"); }   public static void main(String [] args) {      int i = 5;      go(i); // which go() will be invoked?   } } If it is given that both methods exist, which one will be used? In other words, does the compiler think that widening a primitive parameter is more desirable than...

SCJP 6 Questions: Garbage collection

Image
Qu1. See the code ... 3.   class Dozens { 4.      int[] dz = {1,2,3,4,5,6,7,8,9,10,11,12}; 5.   } 6.   public class Eggs { 7.         public static void main(String[] args) { 8.               Dozens [] da = new Dozens[3]; 9.               da[0] = new Dozens(); 10.             Dozens d = new Dozens(); 11.             da[1] = d; 12.             d = null; 13.             da[1] = null; 14.            // more code 15.       } 16.  } Which two are true about the objects created within main(), and eligible for garbage collection when line 14 is reached? A. Three objects were created B. Four objects were created C. Five objects were created D. Zero objects are ...

Interview Question @ IBM: Write your own generic class

Qu1: Can you write a small generic class by yourself ? Answer: This could be like this...... public class GenericClassDemo<X, Y> {    X name;    Y age;    GenericClassDemo(X name, Y age) {       this.name = name;       this.age = age;    }    X getX() { return name; }    Y getY() { return age; }    // test it by creating it with <String, Integer>   public static void main (String[] args) {        GenericClassDemo<String, Integer> obj=                       new GenericClassDemo<String, Integer>("Rajesh Kumar", 15);        String empName = obj.getX();    // returns a String        int empAge = obj.getY();    // returns Integer, unboxes to int        System.out.print...

Ploymorphism with Generics in Java 5 or later

Ploymorphism with Generics Generic collections give us sane advantages of type safety that we have always had with arrays, but there are some crucial differences Most of these have to do with polymorphism. You've already seen that polymorphism applies to the "base" type of the collection: List<Integer> myList = new ArrayList<Integer>(); In other words, we were able to assign an ArrayList to a List reference, because List is a supertype of ArrayList. But what about this? class Parent { } class Child extends Parent { } List<Parent> myList = new ArrayList<Child>(); No, it will not work. Rule : The type of the variable declaration must match the type you pass to the actual object type. If you declare List<Parent> parentList then whatever you assign to the foo reference MUST be of the generic type <Parent>. Not a subtype of <Parent>. Not a supertype of <Parent>. Just <Parent>. Wrong: List<Obj...

Android 4 ice cream

Image
See latest from Android...