This is some information on the Java 6 certification that I prepare. This information is not very well structured and it is most of the time an answer to the questions that come when I read the SCJP 6 book (Bates and Sierra). *** AS OF 5 OF MARCH 2010, I AM SCJP 6.0 ***

Thursday, February 25, 2010

33 - AutoBoxing rules

It is my rules and please drop me an email if you read something incorrect !


Let's say you have several methods with the same name but different parameters(overloaded methods).


You pass a "primitive wrapper object" OR a primitive.


What is chosen ? and Why ?


Diagram :



A) a Short 

can become a Short or an Object (is an Object)


WILL NOT BECOME an Integer, a Long, a Float, a Double


Short is Short OR Object STOP


OR can become a short (a primitive)
look below

B) a short (primitive)

can become (what is short compatible) a short, a int, a long, a float, a double (I mean without a cast) STOP

OR

a Short (primitive wrapper)
Look now the way a Short is evaluated above.

I have to add a rule, var-arg parameters are ALWAYS - ALWAYS - ALWAYS :-) chosen LAST ! Look at example AB2 (AutoBoxing2).
I like the style of Kathy :-)


Examples

Please find the correct answers :


package autoboxing;

public class AB1 {

    static String method1(Short param) {
        return "Short";
    }
    static String method1(short param) {
        return "short";
    }

    public static void main(String[] args) {
        Short x = -32768;

        System.out.println(method1(x));
    }
}


package autoboxing;

public class AB2 {

    static String method1(Short... param) {
        return "Short...";
    }

    static String method1(Object param) {
        return "Object";
    }

    public static void main(String[] args) {
        Short x = -32768;

        System.out.println(method1(x));
    }
}

package autoboxing;

public class AB31 {

    static String method1(short param) {
        return "short";
    }

    static String method1(Object param) {
        return "Object";
    }

    public static void main(String[] args) {
        Short x = -32768;

        System.out.println(method1(x));
    }
}

package autoboxing;

public class AB32 {

    static String method1(short param) {
        return "short";
    }

    static String method1(double param) {
        return "double";
    }

    public static void main(String[] args) {
        Short x = -32768;

        System.out.println(method1(x));
    }
}

package autoboxing;

public class AB33 {

    static String method1(double param) {
        return "double";
    }

    static String method1(int param) {
        return "int";
    }

    public static void main(String[] args) {
        Short x = -32768;

        System.out.println(method1(x));
    }
}

package autoboxing;

public class AB4 {

    static String method1(short param) {
        return "short";
    }

    static String method1(Short param) {
        return "Short";
    }

    public static void main(String[] args) {
        short x = -32768;

        System.out.println(method1(x));
    }
}

package autoboxing;

public class AB5 {

    static String method1(double param) {
        return "double";
    }

    static String method1(Short param) {
        return "Short";
    }

    public static void main(String[] args) {
        short x = -32768;

        System.out.println(method1(x));
    }
}


package autoboxing;

public class AB6 {
    static String method1(Double param) {
        return "Double";
    }

    public static void main(String[] args) {
        short x = -32768;
        System.out.println(method1(x));
    }
}

package autoboxing;

public class AB7 {

    static String method1(Short param) {
        return "Short";
    }

    static String method1(Object param) {
        return "Object";
    }

    public static void main(String[] args) {
        short x = -32768;
        System.out.println(method1(x));
    }
}

package autoboxing;

public class AB8 {

    static String method1(Integer param) {
        return "Integer";
    }

    static String method1(Object param) {
        return "Object";
    }

    public static void main(String[] args) {
        short x = -32768;
        System.out.println(method1(x));
    }
}


























Answers :
AB1:Short,AB2:Object,AB31:Object,AB32:short,AB33:int,AB4:short,AB5:double,AB6:does not compile,AB7:Short,AB8:Object





Friday, February 19, 2010

32 - arrays - Java 1.6

I find the following very fascinating...

package basic;

public class Main {

    public static void main(String[] args) {

        Object d = new Long[4];
        Object[] e = (Object[]) d;
        System.out.println("e[0] : " + e[0]);

        Long[] f = (Long[]) d;
        System.out.println("\nf[0] : " + f[0] + "\n");


        Long[] g[] = {{1L, 2L}, {1L}};
        for (int i = 0; i < g.length; i++)
            for (int j = 0; j < g[i].length; j++)
                System.out.println(g[i][j]);
       
        System.out.println("");
        String[] s = new String[4];
        int i=0;
        for(String s1:s)
            System.out.println(i++ + ":" + s1);

        System.out.println("");
        int []t[] = new int[5][6];
        for(int k=0; k < t.length; k++) {
            int m = 0;
            for(int l:t[k]) {
                System.out.println(m++ + ":" + l);
            }
            System.out.println("");
        }
    }
}

