• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

about pass by value

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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());
}
}
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic