• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method Invocation..  RSS feed

 
Smitha Ballikar
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Answer is 1 not 2..Why??
Here why Int version of method is executed since we are passing a character and also a method with character version is present.
 
P Hunjan
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Smitha,

I guess the second method's parameter java.lang.Character is an object(Wrapper Class), therefore when you pass 'b' it converts it into 'int' & calls the first method.

Thanks
 
Pulamathi Pavan Kumar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Smitha,

In the 2nd method u are using the Wrapper class reference as an argument.
In the main u are invoking the method with a primitive type which is within range of integer. So, 1st method is invoked and hence the answer.

Pavan.
 
Hamid Virani
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In java 1.5, do you think there will be any change in o/p because of autoboxing?
 
Rushikesh Wagh
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First Of All Concentrate On This Constructor.

void iWanna(java.lang.Character c) {
System.out.println("Char version");
}

It Has Object As Parameter (Object Of Character Wrapper Class). Not The char(Premitive).

Here char gets upcasted To int. Hence Output Is: 1.int version
 
Smitha Ballikar
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Thanks!! I got it now.I think I didnt realise it was wrapper object (thought it was char).May be I need to read questions more carefully!!



Cheers
Smitha
 
Joshua Smith
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hamid-

You asked:
In java 1.5, do you think there will be any change in o/p because of autoboxing?

When I first looked at the question that is what was going through my mind as well. With auto-boxing Character seems closer to char than int, so why not auto-wrap it in a Character wrapper and everything will be good. The problem is that it would break backward compatability. It wouldn't be very good if a char auto-promoted to int in Java 1.4 and to a Character in Java 5. The same code would have different behavior depending on the version of the JVM. Not good for write once, run anywhere.

Josh
 
Ryan Kade
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joshua,

Actually that's exactly what happens. The unboxed variable will first look up the conversion tree and, barring any successful match, then look for its boxed equivalent. Yes, this does affect the runtime environment (the program would function differently under JRE 1.4 than JRE 1.5), but that's the case with any version upgrade.

You can read more about boxing conversions (and the types of conversions they apply to, e.g., assignment, method invocation, etc.) in the JLS.

Here are some examples of how it works, too (note you must be using JDK/JRE 1.5 to compile/run this code):



The output of this code is:

0
1
2
3
4
5
6
7
8
9
10
11
printInt(Byte): 12
printInt(Short): 13
printInt(Integer): 14

Notice how if you uncomment the printInt(int) function, all three unboxed variables (r, s, and t) will go back to being promoted to int, just like you'd expect.

EDIT: Fixing a few typos.
[ August 25, 2005: Message edited by: Ryan Kade ]
 
Hamid Virani
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Hi tried to compile and run this program with jdk 1.5

class BoxTest
{
public void callMe(Character c) {
System.out.println("in character version");
}

public void callMe(int i) {
System.out.println("in int version");
}

public static void main(String[] args) {
char chrTest = 'c';
BoxTest b = new BoxTest();
b.callMe(chrTest);
}
}

And the output i got was "in int version".

I think this happens, because whenever the parameter of type short, byte or char is passed, it is autopromoted to int, and so it finds a perfect match before it has to autobox it and so prints "in int version".

Please let me know if my resoning for above is correct or I am missing something.

Regards,
Hamid
 
Ryan Kade
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hamid,

That is correct. The compiler will try to promote the primitives byte, short, and char to int before trying to autobox them.
 
Joshua Smith
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan-

Thanks for your example. I don't think we're saying anything different. Your last post makes it clearest.

"The compiler will try to promote the primitives byte, short, and char to int before trying to autobox them."

This ensures that if we had a method taking an int and a method taking a Character, called with a char, that we will get the same results in Java 1.4 as in Java 5.

Of course, if the int method weren't present, we would get different results due to auto-boxing. In 1.4 it wouldn't be able to find an acceptable method and in 1.5 it would find the method with the Character parameter.

Did I summarize that correctly? There's a lot of forces working here that need to be kept straight.

Josh
 
Ryan Kade
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joshua,

I may have misunderstood what you were originally saying. Looks like we're on the same page.
 
Akshay Kiran
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A very erudite discussion on meethod overloading can be found on java.boot.by a tiger guide by MZ.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!