run:
e[0] : null

f[0] : null

1
2
1

0:null
1:null
2:null
3:null

0:0
1:0
2:0
3:0
4:0
5:0

0:0
1:0
2:0
3:0
4:0
5:0

0:0
1:0
2:0
3:0
4:0
5:0

0:0
1:0
2:0
3:0
4:0
5:0

0:0
1:0
2:0
3:0
4:0
5:0

BUILD SUCCESSFUL (total time: 0 seconds)

31 - Covariant Return - Java 1.6

They use several time a covariant return in the master exams... Be careful...

I find the explanations here interesting

Example :

1) Java 1.1 and after (this will compile) - It is NOT a covariant return !!!


package covariantreturn;

public class A {
    A method() {
        return this;
    }
}

package covariantreturn;

public class B extends A {
    A method() {
        return this;
    }
}

package covariantreturn;

public class C extends B {
      A method() {
        return this;
    }
}

2a) Java 1.5 and after (will not compile in Java < 1.5 !!!) - Covariant Return !
You return a subtype of A in B
You return a subtype of B in C (can be B) 


package covariantreturn;

public class A {
    A method() {
        return this;
    }

}

package covariantreturn;

public class B extends A {
    B method() {
        return this;
    }
}

package covariantreturn;

public class C extends B {
      B method() {
        return this;
    }
}

2b) 

You return a subtype of A in B
You return a subtype of B in C (can be C) 

package covariantreturn;

public class A {
    A method() {
        return this;
    }

}

package covariantreturn;

public class B extends A {
    B method() {
        return this;
    }
}

package covariantreturn;

public class C extends B {
      C method() {
        return this;
    }
}

Sunday, February 7, 2010

0 - ERRATA - MASTER EXAM 1

Question 68
H java -cp /mp/jars/mp.jar player.MusicPlayer
INSTEAD OF
H java -cp /mp/jars/cp.jar player.MusicPlayer

IT IS TOTALLY UNACCEPTABLE !!! GUYS READ YOUR STUFF AND PRODUCE AN ERRATA !!!


But your book is so good that I forgive you Kathy and Bert :-)

Saturday, February 6, 2010

30 - Treeset - Java 1.6.0_18

package javaapplication15;

import java.util.Set;
import java.util.TreeSet;

public class Main {

    public static void main(String[] args) {

        Set treeSet = new TreeSet();

        treeSet.add("2"); treeSet.add("4"); treeSet.add("6"); treeSet.add("8");

        System.out.println("first : " + ((TreeSet)treeSet).first());
        System.out.println("floor(6) : " + ((TreeSet)treeSet).floor("6"));
        System.out.println("higher(6) : " + ((TreeSet)treeSet).higher("6"));
        System.out.println("last : " + ((TreeSet)treeSet).last());
    }

}



first : 2
floor(6) : 6
higher(6) : 8
last : 8

29 - Serialization - Transient - Constructor - Java 1.6.0_18

package serialization2;

import java.io.Serializable;

public class A  {
    int a1 = -1;
    int a2 = -2;
    int a3 = -3;

    A() {
        System.out.println("in A()");
    }
}

package serialization2;

import java.io.Serializable;

public class A1 extends A implements Serializable {
    int a;
    transient int b;

    A1(int a, int b) {
        this.a = a;
        this.b = b;
    }

    public String toString() {
        String aString = Integer.toString(super.a1)
                + " " + Integer.toString(super.a2)
                + " " + Integer.toString(super.a3)
                + " " + Integer.toString(a)
                + " " + Integer.toString(b);
        return aString;
    }
}


package serialization2;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.imageio.stream.FileImageInputStream;

public class Main {
    public static void main(String[] args) throws Exception {
        A1 refA1 = new A1(-1,-2);
        System.out.println("refA1 : " + refA1);
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("A1.ser"));
        oos.writeObject(refA1);

        System.out.println("reading it back");

        refA1 =(A1)new ObjectInputStream(new FileInputStream("A1.ser")).readObject();
        System.out.println("refA1 : " + refA1);
        
    }
}

If A is not serializable 

run:
in A()
refA1 : -1 -2 -3 -1 -2
reading it back
in A()
refA1 : -1 -2 -3 -1 0

The constructor was called. b is transient and assigned 0.

If a is serializable

run:
in A()
refA1 : -1 -2 -3 -1 -2
reading it back
refA1 : -1 -2 -3 -1 0

The constructor was NOT called. b is still transient and still assigned 0.

Followers