Win a copy of Functional Reactive Programming this week in the Other Languages forum!
    Bookmark Topic Watch Topic
  • New Topic

can anyone explain this...

 
saritha cheler
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Report post to moderator
1. public class PrimitiveReferences{
2. public static void main(String args[]){
3. int[] muValue = {1};
4. modifyIt(myValue);
5. System.out.println("myValue contians" + myValue[0]);
6. }
7. public static void modifyIt(int[] value){
8. value[0]++;
9. }
10.}

At line 5 it prints 2. How can it be possible when the incrementation of the value is till that method. It should print 1 at line 5. Can anybody explain this y it prints 2 at line 5.
 
Kathy Rogers
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Report post to moderator
Saritha,
At line 3, you set myArray[0] = 1. Then, at line 4, you call the function modifyIt(). Because you're passing it an array, you actually modify the array itself. So, modifyIt adds 1 to myArray[0] which now equals 2. It doesn't matter here whether you use value[0]++ or ++value[0}. Then control returns to the main function. Line 5 is now executed - and the value 5 is printed out. And the program is finished.
Hope this helps,
Kathy
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Report post to moderator
Closing this thread .. see duplicate post
http://www.javaranch.com/ubb/Forum24/HTML/007481.html
------------------
Jane Griscti
Sun Certified Java 2 Programmer
"When ideas fail, words come in very handy" -- Goethe
 
    Bookmark Topic Watch Topic
  • New Topic