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

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

Moderator: admin

Post Reply
Tariee
Posts: 5
Joined: Wed Sep 02, 2015 7:21 pm
Contact:

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

Post by Tariee »

Hello :)
My question is, I thought once you try to change the value of the local variable itself, e.g s++ or s = a, the reference starts pointing to a new object. So how come in this question, cA[1] in m1() and cA[1] in m2() are still pointing to the same object when the following occurred in m2(): cA[1] = cA[0] = 'm';

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

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

Post by admin »

Tariee wrote:Hello :)
My question is, I thought once you try to change the value of the local variable itself, e.g s++ or s = a, the reference starts pointing to a new object. So how come in this question, cA[1] in m1() and cA[1] in m2() are still pointing to the same object when the following occurred in m2(): cA[1] = cA[0] = 'm';
What you say is true but there is an important point that you are missing.

cA is a reference to an array object. But the elements of that array are not references to other objects because this is an array of chars, which is a primitive. Therefore, cA[1] and cA[0] are not references. They are merely values. That is why, when you change cA[0], you are changing its value right there.

You should try running the same code after changing char arrays to Object arrays and then see what happens.

You may want to go through this article that explains the basic concept: http://www.javaranch.com/campfire/StoryPassBy.jsp

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

Tariee
Posts: 5
Joined: Wed Sep 02, 2015 7:21 pm
Contact:

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

Post by Tariee »

Thank you so much, its very clear now :) :)

AndaRO
Posts: 31
Joined: Wed Feb 08, 2017 5:42 pm
Contact:

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

Post by AndaRO »

It is resolved.

Sorry. :)

mcberenguer
Posts: 4
Joined: Thu Mar 02, 2017 12:00 pm
Contact:

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

Post by mcberenguer »

The explanation to this answer states that "So instance member 'c' keeps its default (i.e. 0) value." But it is not true - the default char value is not 0 but '/0000', this is, null. Here you get zero, because the char is cast to int.
The following snippet prints nothing:

Code: Select all

public class Demo {
	char ch;
	
	public static void main(String[] args) {
	System.out.print(new Demo().ch);
	
	}
}
But the following prints zero:

Code: Select all

public class Demo {
	char ch;
	
	public static void main(String[] args) {
	System.out.print((int)new Demo().ch);
	
	}
}
Last edited by mcberenguer on Sat Mar 04, 2017 4:58 pm, edited 1 time in total.

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

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

Post by admin »

char is an integral type just like byte, short, and int. Its default value is 0. '/0000' is just another way to write it Java code in character format. Internally, it stores 0. There is no difference between the two.

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

mcberenguer
Posts: 4
Joined: Thu Mar 02, 2017 12:00 pm
Contact:

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

Post by mcberenguer »

But the point is that the code is printing "0,m" because the char is cast to int, not because c keeps its default value. Without the cast, the code prints ",m". There may not be difference between the two internally, but when printed out the output is different.

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

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

Post by admin »

That is what the explanation to option 3 says as well, "Because of the explicit cast to int in the println() call, c will be printed as 0."

So yes, the cast to int is important and the fact it keeps its default value 0 is equally important here.

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

mjmsausava
Posts: 19
Joined: Sat Mar 25, 2017 5:38 am
Contact:

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

Post by mjmsausava »

Code: Select all

System.out.println( ( (int)c) + ", " + cA[1] );
In the line of code above, instance variable c is accessed without prefixing any object. I thought only static variables could be accessed directly (without object). Can you please help me in my confusion? Thanks.

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

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

Post by admin »

When there is no explicit object reference, an implicit reference "this" is supplied by the compiler for instance variable. So "c" is actually "this.c". You should read about this reference here:
http://stackoverflow.com/questions/3728 ... is-in-java
If you like our products and services, please help us by posting your review here.

shamran99
Posts: 15
Joined: Wed May 10, 2017 2:49 am
Contact:

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

Post by shamran99 »

Hi,

I just want to know is arrays the only one will be pass by reference?

Regards,
Shamran.

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

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

Post by admin »

No, in Java, everything is passed by value. But in case of objects (any object, including arrays) it is the value of the reference that is passed by value. It is a very important topic so I will suggest you to go through a good book to understand it. Here are a few good links -

http://javadude.com/articles/passbyvalue.htm
and
http://stackoverflow.com/questions/4048 ... s-by-value
If you like our products and services, please help us by posting your review here.

horst1a
Posts: 37
Joined: Mon Jun 12, 2017 2:16 am
Contact:

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

Post by horst1a »

And why does the compiler not put an implicit this before the c in m2()

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

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

Post by admin »

horst1a wrote:And why does the compiler not put an implicit this before the c in m2()
Because a variable named c is already in scope. The compiler tries using "this" (or the classname to use the static variable) only if it is not able to resolve the given variable. Here, it is already able to resolve c as a method parameter. So there is no need to use "this".

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

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 41 guests