• Post Reply Bookmark Topic Watch Topic
  • New Topic

overloaded methods  RSS feed

 
Chadd Franck
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, this is from Sun Certified Programmer for Java 6 Study Guide pg 219.
the answer is -434.

How come when swiffer(aa) is run, a 4 is produced instead of a 1?

How come swiffer(ba) gives a 3?

The book says swiffer(7); an int can be boxed to an Integer and then "widened" to an Object.

15. Given:


What is the result?
A. -124
B. -134
C. -424
D. -434
E. -444
F. Compilation fails


Thanks for your Time:>
 
Aum Tao
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How come when swiffer(aa) is run, a 4 is produced instead of a 1?


Note that you are creating an array of A, but not initializing the individual elements of the array. Therefore, it is treated as an Object and 4 is displayed when

is executed.

Similarly, you are only creating one array object ba[], but not initializing the individual elements. Therefore,

is executed and 3 is displayed.

Finally, as you mentioned, sifter(7) gets boxed to an Integer and widened to an Object to display 4.

Also, I would like to add that in order for this example to behave the way you had expected, you would have to do something like

[ December 10, 2008: Message edited by: Prateek Parekh ]
 
Chadd Franck
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If

uses

and NOT

How come

doesn't uses

Like the A[] would? when run with


As far as I cann tell, both are arrays that have not been initalized, why would one count as an object but the other one work as an array?
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Prateek Parekh:


Note that you are creating an array of A, but not initializing the individual elements of the array. Therefore, it is treated as an Object and 4 is displayed when

is executed.

Similarly, you are only creating one array object ba[], but not initializing the individual elements. Therefore,

is executed and 3 is displayed.

Finally, as you mentioned, sifter(7) gets boxed to an Integer and widened to an Object to display 4.

Also, I would like to add that in order for this example to behave the way you had expected, you would have to do something like


[ December 10, 2008: Message edited by: Prateek Parekh ]


I think that initializing will not alter the output. You can run the following code and see the output.




After running this I am getting the same output.




The concept is when method sifter(aa); is called then method static void sifter(Object o) { s += "4"; } is invoked. The reason is that var-agrs were introduced in java 1.5 . So to maintain the code written by thousands of programmers it is necessary to maintain backward compatibility otherwise it will break everyone's code. So the functions taking varargs are called at the last when no suitable match is found.

[ December 10, 2008: Message edited by: Himanshu Gupta ]
[ December 10, 2008: Message edited by: Himanshu Gupta ]
 
Chadd Franck
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The concept is when method sifter(aa); is called then method static void sifter(Object o) { s += "4"; } is invoked. The reason is that var-agrs were introduced in java 1.5... So the functions taking varargs are called at the last when no suitable match is found.

I understand that var args will be chosen last. but what about ba.

ba and aa were declared in the exact same manner and neither were intialized.

I don't understand why both aa and ba are not objects and why they both don't invoke the method sifter(Object o) { s += "4");?

ba is declared and not initialized exactly like aa. So why is it not an object too???

Sorry for the confusion
[ December 10, 2008: Message edited by: Chadd Franck ]
 
Chadd Franck
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Holy crap I get it now, it doesn't matter wether the arrays are initalized or not, since aa is an array and an object, the object argument will be chosen over the (A[]... a2). However, ba's array call isn't a var-arg so,
(B[] b1) is called and not the object.

I am guessing this is because the most specific argument is attempted first.

Thank you both for helping

Chadd
[ December 10, 2008: Message edited by: Chadd Franck ]
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chadd Franck:
Holy crap . . .
I am guessing this is because the most specific argument is attempted first.


Please . . . this is a family website.

Surely if the most specific argument is attempted first, the Object method will be tried last?

But Himanshu Gupta has given the explanation; if you have a method which takes 3 arguments and a method which takes ... then if you pass 3 arguments the JVM will go for the method with the 3 rather than the ... . Since you are passing 1 argument, the JVM will prefer the method taking 1 parameter over the method taking ... .
 
Chadd Franck
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two things:

1.


Surely if the most specific argument is attempted first, the Object method will be tried last?


I guess it's all in how you look at it, to me, an object would be the least specific item becase anything that extends the object defines it further and makes the object more specific. Therefore, in the way I was thinking when I typed it, (B[] ba) would be more specific then an (Object o).

2.

quote:
--------------------------------------------------------------------------------
Originally posted by Chadd Franck:
Holy crap . . .
I am guessing this is because the most specific argument is attempted first.
--------------------------------------------------------------------------------

Please . . . this is a family website.


I wan't aware crap was a bad word.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1: That's what I thought first, but when I worked out that it would compile with the ... in I realised what was happening. Himanshu Gupta got there first with his explanation.
2: I may be feeling very sensitive tonight, but remember that postings are read by all sorts of people, some with thick skins and some with thin. So it is as well to be careful about that sort of thing. I said "family website" because I remember Gunther saying something similar in "Friends," but it's probably best not say anything in postings which would cause embarrassment even to the smallest child.
 
Chadd Franck
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help again, You all are great.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Chadd Franck
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also to the specific/general issue I just found this on the SCJP faq on this board

http://faq.javaranch.com/java/ScjpFaq#scoresImportant
(near the bottom)


What is a most-specific method?

Suppose that more than one version of an overloaded method applies to a particular call. For example, suppose that Cat extends Animal, and a method is overloaded to take either of these types as an argument.

void method(Animal animal) {...}
void method(Cat cat) {...}

Further, suppose that "milton" references an instance of Cat. That is...

Cat milton = new Cat();

Now, since a Cat is also an Animal, which method will be invoked with the following call?

method(milton);

In this situation, Java invokes the "most-specific" method, as detailed in section 15.12.2.2 of the Java Language Specification. In general, one method is considered more specific than another if its argument types are subtypes of the other method's respective argument types. So because Cat is a subtype of Animal, the method overloaded to accept a Cat is more specific than the method overloaded to accept an Animal. Therefore, method(Cat cat) will be invoked by the call method(milton).

If it's not possible to identify a most-specific method from among the applicable methods, then the invocation is considered "ambiguous" and results in a compile-time error.

Note, however, that a null reference could apply to any object type. Therefore, the call method(null) would also invoke the most specific method -- in this case, method(Cat cat).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!