• 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 class

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ,
will the following statement produce two String objects??
String s=new String("hi");
 
Ranch Hand
Posts: 180
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no it creates only one object
with s referring to that object
 
kumar reddi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The preceding construct is equivalent to, but more efficient than, this one, which ends up creating two identical strings:
String s = new String("Hola Mundo"); //don't do this

this is one of line which i have read from sun.java material
here two identical strings means it will create two string objects
and also they have commented like dont do this ....
 
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
A double-quoted run of characters like "hi" is a String object. It's loaded out of your class file as a full-fledged String object. By using new String("hi"), you're making a copy of this String; that's why there are two. Since Java Strings are immutable -- meaning that once created, they can never change -- there's really no reason to ever use this constructor, so don't. The proper thing to do is just

String s = "hi";
 
kumar reddi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you all
 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ernest just a simple doubt.
If two objects are getting created ,i.e. :
//"hi" and
//new String("hi")

There must be two ways of accessing these two different objects as they must be stored in two different in the memory.Please tell me those.

And what is the use of creating two objects while doing new String("hi"),I mean the String constructor could have been optimized to create only one object.
I think something is missing .
Please tell me.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Raj Kumar Bindal:
Ernest just a simple doubt.
If two objects are getting created ,i.e. :
//"hi" and
//new String("hi")

There must be two ways of accessing these two different objects as they must be stored in two different in the memory.Please tell me those.

And what is the use of creating two objects while doing new String("hi"),I mean the String constructor could have been optimized to create only one object.
I think something is missing .
Please tell me.



will give you access to the String literal.
If you just type then there is no way to access that object. You have to assign it to a reference variable when you create it.


As far as the optimiztion is concerned : what if you wanted to create a new String object with the value "hi" ? Admittedly, there aren't many places where it would be useful, but at least you have the option. If you want to avoid creating two objects, then use
 
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
first, a minor point. there is no string class - it's the String class.

You may want to search for something called the "string pool". In Java, literal strings are created in the string pool. No matter how many times you see "Hi" in the code, there is one special string literal in the pool for that value.

each time you see the word "new" in the code, a new object is being created.

so, if you use String s = new String("Hi");, you put the string "Hi" in the string pool, and use THAT string to create a new String object.
 
reply
    Bookmark Topic Watch Topic
  • New Topic