Java Java [String a = new test ] vs [String b = test ]

  • Thread starter Thread starter estro
  • Start date Start date
  • Tags Tags
    Java Test
AI Thread Summary
In Java, using `String a = new String("test")` creates a new string instance, meaning that two strings initialized this way will not be the same object in memory, even if their values are equal. In contrast, `String b = "test"` references an interned string, where identical string literals point to the same instance, allowing for memory efficiency. Thus, `String a = "test";` and `String b = "test";` will result in both variables referencing the same object. It is generally recommended to use string literals for initialization to take advantage of this interning feature, which conserves memory. Overall, understanding these differences is crucial for effective string management in Java.
estro
Messages
239
Reaction score
0
Java [String a = new "test"] vs [String b = "test"]

Can you tell me what is the difference between the two?
What is going on behind the scenes?
 
Technology news on Phys.org


Assuming that you meant String("test") in the first case, it will create a new string instance. Thus, if you have two such strings they would always be different instances even if the strings themselves are equal, that is

Code:
String a = new String("test");
String b = new String("test");
assert a != b;
assert a.equals(b);

In the second case you are referencing a so-called interned string instance (see [1] and [2]). This means that String a = "test"; is equivalent to String a = new String("test").intern(); which means that if you have two equal text literals (or other interned strings) they will reference the same instance, that is

Code:
String a = "test";
String b = "test";
assert a == b;
assert a.equals(b);

In most circumstances you will want to use the later approach to initialize strings from string literals in Java as multiple uses of the same literal will result in only one string instance being used.

[1] http://en.wikipedia.org/wiki/String_interning
[2] http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#intern()
 


Filip Larsen said:
Assuming that you meant String("test") in the first case, it will create a new string instance. Thus, if you have two such strings they would always be different instances even if the strings themselves are equal, that is

Code:
String a = new String("test");
String b = new String("test");
assert a != b;
assert a.equals(b);

In the second case you are referencing a so-called interned string instance (see [1] and [2]). This means that String a = "test"; is equivalent to String a = new String("test").intern(); which means that if you have two equal text literals (or other interned strings) they will reference the same instance, that is

Code:
String a = "test";
String b = "test";
assert a == b;
assert a.equals(b);

In most circumstances you will want to use the later approach to initialize strings from string literals in Java as multiple uses of the same literal will result in only one string instance being used.

[1] http://en.wikipedia.org/wiki/String_interning
[2] http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#intern()


Thanks for the detailed answer!
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top