• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

pass by value or reference..confused!!!

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi to All,
I have read on many books and websites that Java supports pass by value. I have also read that primitives are passed by value and Objects are passed by reference. Since Java is object based language, it should have supported passed by reference. Isn't it contradictory issue? I am very much confused:-(
Do you have any words to remove my confusion??

Thanks in advance
--Krish
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java always passes by value. See the JavaRanch campfire story, Pass by Value.

Why would be be "contradictory" for Java to not support passing by reference?
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They say in java everything is pass by value. Primitive is pass by value, meaning copy of the number or character or whatever. Original is unaffected.

When it comes to reference variable or pointer, they make a copy of that and passed into the function/method. Original reference variable/pointer is untouched. However even though it is a copy, it still points to the same object as the original pointer. Meaning it can call methods of that object that changes the object's fields/properties like the original pointer. The tricky thing is that you can never mess up the original pointer no matter what you do with the copied one.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Krishna Acharya:
I have also read that primitives are passed by value and Objects are passed by reference.


Wherever you read that, it's wrong and confusing.

Everything is passed by value in Java. Variables of non-primitive types are really references to objects. If you pass such a variable to a method, the reference is passed by value. Note that that's not the same as passing by reference.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi krishna,
in java everythig is pass by value only.Remeber one thing that we are passing a copy of it not the variable or reference variable itself(original).
 
Krishna Acharya
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks so so so much for Marc, Toni,Jesper and Chandraiah.
I am crystal clear now.
--Krish
 
Whip out those weird instruments of science and probe away! I think it's a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic