It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method Resolution Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Method Resolution" Watch "Method Resolution" New topic
Author

Method Resolution

naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

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

Joined: Jul 18, 2011
Posts: 287
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

Joined: Oct 26, 2011
Posts: 15
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".


Rajanand P K, Oracle Certified Professional, Java SE 6 Programmer.
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
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

Joined: Oct 23, 2011
Posts: 384

thanks harsha smith.
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384



Above code prints Object.

But how int is converted to Object ?
Is that what happens : int-->Integer--->Object
Rajanand Pandaraparambil Kuttappan
Greenhorn

Joined: Oct 26, 2011
Posts: 15
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

Joined: Jul 18, 2011
Posts: 287
"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

Joined: Mar 12, 2011
Posts: 808
Rajanand Pandaraparambil Kuttappan wrote:Integer widens to Object


Integer does not "widen" to Object. Integer IS-A Object.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 4087
    
  18

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.


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Rajanand Pandaraparambil Kuttappan
Greenhorn

Joined: Oct 26, 2011
Posts: 15
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

Joined: Mar 12, 2011
Posts: 808
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method Resolution
 
jQuery in Action, 3rd edition