Sun Certified Programmer For Java 6

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, July 5, 2012

37 - to clone or not to clone

Try to understand the following :

package Object;

public class Customer implements Cloneable{

    private String firstName = "firstName";
    private String lastName = "lastName" ;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    Customer(String firstName, String lastName) {
        this.firstName  = new String(firstName);
        this.lastName = new String(lastName);
    }

    public Object getShallowCopy() throws CloneNotSupportedException{
        return this.clone();
    }

    public Object getDeepCopy() throws CloneNotSupportedException{
        return new Customer(this.firstName, this.lastName);
    }
}

package Object;

import org.junit.Test;
import static org.junit.Assert.*;

public class CustomerTest {

    @Test
    public void testClone() throws Exception {
        Customer customer = new Customer("rudy", "vissers");
        Customer clonedCustomer = (Customer)customer.getShallowCopy();
        assertTrue(customer != clonedCustomer);
        assertTrue(customer.getFirstName() == clonedCustomer.getFirstName());
        assertTrue(customer.getLastName() == clonedCustomer.getLastName());
        assertTrue(customer.getFirstName().equals(clonedCustomer.getFirstName()));
        assertTrue(customer.getLastName().equals(clonedCustomer.getLastName()));
    }

    @Test
    public void testDeepCopy() throws Exception {
        Customer customer = new Customer("rudy", "vissers");
        Customer deepCopy = (Customer)customer.getDeepCopy();
        assertTrue(customer != deepCopy);
        assertTrue(customer.getFirstName() != deepCopy.getFirstName());
        assertTrue(customer.getLastName() != deepCopy.getLastName());
        assertTrue(customer.getFirstName().equals(deepCopy.getFirstName()));
        assertTrue(customer.getLastName().equals(deepCopy.getLastName()));
    }

}

That's all folks !

Saturday, March 27, 2010

36 - kick butt and have fun !

kick butt and have fun ! :-) 

 Until the next certification (Java 7 or 8) I continue the story on Java Easy !

35 - up to the next ! :-)

Monday, March 1, 2010

34 - What is printed ?

To relax you a little please make the following exercice and guess what is going to be printed ?


package ex1;

public class Main {

    String hello = "hello";

    {
        System.out.println(this.hello);
        System.out.println("block1");
    }

    public static void main(String[] args) {

        new Main().fool();

    }

    Main() {
        System.out.println("Main");
    }

    void fool() {
        System.out.println("fool");
    }

    {
        System.out.println("block2");
    }
}

==========
































run:
hello
block1
block2
Main
fool

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;
    }
}

Followers