wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Var args once again...... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Var args once again......" Watch "Var args once again......" New topic
Author

Var args once again......

Moguluri Ravi Kiran
Ranch Hand

Joined: Apr 16, 2010
Posts: 62

Hello & hi

See the folowing code,given two lines of code indicated as line1 & line 2 in main(),
now in line 1, var args method is called with 'Integer[]';
line 2, it is called with 'primitive int array';

My Problem is "why in case of primitive int array, the 'ob'j array is (the parameter of printAray()) is not aliased to the array in which
1,2,3,4 are present (see line 2) ?it is just becoming an array with length 1 & holding the refference to the int array...

please explain me the significant difference between these two...

and

Thanks to the all ranchers who are helping alot....

John Mercier
Greenhorn

Joined: Nov 23, 2006
Posts: 9

It looks like line 1 is being treated as an array of Objects and the vararg is able to point to that array. The int[] is being treated as only an Object so it is the same as passing any single object to the method. I'm not sure where this is pointed out in the SCJP guide I have but this behavior is good to know. Thanks for pointing it out!
Samrat Debroy
Greenhorn

Joined: Oct 23, 2009
Posts: 13
Just changed the code little bit for better understanding in which scenario it would work as expected.

This is because the code(line 2) compiles like this:

Hope this clears the doubt.>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Var args once again......
 
Similar Threads
Why it is not 'null' ?
Java ---== operato
Array of Arrays ???
How to understand this statment
doubt in arrays...