• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question about argument passing

 
Wenyu Deng
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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]++;
}
}
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • 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?
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic