Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arrays

 
Shiva Arava
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I know why the value of the zeroth element of myValue is 3, and not 2, because the modifyIt method just increments it.
public class JavaTest
{
public static void main(java.lang.String[] args)
{
JavaTest test = new JavaTest();
int[] myValue = {1};
System.out.println("myValue contains " + myValue[0]);

test.modifyIt(myValue);
System.out.println("myValue now contains " + myValue[0]);
}
public void modifyIt(int[] value)
{
value[0]++;
System.out.println(value[0]);
}
}

Output : myValue contains 1
: myValue now contains 3
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure the code you've posted here is the same code you're running. I just ran it and got this:

This is just what I'd expect.
Corey
 
Shiva Arava
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I checked the program, I get the value of myValue[0] as 2.
Thanks
Shiva
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic