Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about pass by value

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
Would please explain for me why the result of following program is 0 ,instead of 3?
Thanks

public class Test4{
public static void add3(Integer i){
int val = i.intValue();
val +=3;
//i = new Integer(val); //the same result as undeleted
}
public static void main(String args[]){
Integer i = new Integer(0);
add3(i);
System.out.println(i.intValue());
}
}
 
Madan, Gopal
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because you're adding 3 to the local variable val inside the method add3(); And not to the argument Integer i to the same method. When the method returns to main(), the original Integer variable still contains the original int value 0.
Hence the output is 0 and not 3
 
Manish Hatwalne
Ranch Hand
Posts: 2595
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stephan Cattet:
Hello
Would please explain for me why the result of following program is 0 ,instead of 3?
Thanks

public class Test4{
public static void add3(Integer i){
int val = i.intValue();
val +=3;
//i = new Integer(val); //the same result as undeleted
}
public static void main(String args[]){
Integer i = new Integer(0);
add3(i);
System.out.println(i.intValue());
}
}

"int val" is local variable of method add3, it has no scope/visibility outsid eit, you add 3 to this local variable, i which is a reference to an objecct of type Integer, remains unchanged in the entire peration, there's no code which changes its value, so in main it simpply prints 0.
HTH,
- Manish
 
Junilu Lacar
Bartender
Posts: 7572
52
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan,
The formal parameter i in add3() is a separate entity from the actual parameter i in main(). When you call add3, the value in main i gets copied to the parameter add3 i. Now, since i is a reference to an Integer object, what gets copied is the value of the reference (loosely, the place in the heap occupied by the actual object), not the object itself. That is, upon entry to add3, you now have two separate references to the same Integer object (see figure below). When the add3 i is made to reference a new Integer object (initialized with val), the main i is not affected and still references the Integer with a value of 0.
<pre>
(Heap - where objects are created)
Integer(0) Integer(3)
^ ^ ^
................... |.. |...............|...........
| | |
main() { | | |
Integer i = -----+ | |
} | |
+----------+ |
| |
add3(Integer i) { |
... |
val += 3; |
i = new Integer(val); // i ---------+
// at this point, add3 i now references
// another Integer object, different one
// from the Integer in main
}
</pre>
HTH

------------------
Junilu Lacar
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by JUNILU LACAR (edited October 24, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic