• 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

Number of objects when doing new Srting

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a small doubt:

When we create a new String like:
String s1 = new Strring("abc");

How many objects are created at this point?

Thanks.
 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IF you talked about Object then Only 1.

But someone may not be agree.

For those. the total number of objects created are 2 (1. "abc" & 2. S1).


But I would like to ask them a question that why they are treating "abc" as an Object.

It is an string literal value. and stored in literal pools.

Pls correct me if I am wrong.

:roll:
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankur,

what makes you think that a String literal is not an object?
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It creates 43 trillion objects. Oh wait, it's just the 43 trillionth time that this question has been asked How about a FAQ? It is still quite clear that misinformation is being propagated. The problem of course is, do you trust the FAQ? There are many (more than otherwise) that themselves purport to display truth but advise to the contrary.

The next best thing is to figure it out for yourself.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ankur Sharma:

Pls correct me if I am wrong.



OK: you are wrong. A String literal represents a java.lang.String object which lives in the heap like any other Java object. A reference to that String is stored in the literal pool, which is just a hashed set of references; it's not a special area of memory in which objects can be allocated (despite the fact that there are apparently any number of low-quality Java texts that say something to the contrary!)


:roll:



I hope you will now answer me a question: many people seem to use this smiley incorrectly. I don't understand why you've used it here: can you explain to me what you intended for it to mean?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic