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





5 comments:

  1. *** ATTENTION ***
    a Short can also become a Number !!!
    Short--->Number
    I forgot this case...
    I will update this page very soon...

    ReplyDelete
  2. When are you planning to update Short -> Number case?

    ReplyDelete
    Replies
    1. Sorry I was rather busy the last 2 years... Sorry again !
      I will read all the blog and based on the experience I have gained the last 2 years I will make something better...

      Delete
  3. Hello,

    you should correct AB1, they both print "Short"

    ReplyDelete
  4. It is now corrected. Thanks a lot !

    ReplyDelete

Followers