• 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

Copy of value and reference

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody explain what is the basic difference between copy pf value and refrence while passing the variables and objects as arguments with an appropriate example.
thanks in advance
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you invoke a method upon an object the argument are passed by value, which means that a primitive value is copied and a reference value is copied (not the object itself)
Examples:

Let me know if it is not clear
Val
[This message has been edited by Valentin Crettaz (edited September 27, 2001).]
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just wanted add my 2 cents..
Java dont support pass by reference
So i dont think there will be questions in SCJP about this topic
Ragu
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ragu,
Yes, Java passes all values as copies however it is very important to understand that the value of a non-primitive variable is the memory address of an object and not a copy of the object itself.
If the method makes changes to the object referenced by a parameter the change is seen in the original object. This is not the case for primitive parameters.
You will see questions where knowledge of this distinction is required to select the right response.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
chetna_jain
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx a lot
yes i have got an idea but if pass an argument of int thru wrapper class will the value change as it is passed as an object.
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chetna,
You had better learn this little tid-bit before you go to far on the SCJP path:


All wrapper classes are immutable.


What does that mean? It means that once the wrapper class is created with a value that value can NEVER be changed!
Regards,
Manfred.
 
chetna_jain
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Manfred a lot.
I'll keep these things in my mind.
R strings also immutable
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes they are. StringBuffer are not.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic