Here is one question I found in one book....!!!
Q .Consider the following lines of code:
String s1 = "Whatever";
String s2 = new String("Whatever");
String s3 = new String ("Who");
Which of the following statements is true? (Choose all that apply.)
A. The compiler will create two strings Whatever and Who and put them in the pool, and
there will be a string Whatever and Who created at runtime.
B. The compiler will create two strings Whatever and Who and put them in the pool, and
there will be no string created at runtime.
C. The compiler will create two copies of Whatever and one copy of Who and put them in
the pool, and there will be strings Whatever and Who created at runtime.
D. The compiler will create one copy of Whatever and put it in the pool, and there will be
strings Whatever and Who created at runtime.
E. The compiler will create a string Whatever and put it in the pool, and there will be a
string Who created at runtime.
Ans : According to me " D " is the answer.
but they are saying that the answer is " A ".
and justifying in this way....
The compiler creates a string when it sees a string literal, but it does not create duplicates. The
statement with the new operator will be executed at runtime, and the string will be created even if
an identical string was created by the compiler. Can anybody explain their justification?
[ March 22, 2007: Message edited by: Vishal K Patel ]
[ March 22, 2007: Message edited by: Vishal K Patel ]