• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

About null ...

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
can anybody explain this pls.

the out put is:
String Version
Pinky
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler asks whether method(String s) is more specific than method(Object o), meaning whether any invocation handled by the first method could be passed on (up the hierarchy) to the other one without a compile-time type error.
As the answer is "yes" in this case, then method(String s) is invoked.
[ April 23, 2003: Message edited by: Roger Chung-Wee ]
 
pinky yadav
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
does that mean here null is treated as string???
ck out this code

this is giving compile time error
reference to method is ambiguous, both method method(java.lang.StringBuffer) in AQuestion and method method(java.lang.String) in AQuestion match
Pinky
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No coz if you look closely the StringBuffer class does not extend the String class.
In simple terms and the eaiset way to see such codes is assume that both method had different names
say m1(StringBuffer) and m2(String)
now will this compile
m1(new String()) or m2 (new StringBuffer())
if the answer is yes that both the above will compile
then the call with a null will compile too.
E.x
assume m1(Object o) and m2 (String s)
m1(new Object()) or m2(new String())
as you will say that the above will compile and so will the call with null when both methods are same name
(This is a odd approach but I find it could be used)
Lawrence
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roger is correct.


The compiler asks whether method(String s) is more specific than method(Object o)


When it comes to choosing between an overloaded method that accepts a String object and one that accepts a StringBuffer object, the compiler will not be able to determine which method is the more specific when null is passed, because String and StringBuffer are not in the same class hierarchy. Hence the compile error.
String and Object are in the same hierarchy. A String is-a Object. Thus, in your first code posting, the overloaded method accepting a String is chosen when null is passed because String is more specific than Object.
 
pinky yadav
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the help.
I think i got the concept let me put it in this way.
When we call a method with parmeter null for example aMethod(null);
it looks for a implementation accepting a object ref. Now if this method is overloaded and there are two methods accepting object refs then it checks for the hierarchy
for example if aMethod(String) and aMethod(Integer) are present, as they are not in same hierarchy it will give compile time error saying ambiguous call.
But if two methods are present which are in a hierarchy say aMethod(String) and aMethod(Object) then the compilation passes and more specific method is called. In out case method accepting String.
Is this explaination correct? Please correct me if not.
Pinky
 
Rory French
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup Pinky ! That's exactly how I understand it.
 
pinky yadav
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot
Pinky
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Originally posted by Roger Chung-Wee:
The compiler asks whether method(String s) is more specific than method(Object o), meaning whether any invocation handled by the first method could be passed on (up the hierarchy) to the other one without a compile-time type error.
[ April 23, 2003: Message edited by: Roger Chung-Wee ]



I am confused here "invocation handled by the first method" What do u mean by first method.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The details are spelt out here:
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#272863
As this is pretty hard to read, I take the simple approach that you look first at the method at the bottom of the hierarchy. If this first method is valid, and the other methods up the hierarchy are also valid, then you choose the first method.
However, you need to be careful. What do you think will happen if you try and compile and run this.

[ April 24, 2003: Message edited by: Roger Chung-Wee ]
 
pinky yadav
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but 2 is int then how come the compiler is giving error.
Unless specified as 2L it will be considered integer.
Am i correct on this??
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right. If it's 2L, it compiles and outputs "In Child".
There are people who can explain this better than I, but the intuitive answer is that the compiler can find an immediate, exact match by looking first at the get method in Child. However, invoking get with an int gives the compiler a problem as neither version of the get method is more specific than the other (by going up the hierarchy), so the compilation fails.
[ April 24, 2003: Message edited by: Roger Chung-Wee ]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The most specific method is not chosen based only on the types of the arguments. Also the class declaring the methods counts.
In plain English:
A) Parent.get(int) one point because int is equal or widening convertible to long, but minus one point coz Parent is not the same or widening convertible to Child.
B) Child.get(long) one point coz Child is widening convertible to Parent, but minus one point coz long is not the same or widening convertible to int.
Now there is a draw! neither of the methods is more specific than the other, and thus the compiler yields the error.
For a more precise language you can consult the JLS.
 
pinky yadav
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey jose
you have given a very important point here.
I think i got it.
for testing when i put both the methods in same class its not giving error but calling proper method.
Also if i interchange the methods in the parent and child classes ie long goes parent and int comes in child, its working fine.
Thanks i think that was a good explaination
Pinky
 
Eliminate 95% of the weeds in your lawn by mowing 3 inches or higher. Then plant tiny ads:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic