Does Java have pass by reference?

Just curious how this works in Java, seems like it isn’t like classic pass by reference in other languages like C/C++.
11 Replies
JavaBot
JavaBot4mo ago
This post has been reserved for your question.
Hey @DaMango! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st4mo ago
Java is generally pass-by-value but it may pass references by value With primitive data types, you have normal pass by value but with objects, (which are located in the heap), you always have references to these objects so you are passing these references - but if you reassign them, you have a different object and this is not propagated For example
public class SomeClass {
private String text;
public static void caller(){
SomeClass obj = new SomeClass();
obj.text = "caller value";
callee(obj);// you pass the value of the reference to the object
System.out.println(obj.text);//callee value 1
}
private static void callee(SomeClass obj){
obj.text = "callee value 1";// you update the content of the object - this is the same object the caller uses
obj = new SomeClass();//you assign obj to another object - now the variable points to an object that isn't the same as the one used by the caller
obj.text = "callee value 2";// you are changing the content of the different object
}
}
public class SomeClass {
private String text;
public static void caller(){
SomeClass obj = new SomeClass();
obj.text = "caller value";
callee(obj);// you pass the value of the reference to the object
System.out.println(obj.text);//callee value 1
}
private static void callee(SomeClass obj){
obj.text = "callee value 1";// you update the content of the object - this is the same object the caller uses
obj = new SomeClass();//you assign obj to another object - now the variable points to an object that isn't the same as the one used by the caller
obj.text = "callee value 2";// you are changing the content of the different object
}
}
See also https://stackoverflow.com/q/40480/10871900
DaMango
DaMangoOP4mo ago
when would you want to pass the references?
dan1st
dan1st4mo ago
?
DaMango
DaMangoOP4mo ago
when is it advantageous to pass the object reference err if you're passing the reference of the object by value you don't really get a performance benefit from doing so?
dan1st
dan1st4mo ago
What do you mean with that? Using references allows you to have complex/arbitrary object graphs and other structures
DaMango
DaMangoOP4mo ago
in c/c++ you might pass by reference to avoid the overhead of copying a variable and using it to reassign the value of the original reference
dan1st
dan1st4mo ago
So? You are still passing references by value so the only thing that's copied is the reference
DaMango
DaMangoOP4mo ago
what situations have you passed the object reference in java? i'm looking to learn when it might be a good idea to do it vs. when not to
dan1st
dan1st4mo ago
Java always passes object references by value in that way which allows you to pass objects and modify them elsewhere it also has some disadvantages like it introducing indirections (following the reference can be a performance cost in a few ways)
JavaBot
JavaBot4mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?