Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Difference between s = "" and new String("a")

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wht is the difference in these two lines?
String s = "abc";
and
String s = new String("abc");
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think they both have the same result. In the first expression, you assign "abc" to s; and in the second expression, you instaniate a new object of String and assign s to "abc". String is one of the object type, but we don't need to instaniate it before we use it. Unlike other objects which we do need to instaniate, we can treat String as one of the data types.
 
Shirley Li
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay, let me clarify a bit. We can get away with
String s = "abc"; because the compiler will implicitly replace it with: String s = new String("abc");
hope this helps!
 
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Judy,
The difference is as follows:
String = "abc";
creates a String literal, which goes into the String pool.
String = new String("abc");
create a new String object.
If you have many String literals with the same value, they only get put in the String pool once. For example:
String s1 = "abc";
String s2 = "abc";
String s3 = "abc";
Only one String literal is created in the String pool. s1, s2 and s3 all refer to the same String literal. Hence,
s1 == s2 is true.
s2 == s3 is true.
s3 == s1 is true.
On the other hand,
String s1 = new String("abc");
String s2 = new String("abc");
String s3 = new String("abc");
Three unique String objects are created by the lines above. Hence,
s1 == s2 is false;
s2 == s3 is false;
s3 == s1 is false;
Hope this helps.
Stephanie
[This message has been edited by Stephanie Grasson (edited September 28, 2000).]
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thats a Wonderful answer given by Stephanie.

Originally posted by Stephanie Grasson:
Judy,
The difference is as follows:
String = "abc";
creates a String [b]literal
, which goes into the String pool.
String = new String("abc");
create a new String object.
If you have many String literals with the same value, they only get put in the String pool once. For example:
String s1 = "abc";
String s2 = "abc";
String s3 = "abc";
Only one String literal is created in the String pool. s1, s2 and s3 all refer to the same String literal. Hence,
s1 == s2 is true.
s2 == s3 is true.
s3 == s1 is true.
On the other hand,
String s1 = new String("abc");
String s2 = new String("abc");
String s3 = new String("abc");
Three unique String objects are created by the lines above. Hence,
s1 == s2 is false;
s2 == s3 is false;
s3 == s1 is false;
Hope this helps.
Stephanie
[This message has been edited by Stephanie Grasson (edited September 28, 2000).][/B]


 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Stephanie. It cleared a few cobwebs in my brain too.
Ambrose
 
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first set of comparisons is comparing variables, namely to compare their values, which are identical. Whereas the second set is comparing addresses where the values are stored.
The way I understand it, it's like someone owning a house, a condo, and a summer cottage. The person is the same when he/she is looked at as a person. Where he/she resides are three distinct properties.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent !!! Stephanie, I Hope you will keep helping us.
TWR
Jaydeep
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stephanie,
I'm left back there in the dust, a bit. Your explanation was very helpful, but I've not heard about the "string pool." Can you elaborate on this a bit?
Thanks in advance!
Allen
 
Stephanie Grasson
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Allen,
You asked about the string pool.
According to the Java Language Specification:


A pool of strings, initially empty, is maintained privately by the class String.
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals method (�20.12.9), then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.
All literal strings and string-valued constant expressions are interned (�3.10.5).


Basically, the string pool is a way for Java to conserve memory by using many references to the same string object (as opposed to having many unique string objects all with the same contents).
I hope this clears things up for you. If not, please let me know.
Stephanie
 
Allen Alchian
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stephanie,
I'm following you on the string pool concept, but now I have other confusion.
Let's go back to the original question...the difference between
String s = "abc";
String s = new "abc";
You responded with a statement that...
>String = "abc";
>creates a String literal, which goes into the String pool.
I assumed you simply mistyped the above, and meant it to read...
>String s = "abc";
instead of
>String = "abc";
I had always understood that either of the original statements created a reference to a String object...no difference between them. If the string literal "abc" doesn't currently exist, then a new reference, s, will be created and it will reference that String object. either of the following two statements will accomplish the task...
String s = "abc";
String s = new "abc";
In other words, the 'new' operator is called implicitly in the first statement, and explicitly in the second. Either statement will create a new String object provided that just one of the two statements are used. The issue of the string pool comes into play when a second reference is created to the same string, "abc." Is this not correct?
Thanks again,
Allen
 
Stephanie Grasson
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Allen,
Thanks for catching my typo! I definitely meant
String s = "abc";
Sorry if I caused anyone confusion.
However, I'm not sure I understand the last part of your post:


Let's go back to the original question...the difference between
String s = "abc";
String s = new "abc";
...
Either statement will create a new String object provided that just one of the two statements are used. The issue of the string pool comes into play when a second reference is created to the same string, "abc." Is this not correct?


You say that either statement will create a new String object provided that just one of the two statements is used. It is my understanding that
String s = new "abc";
will always try to create a new String object, regardless of whether or not a literal with the same value already exists in the pool.
On the other hand,
String s = "abc";
will only try to create a new String object if a literal with that value does not already exist in the pool. If a literal with the same value already is in the pool, no new object is created, but rather a reference to the literal in the pool is returned.
So, for example, say that the literal "test" is not in the pool.
// new String added to pool
String s1 = "test";
// new String created, not added to pool
String s2 = new String("test");
// another new String created, not added to pool
String s3 = new String("test");
// no new String created, reference to String in pool returned
String s4 = "test";
Again, I'm not sure I fully understood your post, so please let me know if this is not what you meant.
Stephanie
[This message has been edited by Stephanie Grasson (edited October 18, 2000).]
 
Allen Alchian
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay Stephanie,
My basic problem was that I kept trying to put the reference to a String in the string pool, instead of the string itself. I see where I was off target.
Since there is the subtle difference between the two statements...
String s = new "abc";
String s = "abc";
...there must be some reason for having the two statments handled somewhat differently.
It seems to me that using the statment
String s = "abc";
is somewhat more risky because the programmer may not know if "abc" is going to be in the String pool. Is this an accurate appraisal?
And many thanks for your patience in walking me through this.
Allen
 
Stephanie Grasson
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Allen,
According to Java2 for Professional Developers (by Michael Morgan),


Starting in SDK 1.2, constant strings are shared between classes, reducing the memory needs of all classes. Since Java Strings are immutable, you don't need to worry about some other class changing your class's String.


So my guess would be that (unless you have a specific reason not to) the safe route is usually to use String literals because:
(1) it conserves memory, so less chance of out-of-memory exceptions.
(2) Strings are immutable.
You posted:


It seems to me that using the statment
String s = "abc";
is somewhat more risky because the programmer may not know if "abc" is going to be in the String pool. Is this an accurate appraisal?


I'm pretty sure that having the statement
String s = "abc";
in your program means that "abc" will be in the String pool.
Hope this helps.
Stephanie
 
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