• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Var args once again......

 
Ranch Hand
Posts: 63
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....

 
Ranch Hand
Posts: 49
1
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.>
 
He does not suffer fools gladly. But this tiny ad does:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic