Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1174 :

Posted: Wed Apr 03, 2013 8:09 pm
by Codeguru
If you are passing the reference by value, and the local method variable hold the memory location of the actual object, and you by some method change a variable that is inside that object, then is that changed? As in o1.a = 5 inside the method, after execution of the method and when control goes back to main, the actual object that was passed in will have a value of 5 in its a variable?...

Re: About Question enthuware.ocajp.i.v7.2.1174 :

Posted: Thu Apr 04, 2013 6:14 am
by admin
Yes, it will be changed. You might want to try it out.

HTH,
Paull

Re: About Question enthuware.ocajp.i.v7.2.1174 :

Posted: Tue Apr 16, 2013 9:39 pm
by Codeguru
When I was first reading java books, they gave the impression that no matter what you do, nothing is passed by reference, and this clearly is. Just like pointer values in C++. Glad to clear this up finally...

Re: About Question enthuware.ocajp.i.v7.2.1174 :

Posted: Wed Apr 17, 2013 10:32 am
by admin
There is indeed no pass by reference in Java. It is correct that the reference of an object is passed by value.
-Paul.

Re: About Question enthuware.ocajp.i.v7.2.1174 :

Posted: Wed Apr 17, 2013 2:44 pm
by emj211
That was an excellent explanation provided in the test.

Re: About Question enthuware.ocajp.i.v7.2.1174 :

Posted: Thu Apr 18, 2013 2:23 am
by insider
I always like seeing such questions even if I fail. It is a true masterpiece, it catches on things you usually don't give the necessary attention.

Re: About Question enthuware.ocajp.i.v7.2.1174 :

Posted: Sun Oct 16, 2016 11:27 am
by sahilsaid
I though as String is an object and because a value of the reference is passed (in case of an object rather then a copy of it) would change the reference to a new string in this question but seems like that's not the case.

Re: About Question enthuware.ocajp.i.v7.2.1174 :

Posted: Mon Oct 17, 2016 1:38 am
by admin
The value of the original reference variable is passed to the method and inside the method that value is assigned to the local variable. Now, when you change that local variable to point to something else, the original variable's value doesn't change. It still keeps pointing to the same object.

It is explained with diagram nicely in this short article here: http://www.javaworld.com/article/207742 ... value.html

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1174 :

Posted: Mon Oct 17, 2016 11:03 am
by sahilsaid
Many Thanks Paul.

Re: About Question enthuware.ocajp.i.v7.2.1174 :

Posted: Wed Mar 08, 2017 9:26 am
by aliounebadara
Hi PAUL,

First of all, I wanted to thank you for the excellent mock test you have in enthuware !!
In this question I think you should update your explanation, this case is strange because we have the string immutabilty which create an other String objet or redirect the local string reference to an other string object in the string pool.
If we had passed a mutable object in the method, the value should change.

For example :

class Obj {
String a = "8";
}

class Foo {

static void change ( Obj o){
o.a = "2";
}

static void change ( String o){
o = "2";
}


public static void main(String[] args){

Obj i = new Obj();
System.out.println(i.a); //8
change(i);
System.out.println(i.a); //2

System.out.println("-----------");

String s = "8";
System.out.println(s); //8
change(s);
System.out.println(s); //8
}

}

Sorry for the bad code formatting and the bad english ( I am french :p)

Re: About Question enthuware.ocajp.i.v7.2.1174 :

Posted: Wed Mar 08, 2017 10:13 am
by admin
No, the explanation is correct. You will see the same behavior even if you pass a mutable object.

The code example that you have shown is not same as the one given in the question. The code in the question doesn't attempt to change anything inside the passed object but your code is actually changing the contents of the passed Obj object.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1174 :

Posted: Fri Nov 02, 2018 4:48 am
by flex567

Code: Select all

changeIt(str);
changeIt("one time");
means

Code: Select all

changeIt(String s = str);
changeIt(String s = "one time");