• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String Pooling...!!!

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler will create strings when saw different strings and put them in the pool.There will be a string version in pools later.And put the in the pool.
In this section there use new... so you tell the complier "I want a new string one not the same version in pool. And this obj will put in the stack.
 
Vishal K Patel
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But here they are saying that "Whatever" and "who" both are created in the pool...?
Is it correct?
We are using new to create them....!!!

Have you read the options [A]and [D]?
What should be the correct?
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



quote:

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.




The compiler creates a string when it sees a string literal, but it does not create duplicates. I see 3 string literals. Two are duplicates. Compiler creates 2 strings - "Whatever" and "Who"

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. I see 2 new operators. At run time 2 strings will be created - "Whatever" and "Who".

It seems to me that this explains why answer "A" is correct.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, i think 'd' is correct. It may not be COMPLETE, but i don't see anything untrue about what it is saying.

A, however is confusing. "there will be a string Whatever and Who created at runtime".

that "a string" sounds like there will be only one single string created.

so i don't know what the right answer is.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sometimes I find mocks that are misleading.
they attempt to be tricky and force you to reason, but they only confuse your mind.
 
Chris Stann
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Out of what book did this question come? The K&B book is supposed to cover "everything you need to know" for the exam, and I do not recall covering this type of question - it does not drill down to this level of detail of String object and pool creation.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This discussion is a tiny bit outside the scope of the exam.
 
Chris Stann
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Bert!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic