Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Boxing null literal

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 8
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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

 
twoc andles
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much, Himanshu. Much clearer now.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Absolutely helpful.
 
Ranch Hand
Posts: 384
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Himanshu ... nice explanation.
 
Onion rings are vegetable donuts. Taste this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic