4

If I declare a String as

String test=new String("testing");

and

String test1="testing1"

Since String is a class in JAVA how does test1 be a String Object without using a new Operator.Also,when a new Operator is used memory is assigned for new String("testing") so in the case of test1 how is the memory assigned? Also,when the string is interned ,if two Strings have the same value with what reference is the String store once in the String intern pool?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Nirali
  • 55
  • 5

4 Answers4

2

Let us first consider this String test=new String("testing");

  • It creates an String Object in Heap.No Checking is done in String Pool for existence of this String in the pool.

and now this String test1="testing1"

  • It creates String a String Object in String Pool not in Heap.Before Creation check is done whether this string is already there in the pool.If yes its reference is returned else a new String is created in the pool and its reference is returned.Basically this is a String Literal, which is put in Constant pool for memory optimization and re-usability.

intern(): It is used when you construct an object using new() and you call intern() on that object then first a check is done in Stirng pool if that String already exists there or not,if yes it is directly used

Algorithmist
  • 6,657
  • 7
  • 35
  • 49
  • Note however that the String `"testing"` in `new String("testing")` will be interned. – Sotirios Delimanolis Aug 11 '13 at 13:47
  • @SotiriosDelimanolis Exactly 10x for the info. For that we need to do something like this new String("testing").intern(); . – Algorithmist Aug 11 '13 at 13:48
  • So is it a special case only for String that we can declare a String object without using a new Operator? – Nirali Aug 11 '13 at 14:00
  • For all the Wrapper Classes also you can create objects without using new say Integer x=4 also works. – Algorithmist Aug 11 '13 at 14:26
  • The execution of the line `String test1="testing"` creates no strings at all. The whole description is false. – Marko Topolnik Aug 11 '13 at 14:51
  • @SotiriosDelimanolis No, `"testing"` will not be interned. It has existed in the class's constant pool ever since the class was loaded. – Marko Topolnik Aug 11 '13 at 14:52
  • @MarkoTopolnik what you mean? – Algorithmist Aug 11 '13 at 15:01
  • @Algorithmist You must be more specific in your request for clarification. – Marko Topolnik Aug 11 '13 at 15:02
  • @MarkoTopolnik What I meant is that `test1 == test.intern()` would return true. What is the term to describe that? – Sotirios Delimanolis Aug 11 '13 at 23:42
  • @SotiriosDelimanolis Exactly,only if we do intern() it would return true,else it would return false. – Algorithmist Aug 12 '13 at 03:16
  • @SotiriosDelimanolis No term describes that. The `test.intern()` we are talking about does not intern the string because it is already interned. "Interned" means "enrolled in the constant pool", and all string literals, as I said, are enrolled at the time of class loading, *not* at the time of first access. They may not be accessed at all and they will still exist in the pool. – Marko Topolnik Aug 12 '13 at 06:07
  • @MarkoTopolnik you are confusing the things a lot.Please read my answer carefully and then can you please let us know all the negatives that you see in the answer? – Algorithmist Aug 12 '13 at 09:29
  • @Algorithmist I have read your answer carefully and can ascertain that your description below `String test1="testing1"` is dead false. – Marko Topolnik Aug 12 '13 at 09:31
  • @MarkoTopolnik Let us do one thing can you please post a Question containing the description which I have written and let the community decide whether it is correct or not? – Algorithmist Aug 12 '13 at 09:33
  • No, thanks. But you can post it, if you're intersted. Maybe I even come to answer :) If you turn out to be right, I'll come back here and publicly apologize. – Marko Topolnik Aug 12 '13 at 10:09
  • In the meantime, you can [read this answer](http://stackoverflow.com/a/8046106/1103872) by Thilo. – Marko Topolnik Aug 12 '13 at 10:25
  • 1
    @MarkoTopolnik I guess how I said things was confusing. I meant the String literal in the String constructor call will have been put in the pool. Calling `intern()` on the reference returned by that constructor call will return the same reference as that assigned by the direct string literal. In the question, if we did `test1 == test.intern()` after the two assignments, the test would produce `true`. – Sotirios Delimanolis Aug 12 '13 at 12:19
  • @SotiriosDelimanolis I think this is also what I have written in my answer. – Algorithmist Aug 12 '13 at 12:44
1

Java has a separate memory for Strings that are created without calling the constructor with new. Every time such a String is created Java checks if that String is already in this memory. If it is, then Java sets the same reference to the new String until one of them changes.

When you create a String with the constructor using new then it behaves as a normal object in Java.

Take a look at this example:

String s1 = "Test";
String s2 = "Test";

When you compare this String with the == operator it will return true. s1.equals(s2) will also return true.

It looks different if you create String objects with the constructor like this:

String s1 = new String("Test");
String s2 = new String("Test");

When you now compare this Strings with the == operator it will return false, because the reference of this strings is now different (you created 2 unique String objects). But if you use s1.equals(s2) it will return true as expected.

Daniel K
  • 158
  • 5
  • So is it a special case only for String that we can declare a String object without using a new Operator? – Nirali Aug 11 '13 at 14:25
0

When you are using

String test1="testing1"

then it means you are storing only one copy of each distinct string value

but

String test=new String("testing");

gives you a new string object.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

Consider your second assignment was:

String1 test1 = System.getenv("PATH");

Here, test1 is most probably also a reference to a String object, without using new(). You can assign references to already existing objects to new variables. So where is the problem?

The problem is, you must not use sloppy wording like "test1 is a String object". It is not. It is a reference to a String object or null. That's all about it.

Ingo
  • 36,037
  • 5
  • 53
  • 100