This question already has answers here:
Change boolean Values?
(5 answers)
Closed 6 years ago.
I have a scenario where I want to set a Boolean object and then use its booleanValue() in a constructor later on in the method. However, the scope in which the object is set is different. It is set in a method called by the method in which the object is first instantiated. Based on my understanding of how Java passes primitive and object arguments and reading several posts online (such as this), when an object is passed into a method, its properties are passed by reference and any change in the called method should be reflected in the calling method after the called method has finished execution. However I am noticing that when the called method is finished, any change in there is not taking effect in the calling method.
Here is a snapshot of my scenario:
private CustomObject1 callingMethod(){
Boolean b = Boolean.TRUE;
List<CustomObject2> list = this.calledMethod(b);
//Create CustomObject1 with b.booleanValue() as one of the arguments in the constructor
}
private List<CustomObject2> calledMethod(Boolean b){
...
...
if(condition){
b = Boolean.FALSE;
}
...
...
}
By the time the code reaches the CustomObject creation b.booleanValue() is always true, even if the if-statement in callingMethod() is true and the Boolean is set to false in that method.
I am reluctant to change the return type of the calling method to be a boolean as it would require changes in other bits of code that may call this method. Currently they only need a signature change but a return type change would require more work as the logic needs to be maintained (i.e populating a list and then doing something with it)
First, there is a lot of misinformation about parameter passing in Java, like "objects are passed by reference, primitives are passed by value" which is not true. Everything is passed by value.
You're not passing an object by reference in Java, you're passing an object reference by value. Boolean b does not hold a Boolean object, it holds a reference (a pointer) to a Boolean object.
Here's a good article about it: http://javadude.com/articles/passbyvalue.htm
Second, Boolean (like the other wrapper objects and also String) are immutable objects. So with an immutable object, and as they are passed by value (better said, their references are passed by value), you cannot achieve what you desire. You'll need to have a mutable object instead, like #rob mentioned.
Or use MutableBoolean from Apache Commons Lang.
The problem is that you are reassigning b in calledMethod. A reassignment in calledMethod only reassigns the variable declared in that method's parameter list; it does not modify the variable declared in the caller's scope.
To do what you want to achieve, you could either convert b to a field, or create a MutableBoolean class which allows you to do something like b.setValue(false).
Related
I am trying to use Spring AOP/ AspectJ to access my parameter after the method has done some modifications to it.
example:
public void changeValueOnFoo(Foo fooToModify) {
fooToModify.changeValue("1");
}
#Around("execution(* com.my.FooFunctions.changeValueOnFoo(..)")
public void interceptFoo(ProceedingJoinPoint jp) {
Foo f = (Foo) jp.getArgs()[0];
System.out.println(f.getValue()); // will print "1"
jp.proceed();
Foo modifiedf = jp.getArgs()[0];
System.out.println(modifiedF.printValue()); // will print "2"?
}
Is something like this possible? Procceding, then recalling the parameter after it's been modified by the method? Or does getArgs simply hold a pointer to the original state of the parameter so this isn't possible?
Is something like this possible? Procceding, then recalling the
parameter after it's been modified by the method?
Yes, it would work, because getArgs() holds the reference to the object passed as parameter (i.e., Foo). So any changes made to the fields of that object will be visible to the outside has it would be using plain Java.
Or does getArgs
simply hold a pointer to the original state of the parameter so this
isn't possible?
That "simply" makes it possible to actually see the changed state.
Bear in mind, however, that this will only work for object types, since they are called by reference. This will not work for primitives data types (e.g., int, float, ...), or immutable objects (e.g., Integer, String and so on).
I know java is pass by value, period. However, I still can't figure out this.
public static void changeTheName(String obj){
obj.toUpperCase();
}
This method will not affect the original string object, Fairly understandable. Because strings are immutable and changing in string literals means that the reference variable will now refer to the new object and the old one will be left for the garbage collector. But when I pass a string array I'm able to change the string literals that means I'm able to change the references. Why is this happening with the array because if we do obj[]= new String[]{} it will not affect to the original array and the original still refers to the old array and that is similar to directly changing the string literals
public static void ChangeTheName(String obj[]){
for(int i=0;i<obj.length();i++) obj[i]=obj[i].toUpperCase;
}
Edit:
The answer I was looking for is that reference of obj and obj[0] are unique and that's why the second method is able to change the entire content of my array. As I'm from C background and I thought obj and obj[0] has same refernces but that is not the case in java for sure.
toUpperCase does not change the String, it returns a new String which is uppercase.
#karthikdivi said - toUpperCase() does not change the String. it returns a new String which is uppercase.
But as i understand that you want to know why object value is changed in method but not premitive data type value?
Although Java is strictly pass by value, the precise effect differs between whether a primitive type or a reference type is passed.
.
When we pass a primitive type to a method, it is passed by value. But when we pass an object to a method, the situation changes dramatically, because objects are passed by what is effectively call-by-reference. Java does this interesting thing that’s sort of a hybrid between pass-by-value and pass-by-reference. Basically, a parameter cannot be changed by the function, but the function can ask the parameter to change itself via calling some method within it.
While creating a variable of a class type, we only create a reference to an object. Thus, when we pass this reference to a method, the parameter that receives it will refer to the same object as that referred to by the argument.
This effectively means that objects act as if they are passed to methods by use of call-by-reference.
Changes to the object inside the method do reflect in the object used as an argument.
if you are doing like below will only change the value.
public static void(String obj[]){
for(String s:obj) {
s=s.toUpperCase();
}
}
I think thus make changes.
What difference does it makes?
Let's think we have a method in java as following:
void demoMethod(MyClass mc){
// some operations
}
First type:
demoMethod(new MyClass()); // directly passing an object
Second type:
MyClass mc = new MyClass();
demoMethod(mc); // passing reference of an object
No difference in terms of the method's behavior on that reference. The first code can semantically translate to the second one. Eventually, the object created using new MyClass() needs to be stored somewhere so that it can be re-loaded and passed to the method.
However, using the second code you can re-use the reference.
It doesn't make any difference for demoMethod. Actually in both cases you are passing reference only.
However if you want to use the information after demoMethod does some operation in the calling method, you can't do that in first type.
Assume your demoMethod sets a property of your MyClass object to true or false, you don't have any way to find out what it's value is set to.
So, you can do something like
demoMethod(mc);
if(mc.isMyProperty()==true)
System.out.println("mc is changed");
This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 9 years ago.
I'm interested to know exactly what's happening under the bonnet when passing a variable or object into a function.
When passing an object or variable into a function, is a new copy of the object/variable created in the new scope? (A set of parentheses constitutes a scope in java right?). Or is the reference to the existing variable/object in memory passed in? Although that would only make sense for a global object/variable?
java is always pass by value so a new variable or reference variable(which refer to some object) will be created in the function to receive the value that has passed to it...
The scope of these variable will be withing that function in which it has created.
One thing you should know that even object are passed by value in java...when people say we pass the object to method ,that time we actually pass the value referred by reference variable not the object...so both the old and new reference variable refer to same object in heap memory..
check this for reference...
http://javadude.com/articles/passbyvalue.htm
http://www.programmerinterview.com/index.php/java-questions/does-java-pass-by-reference-or-by-value/
The easiest way to think of this is to get away from thinking of variables as ever being objects. A reference variable or expression is either null or a pointer to an object of appropriate class for its type.
Under this model, all Java argument passing is by value.
When you pass a reference to a method, you pass the null-or-pointer value to it. Assignment to the argument only affects the argument. It does not affect any variables in the caller's environment. On the other hand, if it is not null it points to the same object as the caller's variable or expression pointed to. Calling a value-changing method in that object changes its value for all code using a pointer to that object, including the caller.
Both - you get a copy of the object reference (for objects), and a copy of the value for primitives.
So unlike C, you can't pass in a variable reference (for a string for example) and end up with it being repointed to something else. And you can't pass in an int, for example, and change it's value within the method - any changes it to it will only be within the method scope.
e.g:
MyObjectHolder holder = new MyObjectHolder();
holder.setObject(new Object());
//holder reference id = 1
// holder.object reference id = 2
doIt(holder);
public void doIt(MyObjectHolder methodScopeHolder) {
// methodScpeHolder reference id = 3
// methodScopeHolder.object reference id = 2
}
In Java your program's "local" variables are maintained in a "stack frame", which is a section of a large array whose elements can contain any data type.
When you call, you copy the parameters (which may be either "scalars" -- chars, ints, floats, etc -- or "references") into a new area of the array (the "top"). Then, during the call, the index values that control which elements of the array you can access are adjusted, and the copied parameters become the "bottom" of a new stack frame, with the called method's local variables being above parameters. So to the new method its copies of the parameters are just like local variables.
Effectively, each method has a "window" into the overall stack, and the "windows" overlap to cover the parameter list.
Of course, when you "pass" an object you're really just passing a reference to the object, and the object itself is not copied.
When you pass a variable, you are passing the reference.
When you pass an object, you are passing a copy of it.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is Java pass by reference?
Hi guys,
I have a question about the arguments passing in Java, I read it from a book "In Java the arguments are always passed by value", what does this mean?
I have no experience of C++ and C, so it is a little bit hard for me to understand it.
Can anyone explain me?
Yes, java method parameters are always passed by value. That means the method gets a copy of the parameter (a copy of the reference in case of reference types), so if the method changes the parameter values, the changes are not visible outside the method.
There are two alternative parameter passing modes:
Pass by reference - the method can basically use the variable just like its caller, and if it assigns a new value of the variable, the caller will see this new value after the method finishes.
Pass by name - the parameter is actually only evaluated when it's accessed inside the method, which has a number of far-reaching consequences.
It means that when you pass a variable to a method, the thing that is passed is the value that is currently held by the variable. Thus, subsequents assignments to the method's argument will not affect the value of that variable (caller side), nor the opposite.
A pass-by-reference means that the callee receives a handle to the caller-side variable. Thus, assignments within the method will affect the caller-side variable.
In Java everything is an object. Object is a pointer like C. But in Java, it points the memory place of a class. Passed by value means, what object's value is, this value is passed by value. For example; Integer a=new Integer(); Integer b=new Integer(); setAInteger(b);
public void setAInteger(Integer c){
a= c;
}
After this operation a points the memory place of b. Lets say, at the beginning a=2500 b=3500, after method is called, new a value is 3500. By the way, 2500 and 3500 are memory addresses.