About Question enthuware.ocajp.i.v7.2.1174 :
Moderator: admin
-
- Posts: 3
- Joined: Fri Mar 29, 2013 2:53 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.1174 :
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?...
-
- Site Admin
- Posts: 10113
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1174 :
Yes, it will be changed. You might want to try it out.
HTH,
Paull
HTH,
Paull
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Fri Mar 29, 2013 2:53 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1174 :
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...
-
- Site Admin
- Posts: 10113
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1174 :
There is indeed no pass by reference in Java. It is correct that the reference of an object is passed by value.
-Paul.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Wed Apr 17, 2013 2:41 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1174 :
That was an excellent explanation provided in the test.
-
- Posts: 29
- Joined: Wed Apr 17, 2013 9:22 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1174 :
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.
-
- Posts: 5
- Joined: Tue Oct 11, 2016 4:41 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1174 :
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.
-
- Site Admin
- Posts: 10113
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1174 :
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.
It is explained with diagram nicely in this short article here: http://www.javaworld.com/article/207742 ... value.html
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Tue Oct 11, 2016 4:41 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1174 :
Many Thanks Paul.
-
- Posts: 1
- Joined: Wed Mar 08, 2017 9:17 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1174 :
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)
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)
-
- Site Admin
- Posts: 10113
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1174 :
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1174 :
Code: Select all
changeIt(str);
changeIt("one time");
Code: Select all
changeIt(String s = str);
changeIt(String s = "one time");
Who is online
Users browsing this forum: No registered users and 11 guests