Page 1 of 1

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

Posted: Mon Dec 15, 2014 2:51 pm
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,

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

Posted: Mon Dec 15, 2014 10:21 pm
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.

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

Posted: Wed Jun 24, 2015 3:09 pm
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?

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

Posted: Wed Jun 24, 2015 9:29 pm
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.

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

Posted: Mon Sep 03, 2018 8:13 am
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

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

Posted: Mon Sep 03, 2018 9:24 am
by admin
Please type the code exactly as given in the question.

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

Posted: Mon Dec 09, 2019 10:52 am
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?

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

Posted: Mon Dec 09, 2019 11:13 am
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.

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

Posted: Mon Dec 09, 2019 12:00 pm
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?

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

Posted: Mon Dec 09, 2019 9:15 pm
by admin
Right, if the string is not a constant.

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

Posted: Thu Jan 16, 2020 11:04 pm
by Nathan777
This question was on the exam. They just used different letter to make "Java" instead of "good".

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

Posted: Tue Nov 21, 2023 4:22 am
by Tester
Is the explanation:
"and false for newStr.equals(myStr) because newStr would then contain "nullgood"."
Right?

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

Posted: Tue Nov 21, 2023 5:34 am
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.

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

Posted: Tue Nov 21, 2023 7:32 am
by Tester
thank you for additional example! I missed conditional sentence.