• 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

How many String Objects are created?

 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Problem : Consider the following code and tell how many Objects are created ??




Confusion :- I am confused in answer 5 and 6.

The anonymous string Object created in line 1 and 2 are same or different. ie the anonymous String Object "c";

Thanks !!!
Happy Learning !!!

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You initialized 5 strings. Line 1 and line 2 are different since they have different length.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The anonymous string object "c" in lines 1 and 2 is the same. The object is a string literal. String literal is placed in the string constant pool, on the heap, by the JVM. Each time a literal is encountered, the pool is checked to determine if it exists. If it exists, it is reused. Otherwise a new one is created and placed in the pool.

The anonymous string object "c" is created at line 1. At line 2, when it is encountered the pool will be checked. Since it exists it will be reused.

BTW, the code will not compile because a } is missing.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Dixon wrote:

The anonymous string object "c" in lines 1 and 2 is the same. The object is a string literal. String literal is placed in the string constant pool, on the heap, by the JVM. Each time a literal is encountered, the pool is checked to determine if it exists. If it exists, it is reused. Otherwise a new one is created and placed in the pool.

The anonymous string object "c" is created at line 1. At line 2, when it is encountered the pool will be checked. Since it exists it will be reused.

BTW, the code will not compile because a } is missing.



So It means 6 String Objects are created, One string object on each line, except for line 1 where two String objects are created( "c" and s3), please correct me if i am wrong

Thanks in advance

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think 3 String objects will be created.


There is one more similar question :
Given:

How many String objects will be created when this method is invoked?
A. 1
B. 2
C. 3
D. 4
E. 5
F. 6

I think here it should create 4 objects, but they have given correct answer as C. Can anyone clarify?
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Varsha Gadekar wrote:I think here it should create 4 objects, but they have given correct answer as C.


And remember to QuoteYourSources when posting mock questions.
This is one of the most frequently raised questions, please do a search (perhaps for "Fred 47") in the forum, you will find many similar posts & answers.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic