3

Consider statement:

String s=new String("abc");

Will this statement creates two String objects namely "abc" and the one represented by 's'?

and if it creates two objects then will "abc" get stored in String pool or just discarded?

EDIT:

i am asking this question in reference to Difference between string object and string literal, where in the last two answers , creation of two objects is denied.

Community
  • 1
  • 1
a Learner
  • 4,944
  • 10
  • 53
  • 89
  • Well, what happened when you tried it? (Look up "Java string ==" questions as most of the [better] answers ought to discuss `new String` as well. Note that a string literal is a string literal. It doesn't matter what "uses" it.) –  Jun 23 '12 at 06:54

2 Answers2

5

Avoid such kind of behavior , because "abc" is already a String and by making a new String, you are creating an unnecessary Object.

Instead go for String s = "abc";

This way, the String gets interned by the JVM and is added to a pool.

To answer your question, you are just creating an Object s that is referring to "abc".

So when you do say String t = new String("abc"); and then do s==t, will yield in false. Because they have their separate instances to abc.

Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
  • 1
    So u mean `String s=new String("abc");` will create only one object 's' but not two objects-'s' and "abc"? – a Learner Jun 23 '12 at 07:04
  • @aLearner When you say `"abc"`, it is already `String`(`Object`). Get the point now? – Kazekage Gaara Jun 23 '12 at 07:06
  • i think u didn't get my question. "abc" always represents a String object and would be stored in String pool. Thats OK. But i am asking that will above statement also store a new object "abc" in String pool if it not there already,apart from creating 's'. – a Learner Jun 23 '12 at 07:15
  • @aLearner there would be 2 objects, whereas `String s="abc";` only creates one object. In both cases the litteral "abc" is interned in the string pool. – assylias Jun 23 '12 at 07:15
  • 1
    @aLearner If `"abc"` is not there already, a new `String` will be created and placed in the pool. If it is already there, the `String s = new String("abc");` just creates one `Object` ,`s`. – Kazekage Gaara Jun 23 '12 at 07:24
  • @aLearner: The code you describe will create two objects. But that's a dumb thing to do, is the answer's point. – Louis Wasserman Jun 23 '12 at 08:35
  • @KazekageGaara you can override equals method and then s==t can return true. – Simon Dorociak Jun 23 '12 at 09:28
0

String s = "HELLO";

Here "s" is a object reference variable of type String, which refers to the String literal object "Hello" which is added to the String Literal Pool.

String t = new String("Hello");

Here t is a object reference variable of type String, which refers to the String object "Hello" which is added to the String Pool.

Difference Between String Literal and String :

Assume

String s = "Hello";

String t = new String("Hello");

Now if following changes are done:

s = null;

t = null;

Hello String object associated with t will be a candidate for Garbage Collector, But Hello String Literal associated with s will NOT BE A CANDIDATE for Garbage Collector, as there will ALWAYS BE A REFERENCE FROM STRING LITERAL POOL to it.

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • `Here t is a object reference variable of type String, which refers to the String object "Hello" which is added to the String Pool.`--I think `t` will not point to a string object in the pool as string created by `new` never points to string in pool unless explicitly interned by intern() – a Learner Jun 23 '12 at 12:31