• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

STRING objects

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

want to know bit about Strings

String s1 = new ("Java");
String s2 = "abc";


I think here total 4 objects will be created technically

2 of them for non-pool memory i.e. s1 and s2
2 of String pool i.e. java, abc ; which are the new values in pool ;

I am confused about it, would you help me out ?
Thanks in advance

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

Originally posted by chintan ramavat:
Hello ranchers

want to know bit about Strings

String s1 = new ("Java");
String s2 = "abc";


I think here total 4 objects will be created technically

2 of them for non-pool memory i.e. s1 and s2
2 of String pool i.e. java, abc ; which are the new values in pool ;

I am confused about it, would you help me out ?
Thanks in advance

- chintan



I would say there are 3 objects. s1 and s2 are references to Strings. But there is one object created by new if you include the word String, and then two String literals.
[ March 01, 2007: Message edited by: Keith Lynn ]
 
chintan ramavat
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lynn

so if i do,

String s1 = new String ("ABC");
String s2 = new String ("DEF"):

how many here will be created ?


String s1 = new ("Java");
String s2 = new ("def");

how many here will be created ?

- I think String references are maintained in non-pool so to me whenever we do String s there's one new reference to string , but still i am not clear about String pool literals or values ? can you explain me ?

Thanks lynn

- chintan
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anytime you use the new keyword, an object will be created. When you use a String literal, implicitly a String object is created, and any other String reference assigned to that literal will refer to that same object.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good one

http://www.javaranch.com/journal/200409/Journal200409.jsp#a1
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was about to send reply with the same URL...
This forum rocks !!
 
Ranch Hand
Posts: 355
Redhat Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by chintan ramavat:

String s1 = new ("Java");
String s2 = new ("def");


That's absolutely wrong Chintan! Please correct it. It can be,

Did you notice what you have missed? Alright, if you do not want to use "new", then it becomes

Hth,
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Neo Phesus:
I was about to send reply with the same URL...


Me too.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic