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 difference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String difference" Watch "String difference" New topic
Author

String difference

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
public class StaticTest {

public static void main(String[] args) {

String str1="HI";
String str2 = new String("HELLO");

}

}

Hi ,
I would like to know the difference b/w str1 & str2 if any ...
( if both r reference then where is the object of str1, I mean without new operator how can we allocate memory in heap )
Thanks .
Sanjaya Sugiarto
Ranch Hand

Joined: Mar 25, 2004
Posts: 229
The first one: it creates one String object and one reference variable.

The secone one: it creates two String objects and one reference variable (String and literal objects and str2 refers to the String object).


<a href="http://www.wi.hs-furtwangen.de" target="_blank" rel="nofollow">Business Information Technology - Hochschule Furtwangen University, Germany</a>
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
What do u mean by literal object .
what is the use of that .
Every time when we create any object the literal object gets created .
Please help ?
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Sanjaya is referring to the String literal pool.

Consider the statement "String str2 = new String("HELLO");". The Java compiler sets up every String literal like "HELLO" in your source program in a special memory area called the "String literal pool". If your program used the literal "HELLO" somewhere else, there would still be only one String like this in the pool. The literal "HELLO" is supplied as an argument to the String() constructor and a new object is created at runtime. A reference to that new object is then stored in reference variable str2. So there are two objects, one in the String literal pool and another one in the same heap where all other objects are kept.

Now consider "String str1="HI";". Here, "HI" is in the String literal pool. A reference to "HI" in the pool is stored in str1. No other object is created.


Mike Gershman
SCJP 1.4, SCWCD in process
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Thanks Mike It was very good explanation .
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
It means it is always better to make String Object ( Actually reference )
like this :
String s = HI";
instead of :
String s = new String("HELLO");
because it will save the memory in heap ( no object in heap , just one object in literal pool ) .
Am I right ?

thanks
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Are your Strings big enough to matter in a modern computer with megabytes or gigabytes of memory?

Do you mind giving up all hope of garbage collecting the Strings when you are done with them?

Does it matter to you that identical Strings in the String Literal Pool use the same memory while identical String objects on the heap each use separate memory?

Now you decide.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Whatever is the Case ....
Saving is always better ....
Is there any loss with this in particular :
String s = "HI"

Reply, Thanks .
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

I think Mike was making the point: "Why does it matter?".


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Is there any loss with this in particular :
String s = "HI"


String s = "HI";
puts one sharable string in the String Literal Pool
String s = new String("HI");
puts a sharable string in the String Literal Pool and an unshared string on the heap. So the first form is better if your main issue is minimizing peak memory usage.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String difference