• 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

pass by value or passl by reference!

 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at the Campfire story on pass by value in Java.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic