File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes string class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "string class" Watch "string class" New topic
Author

string class

kumar reddi
Greenhorn

Joined: Apr 23, 2007
Posts: 8
hi ,
will the following statement produce two String objects??
String s=new String("hi");
saurav sarkar
Ranch Hand

Joined: Jan 07, 2007
Posts: 180

no it creates only one object
with s referring to that object


Be Objectively Oriented.Explore the power of OOPs.
My Blog, Eclipse EMF Query committer.
kumar reddi
Greenhorn

Joined: Apr 23, 2007
Posts: 8
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 ....
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

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";


[Jess in Action][AskingGoodQuestions]
kumar reddi
Greenhorn

Joined: Apr 23, 2007
Posts: 8
thank you all
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
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.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3539
    
  15
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


Joanne
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11312
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: string class