• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

What this paragraph means?

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

String s = new String ("abc");

In this case, because we used the new keyword, Java will create a new String object in normal (nonpool) memory, and s will refer to it. In addition, the literal "abc" will be placed in the pool.



What's a nonpool memory? How come there are two object from the above line of code?

Thanks guys!
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java handles String differently. The String object in Java are immutable that means once it has been assigned with some value, it can not be changed.
The paragraph means, when creating new string object it creates 2 copies of it, one on the heap (non-pool) and one on the string buffer pool. When user tries to create another string with value say 'abc' as in this example, then it will first check it in the String buffer pool, if it is available it will not create one more object rather it will assigned that new object to refer to the existing string. If not , it will create again 2 objects as said earlier.
Hope this will clear your doubt.
 
Sheriff
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read this to know what string literal pool actually is...
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
does this mean that
String s=new String("abc"); and
String s1=new String("abc") actually create 3 objects?

if yes then we are clear on this.....

 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankur kothari wrote:does this mean that
String s=new String("abc"); and
String s1=new String("abc") actually create 3 objects?

if yes then we are clear on this.....




Yes it will create 3 objects.
 
Ankit Garg
Sheriff
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankur kothari wrote:if yes then we are clear on this.....


Whether Erap is clear or not also matters here...
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we refers to both of us....
 
Ankit Garg
Sheriff
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankur kothari wrote:we refers to both of us....


How are you sure he's clear on it??
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you will see....

 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even Soumil Shah is also not clear, see his answer, he is creating an object on the buffer pool. While objects are always created on the heap.
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but after my post he would be clear too....
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok enough, this game isnt getting any better
 
Ankit Garg
Sheriff
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankur kothari wrote:you will see....


Won't we see
Don't take whatever I'm saying offensively, what I meant initially was that if you understood it doesn't mean that everyone understood it. Different people have different levels of knowledge. Suppose I asked a question, a person gives an answer, a third person comes and says "well, that clears it all", then even if I don't understand it, I might hesitate in asking a followup question. I might feel that the third person understood it and I didn't, and if I say that I didn't understand it, then people might think that I'm dumb. So I might stay silent and let the misconception stay in my mind. Just think about it...
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankit please explain me this....am sorry for above.
https://coderanch.com/t/472669/Programmer-Certification-SCJP/certification/Array-enthuware#2116474
 
Ankit Garg
Sheriff
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankur kothari wrote:Ankit please explain me this....am sorry for above.


No need to say sorry. I would like to remind you that asking your own completely different question in someone else's topic is called a hijack. So try to refrain from that. Punit has already replied to your question, so you've got to be patient...
 
Soumil Shah
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Punit Singh wrote:Even Soumil Shah is also not clear, see his answer, he is creating an object on the buffer pool. While objects are always created on the heap.

Oops, I guess i had typed it wrong; instead of String literal pool, i written string buffer.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Soumil Shah wrote:

Punit Singh wrote:Even Soumil Shah is also not clear, see his answer, he is creating an object on the buffer pool. While objects are always created on the heap.

Oops, I guess i had typed it wrong; instead of String literal pool, i written string buffer.



If you write String literal pool, then also it will be wrong to create an object in String literal pool.
 
reply
    Bookmark Topic Watch Topic
  • New Topic