• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

pass by value or passl by reference!

 
Manas Ahlaad
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java says all the variables are passed by value irrespective of they are objects are variables.

Why not objects passed by reference ???
TIA
 
Bosun Bello
Ranch Hand
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object references are passed by reference. Yes, a coopy of the reference is passed to a method, but changes made using thet copy of the reference are reflected in the calling method. In the case of primitives, they are passed by copy. i.e changes made in the called method are not reflected in teh calling method.
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manas
It is slightly confusing when you first hear it, but:
All variables are passed by value. For primitives the value passed is simply the value that the variable contains. For reference variables (objects and arrays) the value that the variable holds is a reference to the object or array, so it is a reference that is passed.
hope that helps
 
Michael Matola
whippersnapper
Ranch Hand
Posts: 1817
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the Campfire story on pass by value in Java.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Corey McGlone has a neat Flash animation on his website in the SCJP section that is sure to help any beginner conceptualize what's going on.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic