• 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

whether java has Pass by value/ pass by reference

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Experts,
can any please tell me whether java uses pass value, or pass by reference , in complete reference it is written that it uses both ,but in many blogs i have read it uses only pass by value
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

raj talatam wrote:
can any please tell me whether java uses pass value, or pass by reference , in complete reference it is written that it uses both ,but in many blogs i have read it uses only pass by value



https://coderanch.com/how-to/java/CallByReferenceVsCallByValue

 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


in complete reference



Which reference?
 
raj talatam
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ivan Jozsef Balazs wrote:


in complete reference



Which reference?


java2 complete reference
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The book is wrong, or at least it's using sloppy language.

Java only has pass-by-value. But what might be confusing is that references are also passed by value. People sometimes say that "objects are passed by reference". But they are using sloppy language when they say that. First of all, objects are not manipulated directly in Java; they are always manipulated through reference variables. So, you can't "pass an object", what you do is you pass a reference to an object. And that reference is passed by value; in other words, a copy of the reference is passed when you call a method.

Note that passing a reference by value is not the same thing as pass-by-reference.
 
raj talatam
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
k guys,

i have one class for example



if java passes variables by value , then it should print 20, 10 .

but it is printing 20,20;
Actually what is happening ???
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

raj talatam wrote:
if java passes variables by value , then it should print 20, 10 .

but it is printing 20,20;
Actually what is happening ???




Generally, "pass by value" versus "pass by reference" is in the context of parameter passing.

So, you need to explain to us what is "pass by value" mean in this context -- as you are not using the standard definition.

Henry
 
Jesper de Jong
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

raj talatam wrote:if java passes variables by value , then it should print 20, 10 .


No, your example doesn't have anything to do with pass-by-value or pass-by-reference. Pass-by-value or pass-by-reference are concepts that have to do with how arguments to a method are passed. In your example, you're not calling any methods, so the example says nothing about whether arguments are passed by value or by reference to a method.

See the JavaRanch Campfire Story: Pass-by-Value Please, which explains the concept in detail.
 
raj talatam
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:

raj talatam wrote:if java passes variables by value , then it should print 20, 10 .


No, your example doesn't have anything to do with pass-by-value or pass-by-reference. Pass-by-value or pass-by-reference are concepts that have to do with how arguments to a method are passed. In your example, you're not calling any methods, so the example says nothing about whether arguments are passed by value or by reference to a method.

See the JavaRanch Campfire Story: Pass-by-Value Please, which explains the concept in detail.



in that example i am assigning same object COne to xyz also right, if java passes by value , the Actual value of var1 has to be print right.
 
Jesper de Jong
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

raj talatam wrote:in that example i am assigning same object COne to xyz also right, if java passes by value , the Actual value of var1 has to be print right.


Once again, this does not have anything to do with pass-by-value or pass-by-reference. Those terms only refer to how parameters are passed to methods. Pass-by-value or pass-by-reference has nothing to do with the fact that variables in Java (of non-primitive types) are references.

What's happening in your example code:

Line 10: You create a new COne object and assign it to the variable 'abc'. Its member variable var1 will be initialized to 10 (line 3).
Line 11: You set the member variable var1 of the object created in line 10, to 20.
Line 12: You print the value of the member variable var1 of the object created above. It will print 20.
Line 13: You declare and initialize another variable 'xyz' and let it refer to the same COne object that 'abc' refers to. Note: There is no new COne object created. Note that the member variable var1 of the object is still 20, because you set it to 20 in line 11.
Line 14: You print the value of the member variable var1 of the object that 'xyz' refers to. It's 20, because there's only one COne variable, to which 'abc' and 'xyz' both refer.

No methods are called, there's no pass-by-value or pass-by-reference involved.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

raj talatam wrote:in that example i am assigning same object COne to xyz also right, if java passes by value , the Actual value of var1 has to be print right.


This may sound harsh, but I'm not sure whether you're quite ready to tackle this one yet.

As Jesper says the pass-by-value/pass-by-reference distinction ONLY applies to values passed to methods and, to be honest, in Java the whole point is moot because the language only does it one way.
It's mainly for programmers crossing over from C or C++ that have to learn "the Java way of passing things"; if you're starting out with the language there's basically nothing to learn - although most good tutorials or textbooks will explain the mechanics.

Winston
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic