[HD Pg 132, Sec. 5.2.1 - string-interning]

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

Moderator: admin

Post Reply
Username987654
Posts: 95
Joined: Sat Dec 26, 2015 6:37 pm
Contact:

[HD Pg 132, Sec. 5.2.1 - string-interning]

Post by Username987654 »

In fact, when you do new String("hello"); two String objects are created - one in the string
pool because of the use of a quoted string, and one in the regular heap space because of the use of
new keyword.
should be
In fact, when you do new String("hello"); two String objects, at most, are created - if it does not already exist, one in the string
pool because of the use of a quoted string, and one in the regular heap space because of the use of the new keyword.
?
Thus, there should only be one "hello" in the string pool and 2 "hello" String objects after the first (7 lined) example. Correct? If not, why not?

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

Re: [HD Pg 132, Sec. 5.2.1 - string-interning]

Post by admin »

Correct, that statement should be corrected.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.


Username987654
Posts: 95
Joined: Sat Dec 26, 2015 6:37 pm
Contact:

Re: [HD Pg 132, Sec. 5.2.1 - string-interning]

Post by Username987654 »

Please help me to clarify my doubts. By the way, does the exam ask "how many objects" with respect to String interning?
String str1 = "hello"; // 1
String str2 = new String("hello"); // 2
String str3 = str2.intern(); // 3 get an interned string object for str2
System.out.println(str1 == str2); // 4 prints false
System.out.println(str1 == str3); // 5 prints true
In the text copied and pasted from the book, and slightly modified above to show line numbers, only two objects are created in total: At 1, "hello" is created in the String Pool. At 2, "hello" is created in the heap space. Correct?

str3 points to the object created at 1 since at 2, "hello" is already in the String Pool from 1, so when line 2 sees "new", the String constructor is passed "hello" to the String Pool object created at 1. Correct?

The String "hello" object referenced by str3 is not created at 3. Correct?[

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

Re: [HD Pg 132, Sec. 5.2.1 - string-interning]

Post by admin »

>At 1, "hello" is created in the String Pool.
Correct.
>At 2, "hello" is created in the heap space. Correct?
All objects are created in heap space. String pool is a special part of the heap space. But if you mean "not in string pool", then yes.

>str3 points to the object created at 1
Correct.

>since at 2, "hello" is already in the String Pool from 1, so when line 2 sees "new", the String constructor is passed "hello" to the String Pool object created at 1. Correct?
Sorry, couldn't understand what you mean here.

At 2, a new String object containing the same chars as "hello" is created. It has nothing to do with the string stored in the string pool containing the same chars. This is because of the "new" keyword.

At 3, the JVM checks if a string "hello" already exists in the string pool, if it does (and it does in this case), it returns a reference to that object. In this case, it is the same object that was created at 1.

>The String "hello" object referenced by str3 is not created at 3. Correct?
Correct. It was created at 1.
If you like our products and services, please help us by posting your review here.

Username987654
Posts: 95
Joined: Sat Dec 26, 2015 6:37 pm
Contact:

Re: [HD Pg 132, Sec. 5.2.1 - string-interning]

Post by Username987654 »

by admin » Thu Jun 20, 2019 9:27 pm
At 2, a new String object containing the same chars as "hello" is created. It has nothing to do with the string stored in the string pool containing the same chars. This is because of the "new" keyword.
More specifically, I was thinking about the following constructor copied from the book:
String(String str), String(StringBuilder sb) - Create a new String by copying the sequence of characters currently contained in the passed String or StringBuilder objects.
String str2 = new String("hello"); // 2
Here I thought that to begin to evaluate the 2 (new) constructor, since the only string pooled "hello" in then existence and used as the copied sequence of characters would be from 1, then allowing for the constructor to be evaluated based upon the new keyword, where evaluation would result in the "hello" object creation at 2 in the non-String pooled heap space, where str2 is finally assigned to reference it.

This would not be inconsistent with your previous confirmations? Further, I was thinking that this is how the string stored in the string pool is quite fundamental with this specific constructor at 2?

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

Re: [HD Pg 132, Sec. 5.2.1 - string-interning]

Post by admin »

The new string object created due to the execution of new String constructor is independent of the pooled string object that was used to create it. I still not sure what is it that you are getting at. The constructor uses reference to some other string object (in this case, a reference to the pooled string object), to create a new string object. So? The newly created non-pooled string object has nothing to do with the original pooled string object.

You could as well create another new non pooled string object using new String(str2);
If you like our products and services, please help us by posting your review here.

Username987654
Posts: 95
Joined: Sat Dec 26, 2015 6:37 pm
Contact:

Re: [HD Pg 132, Sec. 5.2.1 - string-interning]

Post by Username987654 »

by admin » Fri Jun 21, 2019 12:41 am
I still not sure what is it that you are getting at.
Two things:
1)
by Username987654 » Wed Jun 19, 2019 3:30 pm
By the way, does the exam ask "how many objects" with respect to String interning?
2) Specifically here, it sounds as if we are in agreement that a total of 2 objects are created. Correct?

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

Re: [HD Pg 132, Sec. 5.2.1 - string-interning]

Post by admin »

>By the way, does the exam ask "how many objects" with respect to String interning?
No. But it does expect you to know about how equals and == will behave for two strings.

>2) Specifically here, it sounds as if we are in agreement that a total of 2 objects are created. Correct?

Considering all three lines, yes. Two string objects are created - one in string pool and one not in the pool.

String str1 = "hello"; // 1
String str2 = new String("hello"); // 2
String str3 = str2.intern(); // 3 get an interned string object for str2

If you consider just line 2, then no, this line creates only 1 string object because "hello" already exists in the pool.

If you remove line //1 altogether then, yes, String str2 = new String("hello"); will create two Strings, one in the pool because of the hardcoded "hello" and one not in the pool due to new.
If you like our products and services, please help us by posting your review here.

Username987654
Posts: 95
Joined: Sat Dec 26, 2015 6:37 pm
Contact:

Re: [HD Pg 132, Sec. 5.2.1 - string-interning]

Post by Username987654 »

(I may not have explained parts of my question correctly, but we are on the same page.) Thanks!

Post Reply

Who is online

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