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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Codeguru
Posts: 3
Joined: Fri Mar 29, 2013 2:53 pm
Contact:

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

Post 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?...

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Yes, it will be changed. You might want to try it out.

HTH,
Paull
If you like our products and services, please help us by posting your review here.

Codeguru
Posts: 3
Joined: Fri Mar 29, 2013 2:53 pm
Contact:

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

Post 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...

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

emj211
Posts: 5
Joined: Wed Apr 17, 2013 2:41 pm
Contact:

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

Post by emj211 »

That was an excellent explanation provided in the test.

insider
Posts: 29
Joined: Wed Apr 17, 2013 9:22 am
Contact:

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

Post 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.

sahilsaid
Posts: 5
Joined: Tue Oct 11, 2016 4:41 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

sahilsaid
Posts: 5
Joined: Tue Oct 11, 2016 4:41 am
Contact:

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

Post by sahilsaid »

Many Thanks Paul.

aliounebadara
Posts: 1
Joined: Wed Mar 08, 2017 9:17 am
Contact:

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

Post 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)

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post by flex567 »

Code: Select all

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

Code: Select all

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

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 26 guests