Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Java support only pass by value. Primitive types can be changed using wrapper class

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java support only pass by value.  If you're sending an object ideally you will send the object reference, this reference will be copied to method parameter ( system.out.print(object) will give you a address of the object). So in this case object reference will be used throughout the function, that's why the changes in other methods will reflect in main method. If you reinitialise the passed object than the changes will not reflect in main method.
If you want to change primitive types than do wrapper class.
 
Ranch Hand
Posts: 128
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Marshal
Posts: 68112
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch again

Please explain more. The only part I can understand is the first sentence. The remainder is confusing and I suspect you have misunderstood it.
 
Bartender
Posts: 21756
148
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Manjunath Dyavannanavar wrote:Java support only pass by value.  If you're sending an object ideally you will send the object reference, this reference will be copied to method parameter ( system.out.print(object) will give you a address of the object). So in this case object reference will be used throughout the function, that's why the changes in other methods will reflect in main method. If you reinitialise the passed object than the changes will not reflect in main method.
If you want to change primitive types than do wrapper class.



No. Java does pass by reference for objects, and pass by value for primitives, including primitive expression results. And you should NOT refer the the reference as an "address", because chances are very high that it is no such thing. It can, for example, be an index into a master object locator table.

Changes to objects will be reflected back to the caller, since they are literally changes to that object, not a copy of that object (as they would be if objects were call-by-value). Since the parameters in a method are simply variables initialized with the parameter values/references passed, changing the variables themselves simply re-assigns values to the variables. Which should not be confused with changing the properties of variable's object while keeping the object reference unchanged.
 
Rancher
Posts: 4493
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:
No. Java does pass by reference for objects, and pass by value for primitives, including primitive expression results.



Unless the definitions have changed since the last time this topic came up then that is not correct.
Java is pass by value always.

You can't change the valueof a parameter passed into a method and have it reflected outside of that method.
 
Campbell Ritchie
Marshal
Posts: 68112
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:. . . You can't change the valueof a parameter passed into a method and have it reflected outside of that method.

That is my understanding too: pass by value only. This JavaPapers link explains it; the section from the JLS (=Java® Language Specification) tells you that the method uses newly created parameter variables.
What confuses people is that mutable reference types can change their state inside a method and they don't know that isn't pass by reference.
 
Joseph Mokenela
Ranch Hand
Posts: 128
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What confuses people is that mutable reference types can change their state inside a method and they don't know that isn't pass by reference.



I think the confusion is caused by some text books which actually teach that java is pass by reference on Object types and pass by value on primitive types.
 
Campbell Ritchie
Marshal
Posts: 68112
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, both those factors confuse people.
 
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java is pass by value always, even for object references. The differences with object references is that unless they are immutable, changes made to the referenced object in the called method will be reflected to the object in the calling method. The reference in the calling method, however, can never be changed to refer to a different object, no matter what happens to the reference in the called method.

In summary, if an object is mutable and a reference to it is passed to another method, the reference itself is passed by value and will not be affected by any reassignment in the called method. However, changes made to the object via that reference will be seen.
 
Junilu Lacar
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This code will print [1, 5, 3, 4] not [5, 6, 7].
 
Marshal
Posts: 7501
509
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some of you (well, most actually) in this thread have a long lasting record in the field, and the term "pass by value" you can and possibly relate to some past experience and possibly even different language (where things work differently) - so it has a specific, more concrete/tangible meaning.

For me, let's say relatively new to field, even though I know what is meant and how that actually works - term "pass by value" says absolutely nothing, and I'd say even confusing from what I learned in university when there were talkings about primitive values and object references.

So really I do understand the folks new to the field, and get why there is so much confusion about this topic.

Junilu's example covers both scenarios which I understand, however, even understanding that, I could not wrap up in my head what this "pass by value" really is. I think to most "pass by copy" probably would be more clear and would help to unwrap the concept easier and quicker.
 
Junilu Lacar
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pass by value is essentially passing a copy. In the case of an object reference, you're passing a copy of the reference, which references the same object on either side of the call. If the called method mutates the object via its copy of the reference, those changes will be seen from the caller. However, if the called method changes the reference itself, i.e., reassigns its parameter to a different object, then it's only changing a local variable and therefore that change will not be reflected in the calling method. That's really all you have to remember: a parameter is just a local variable so if you reassign it, that reassignment does not affect what was passed in.
 
Saloon Keeper
Posts: 11488
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pass by reference means you can change the value of the variable that you used as an argument. In other words, the method parameter is an alias for the variable in the calling code. By changing the value of the parameter, you change the value of the variable in the calling code.

From this we can conclude that Java does not support passing by reference, as you can never change the value of a variable by calling a method on it. A local variable of a reference type will always reference the same object it referenced before the method was called.
 
Campbell Ritchie
Marshal
Posts: 68112
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Manjunath Dyavannanavar wrote:. . . If you want to change primitive types than do wrapper class.

Please explain more. Wrapper objects are usually immutable. I don't see how you can use them to change primitives.
Actually, primitives are sort of immutable. It is not possible to change their values, only to replace the variable with a new value.
 
What a stench! Central nervous system shutting down. Save yourself tiny ad!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!