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

Method Invocation

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found this question in abhilash.

eg 1:

public class AQuestion {
public void method(Object o) {
System.out.println("Object Verion");
}
public void method(String s) {
System.out.println("String Version");
}
public static void main(String args[]) {
AQuestion question = new AQuestion();
question.method(null);
}
}

Answer: String Version

eg 2:

public class AQuestion {
public void method(StringBuffer sb) {
System.out.println("Object Verion");
}
public void method(String s) {
System.out.println("String Version");
}
public static void main(String args[]) {
AQuestion question = new AQuestion();
question.method("");
}
}

Answer: String Version

Can anyone explain me, how this selects the particular method..Please
 
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sangeetha ,

Well for the 2nd eg. let me clear one point that:

public class AQuestion {
public void method(StringBuffer sb) {
System.out.println("Object Verion");
}
public void method(String s) {
System.out.println("String Version");
}
public static void main(String args[]) {
AQuestion question = new AQuestion();
question.method("");
}
}

The line question.method("") Creates a String object and not a StringBuffer hence the method with the parameter of String is called.

Sandy
[ September 14, 2005: Message edited by: Sandeep Chhabra ]
 
Ranch Hand
Posts: 579
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
eg 1:

public class AQuestion {
public void method(Object o) {
System.out.println("Object Verion");
}
public void method(String s) {
System.out.println("String Version");
}
public static void main(String args[]) {
AQuestion question = new AQuestion();
question.method(null);
}
}

Answer: String Version

eg 2:

public class AQuestion {
public void method(StringBuffer sb) {
System.out.println("Object Verion");
}
public void method(String s) {
System.out.println("String Version");
}
public static void main(String args[]) {
AQuestion question = new AQuestion();
question.method("");
}
}

Answer: String Version





In eg1,
call to method (question.method(null) ) is with null.Now More Specific Version Of Overloaded Function 'll B Chosen.
So String Version Is Chosen.

For eg2,
i'm Agree with Mr. Sandeep



Agrah Upadhyay
B.tech
3rd Year
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I disagree, passing a null value to an overloaded object which is expecting either an StringBuffer or a String will cause a compile error because null is ambiguous. The value null is not an object of either String or StringBuffer it not an object of any type. There is no way the compiler can determine the correct method to call.
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I disagree, passing a null value to an overloaded object which is expecting either an StringBuffer or a String will cause a compile error because null is ambiguous. The value null is not an object of either String or StringBuffer it not an object of any type. There is no way the compiler can determine the correct method to call.



Sorry Mr. Thomas Drew

The First eg will surely compile and run..
and will print "String Version".

You should have tried the code before making any comment.

I request you to please reconsider.

Sandy
 
Thomas Drew
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The version will not run because you are passing a null value. There is no way for the compiler to determine which method to call. Yes, I tried to pass an null value to a method expecting an a String object or a StringBuffer. I received a compile error. Have you typed in the code and tried to run it ?
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when having String and Stringbuffer compiler surely throws error

error displayed is

AQuestion.java:10: reference to method is ambiguous, both method method(java.lang.StringBuffer) in AQuestion and method method(java.lang.String) in AQuestion match question.method(null);

i hope it differs from version to version..but not sure

i am using jdk1.4
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sir,
When i tried the above mentioned code. it compiled as well as run printing String Version.
also the Q No.21 in Practice Exam 2 of Whizlabs Simulator for SCJP1.4 shows the corret answer to be String.

If there is difference between the output from Compiler to Compiler then thats a different case.

Sandy
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What i Know is that there should not be anything like compiler differnce ,otherwise there would be a big fight all over that someones compiler is giving some results and other are giving other,otherwise from my side it is simply that my compiler gives me

String Version
 
Thomas Drew
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sandeep , Why do think an String object and the value null are equal.

null is a value which represents nothing. It is not an object of any type. Why do you thank that null would be equal to a String. You can assign null to any object regardless of the object type. So why would a String be a better choice than a StringBuffer?
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Thomas Drew

Sir, i know that null can be assigned to ANY object. I know null does not represent anything. nor it is an object of any type. and i never said that null would be equal to String.

and i would have not thought so that String be a better choice than a String for null if my compiler would not have resulted in that output.

Although i dont know why does it calles the Strng Method and not StringBuffer method. but still i know one thing that it does compiles and also prints String Version when i compile and run it in JDK1.5

I myself am expecting some explination of it.

Sandy
 
sangeetha balasubramaniam
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Thomas,

both eg1 and eg2 definetely complies for sure.Becasue i compiled and got those answers.

In eg2 , when you change the line "question.method(null);" , definetely for sure it will give the compiler error as ambiguous.So i doubt you did that.Can you post the code which you tired..Please..
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
when methods are overloaded, the most specific method will be the one thats chosen from all the "applicable" methods.

The most specific method is the method in which - 'all' its arguments can be assigned to the argument types of the other method .


for example between methods
m1(Object){System.out.println("Object");}
m1(String){System.out.println("String");}

for the invocation m1(null)- null being the default value for both reference types - m1(String) would be the most specific method because String can be assigned to an Object and not vice versa.

similarly in case of primitives
m1(long) m1(float)

if it was invoked with m1(10)- which is an int - the method chosen would be
m1(long) because a long can be assigned to a float but a float cant be so assigned unless u cast it.

By applicable methods i mean in case of m1(1.0) m1(long) isnt applicable anyway. so there can be no ambiguity.

So decide what methods are applicable to the invocation and then choose the most specific method.

Though this explanation is not in itself technically complete for further reference It would be nice if you can read the Java Language Specification 2nd edition for further clarification regarding "method resolution" process. chapter 15 i believe.
there is also Java Language Specification 3.0 which deals with 5.0 but with generics involved the explanation given for method resolution gets very complicated.

As far as the second ones, StringBuffer wont be called for the reason you cant assign a String literal to StringBuffer.

StringBuffer s = "abcd"; --> is a compile time error. and StringBuffer objects are not implicitly created from String literals as is the case with String Objects.

Hope this helps... Pls do suggest any corrections

[ September 14, 2005: Message edited by: sankar velamuri ]
[ September 14, 2005: Message edited by: sankar velamuri ]
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hope this below mentioned URL Helps..

According to JLS , the most specific method is chosen..

Java Language Specification
15.12.2.2 Choose the Most Specific Method
 
anand phulwani
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perfect Example To Conclude ,
This will always call the class derived class,
which is at the bottom of the hiearachy



Results into:

null Called Sub
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic