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

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

Moderator: admin

Post Reply
jp0202
Posts: 3
Joined: Wed Jun 04, 2014 8:22 am
Contact:

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

Post by jp0202 »

Which of the following expressions will evaluate to true if preceded by the following code?

Code: Select all

String a = "java";     
char[] b = { 'j', 'a', 'v', 'a' };     
String c = new String(b);     
String d = a;
Answers:

Code: Select all

1. (a == d)
2. (b == d)
3. (a == "java")
4. a.equals(c)
Correct answers as per software are 1, 3 and 4.
Why no.4 is correct? Don't get it.

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

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

Post by admin »

Both a and c point to two different String objects containing the same string, so equals returns true. Please read about String.equals method.
If you like our products and services, please help us by posting your review here.

jp0202
Posts: 3
Joined: Wed Jun 04, 2014 8:22 am
Contact:

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

Post by jp0202 »

I understand String.equals, what I don't get is where the idea that a and c contain the same string as it's not obvious from the given code at all?
Thank you.

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

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

Post by admin »

You may want to go through this: http://docs.oracle.com/javase/7/docs/ap ... tring.html
Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. For example:

String str = "abc";

is equivalent to:

char data[] = {'a', 'b', 'c'};
String str = new String(data);
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

jp0202
Posts: 3
Joined: Wed Jun 04, 2014 8:22 am
Contact:

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

Post by jp0202 »

Thanks for the explanation, that's great.

Kevin_C
Posts: 14
Joined: Mon Nov 03, 2014 5:18 am
Contact:

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

Post by Kevin_C »

I have a question about the third option. Let's say we have the following code:

Code: Select all

String s = "java";
The following returns true, because both String-values are equal:

Code: Select all

s.equals(new String("java"))
The following returns false, because both String-references are pointing to a different memory location:

Code: Select all

s == new String("java")
The following (similar to the third option) returns true, ...:

Code: Select all

s == "java"
... = because Strings are immutable and == "java" just looks at the value "java", instead of the String-object(-reference) "java"? So == "java" doesn't create a new String-object behind the scene with a new reference pointing to a different memory location, instead it only looks at the value?

Just want to make sure I correctly understand it.

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

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

Post by admin »

== checks just the memory location. So ideally, s == "java" should return false. But as you found out, it actually returns true. This happens because Java treats String objects differently. Whenever it sees a string literal being created (i.e. without the new keyword new String("java") ), it first checks if the same string object was already created before. If it was, then it assigns the same object i.e. the same memory location to the reference. If it wasn't, then it creates a new String object and keeps it in its pool of String objects. That is why s == "java" returns true.

This is called "interning" of Strings and you should read more about it from any book or online tutorials/articles.

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

raj_dp
Posts: 16
Joined: Sun Jun 12, 2016 7:43 am
Contact:

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

Post by raj_dp »

With reference to the question 57 of Test 6 the following String objects are created in the Heap and SCP. (Please correct me if I am wrong).
1) String a = "java" --> because of this a String object "java" is created in the String Constant Pool (SCP) and is pointed to by a.
2) String c = new String (b) --> Because of this a new String object "java" is created in Heap area and is pointed by c.
3) String d = a --> because of this the String object "java" which was created in step 1 is also being pointed by d.
Hence (a == d) is true as both are referring to the same object.
Also a.equals(c) is true as contents of both the string objects are same.
As for a == "java" I know it will be true but don't quite understand why.
I have gone through your above explanation but not quite clear. Also you mentioned about "interning" which I understand is creating an equivalent String object in the SCP if such an object is present in Heap area.
E.g.: String e = c.intern() would have created a String object in the SCP and pointed by e. But in given statements I am not able to see where interning happens. I am not sure if any interning happens implicitly.
Can you please help me understand this.
Regards
Raj

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

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

Post by admin »

You might want to go through this article:
geeksforgeeks.org/interning-of-string

String interning is a very well discussed topic on the internet. Just google because it is too much to explain in a post.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 98 guests