• Post Reply Bookmark Topic Watch Topic
  • New Topic

whether java has Pass by value/ pass by reference  RSS feed

 
raj talatam
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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://www.coderanch.com/how-to/java/CallByReferenceVsCallByValue

 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

in complete reference


Which reference?
 
raj talatam
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:

in complete reference


Which reference?

java2 complete reference
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!