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

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

Moderator: admin

Post Reply
yardimci
Posts: 1
Joined: Mon Dec 15, 2014 1:50 pm
Contact:

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

Post by yardimci »

Hi all,

I just wonder that, when i write this code block instead of for loop:

Code: Select all

String newStr = "" + 'g' + 'o' + 'o' + 'd';
boolean variable b1 is evaluate to true either. Because newStr and myStr refer to same object in String pool.

Why the expression in the question doesn't work that way ? What is the difference?

Thanks in advance,

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

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

Post by admin »

You've asked a very good question.

This is because in case of String newStr = "" + 'g' + 'o' + 'o' + 'd'; you are forming the new string with compile time constant expression. So the resulting string will automatically be interned by the JVM, which means it will assign the same String object that the JVM created earlier.

In the question, the new string is computed by a loop. Loop is not a constant expression. It is computed at run time and so the JVM returns a new un-interned String object.

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

philfrei
Posts: 3
Joined: Fri Apr 05, 2013 5:25 pm
Contact:

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

Post by philfrei »

I do not understand when the String pool is or isn't used. Why isn't an existing String in the String pool found and used at run time? Wouldn't part of the benefit of having a String pool include making use of it at run time?

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

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

Post by admin »

Basically, the String pool is used whenever you have a String whose value is know at compile time. The exact details are given in Section 3.10.5 of JLS.
Can't really comment on why it is done that way. In designing a language some things are done a certain way not because they are right or wrong but because the designers believe that that is more practical or useful based on their experience. There are pluses and minuses of each approach and they have to strike a good balance.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

camilopro2005
Posts: 1
Joined: Mon Sep 03, 2018 8:02 am
Contact:

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

Post by camilopro2005 »

this should be corrected in the simulator
false false


public class StringFromChar {

public static void main(String[] args) {
String myStr = "good";
char[] myCharArr = {'g', 'o', 'o', 'd' };

String newStr = "";
for(char ch : myCharArr){
newStr = newStr + ch;
System.out.println(newStr);
}

System.out.println(myStr);
System.out.println(newStr);

boolean b1 = newStr == myStr;
boolean b2 = newStr.equals(myStr);

System.out.println(b1+ " " + b2);

}
}



good
good
false true
BUILD SUCCES

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

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

Post by admin »

Please type the code exactly as given in the question.
If you like our products and services, please help us by posting your review here.

demetrio
Posts: 16
Joined: Mon Sep 30, 2019 11:40 am
Contact:

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

Post by demetrio »

Why doesn´t in the last loop the String resulted from "newStr + ch;" come from String Pool? I mean, in the last iteration of such loop there is the string "good" which can be found in String Pool? I read all comments above and I don´t understand why "In every iteration of the loop, a new String object is created by appending the character to the existing String object". It makes sense that "g", "go", "goo" are created but why "good" isn't just used from String Pool? Should I assume that whenever a String is used in a loop it will not be used from String Pool? Or should I assume that whenever the String is resulted from concatenation (i.e newStr + ch) will never be used from String Pool?

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

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

Post by admin »

A String from the string pool will be used if it is computed at compile time, or, are constants. In this case, a string is being computed at runtime and so, it cannot be interned automatically. You would have to call the intern() method on that string to get the string object from the string pool

You may go through Section 3.10.5 String Literals of JLS 8 to learn the complete details.
If you like our products and services, please help us by posting your review here.

demetrio
Posts: 16
Joined: Mon Sep 30, 2019 11:40 am
Contact:

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

Post by demetrio »

I understood from "it cannot be interned automatically" that the only way to put a string in String pool after the rpogram starts is by calling intern method, right?

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

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

Post by admin »

Right, if the string is not a constant.
If you like our products and services, please help us by posting your review here.

Nathan777
Posts: 2
Joined: Tue Oct 01, 2019 4:10 pm
Contact:

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

Post by Nathan777 »

This question was on the exam. They just used different letter to make "Java" instead of "good".

Tester
Posts: 34
Joined: Mon Oct 30, 2023 11:55 am
Contact:

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

Post by Tester »

Is the explanation:
"and false for newStr.equals(myStr) because newStr would then contain "nullgood"."
Right?

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

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

Post by admin »

I am not sure what you are asking. The given explanation is correct, please let me know if you have any issue with it.
If you like our products and services, please help us by posting your review here.

Tester
Posts: 34
Joined: Mon Oct 30, 2023 11:55 am
Contact:

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

Post by Tester »

thank you for additional example! I missed conditional sentence.

Post Reply

Who is online

Users browsing this forum: Google [Bot], marpiva and 47 guests