Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Boxing null literal Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Boxing null literal" Watch "Boxing null literal" New topic
Author

Boxing null literal

twoc andles
Greenhorn

Joined: Jun 22, 2010
Posts: 4
Hi, i'm new here and i'm preparing for the scjp.

I've come across a question I don't find a reasonable answer.

Take a look at the following code:


This code don't compile. It says null is an ambiguous reference, but if one of the m1 methods is removed, then the code is fine.

Now, if I change the m1 methods for these:


It seems the null literal works as a "wild" reference and that's what I don't understand.
Nisha Ganeriwal
Greenhorn

Joined: Feb 25, 2010
Posts: 8

String and Integer classes are on the same level unlike String and Object where Object is a superclass. Thus null is casted to String in the second case preferring the subclass.
twoc andles
Greenhorn

Joined: Jun 22, 2010
Posts: 4
So null is always casted to the "more specific class that can be matched". If there's more than 1 class not belonging to the same hierarchy then an "ambiguous reference" compiler error is notified. Is that so?

Thanks for the answer!

btw, not a "logical" behaviour, is it?
Nisha Ganeriwal
Greenhorn

Joined: Feb 25, 2010
Posts: 8

Any class is casted to the
more specific class that can be matched
. null is just a specific value that the class variable could have.
twoc andles
Greenhorn

Joined: Jun 22, 2010
Posts: 4
Nisha Ganeriwal wrote:Any class is casted to the
more specific class that can be matched
. null is just a specific value that the class variable could have.


but why the String takes preference over Object? or in general, why a specific class takes precedence over a class up in the hierarchy tree?
Himanshu Mall
Greenhorn

Joined: Jun 28, 2010
Posts: 22
@twoc andles

"NULL" has nothing to do with it.

We are talking about "Method Overload Resolution" here.

1) Most important thing to remember is that "Method Overload Resolution" happens at compile time i.e. Compiler does it.

2) Compiler resolves the method to be called in three phases mentioned below:

a) Accessibility: Whether the methods in question are accessible at the first place or not?
For example:



In the example above, print(Integer i) is not accessible to MethodOverloadDemo at the first place(Its a PRIVATE thing!), hence it is out of consideration. Result: print(String) is being called

B) After accessibility, compiler chooses the applicable methods.
A method is applicable if:
It number of argument matches.
Method can accept the arguments.

Example:


Now both String and Integer references can refer to null (to nothing)
Integer i = null;
String s = null;

At this point Compiler has two methods, both accessible and applicable, so it performs the third step: It chooses the MOST SPECIFIC METHOD

What is MOST SPECIFIC METHOD: A method is less specific if it can take all arguments passed successfully to the other method.

Lets take the above mentioned example with this:

1) Can a reference of type Integer refer to String object? Answer is NO: One cannot say Integer i = "Himanshu";
2) Can a reference of type String refer to an Integer object? Answer is NO: One cannot say String s = 1;

Compiler has done with all three steps, still it cannot choose one method. So its an ambiguity.

Lets take other example:



Now lets apply all three steps and see what happens:
1) Both are accessible
2) Both are applicable i.e. It is legal to say Object o = null; and String s = null;
3) Now third (choose most specific)
Can we say:
String s = new Object(); NO we cannot do that, pretty obviously
Can we say
Object o = new String("Himanshu"); YES, again obviously

In other words, print(Object o) can take all the arguments that can be passed to print(String s), however reverse is not true.

Now we, just like the compiler, can say print(Object o) is less specific and print(String s) is most/more specific and hence it wins.

Note: Apply the same rule irrespective of the whether actual argument is null or some other value.



Hope it helps.
Himanshu Mall


Regards
Himanshu Mall
@mall.himanshu84@gmail.com
twoc andles
Greenhorn

Joined: Jun 22, 2010
Posts: 4
Thank you very much, Himanshu. Much clearer now.
Unmesh Chowdhury
Ranch Hand

Joined: Jun 20, 2010
Posts: 45
Absolutely helpful.


M.Sc. in CS, OCPJP6 93%
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

@Himanshu ... nice explanation.


http://plainoldjavaobject.blogspot.in
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Boxing null literal