aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes about  pass by value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "about  pass by value" Watch "about  pass by value" New topic
Author

about pass by value

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Aug 13, 2001
Posts: 86
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

Joined: Sep 22, 2001
Posts: 2581

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

Joined: Feb 26, 2001
Posts: 5288
    
  10

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).]


Junilu - [How to Ask Questions] [How to Answer Questions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about pass by value