Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method Resolution

 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why following code allowed



But Not This , It gives warning as : non-varargs call to varargs method with inexact argument type for last signature






 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler is alerting you. Its telling you that if you want to call the caller(Object ll) method cast the argument to Object or cast it to Object [] if you want to invoke the var-args method. The default invocation is the var-args method with no explicit casting.
 
Rajanand Pandaraparambil Kuttappan
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If default invocation always to var-args one, then that does not happen if we pass an Integer instead of an Integer array, like given below:-




Here it prints "Object".

 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because it is an object but not object array.

Even if you replace Integer pp =1 with int [] pp = {1}; The call goes to caller(Object ll). Because in Java arrays are objects
 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks harsha smith.
 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Above code prints Object.

But how int is converted to Object ?
Is that what happens : int-->Integer--->Object
 
Rajanand Pandaraparambil Kuttappan
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because in Java arrays are objects


thats exactly what I am talking about. an Integer widens to Object and resolve to the method with Object argument. An int auto-boxes into Integer, then widens to Object, again find the method with Object argument. In case of an Integer[], it again need to be widened to an Object. Why, only in this case, it prefers the var-arg method over the one with just Object argument ?
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Widening beats var-args"

But incase of Integer [], Object...objects is more appropriate than Object(as it is an object of objects)
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajanand Pandaraparambil Kuttappan wrote:Integer widens to Object


Integer does not "widen" to Object. Integer IS-A Object.
 
Stephan van Hulst
Bartender
Pie
Posts: 5553
53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, the JLS uses the phrase "widening" to refer to (implicitly) casting a reference to a supertype.

So while it's correct to say that "an instance of" Integer "is-an instance of" Object; it's not correct to say that Integer "is-an" Object. After all, Integer and Object are distinct types.
 
Rajanand Pandaraparambil Kuttappan
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I meant to "cast-to-a-super-type" by widening. So Harsha Smith's point seems to be logical that it calls var-args form in case of Integer[], since it is an Object of Objects.

I tried with int[] instead of Integer[], which calls the Object form instead of var-args, may be because it is an Object of primitives(not objects) ?

Harsha Smith, are you aware of any documentation on this behavior to refer to ?

Thanks
Raj
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Actually, the JLS uses the phrase "widening" to refer to (implicitly) casting a reference to a supertype.


Thanks for letting me know about this usage.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic