• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

question about argument passing

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
anyone can tell me the myValue[0] is 1 or 2? thanks a lot,the code is below:
public class PrimitiveReference{
public static void main(String args[]){
int [] myValue={1};
modifyIt(myValue);
System.out.println("myValue is"+myValue[0]);
}
public static void modifyIt(int[] value){
value[0]++;
}
}
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The best way to know is to run it!
The general rule is the method cannot change the parameter, so it cannot make the caller's variable myValue point to a different array. But the method can change what's in the array, so it can change value[0] from 1 to 2.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will tell you what I did, I analysed the code and predicted the answer. Then I compiled the code, and I was correct.
Have you done that?
 
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Wenyu
I suggest that you can run your programme yourself that must get the result
After run your programme,if you do not know why, you can talk about it
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I predict that you want to ask the question which concept of the programme is pass by reference or pass by value?
Is it right?
or you want to ask why?
 
reply
    Bookmark Topic Watch Topic
  • New Topic