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

  • Context: Java 
  • Thread starter Thread starter estro
  • Start date Start date
  • Tags Tags
    Java Test
Click For Summary
SUMMARY

The discussion clarifies the difference between creating strings in Java using new String("test") and using string literals like "test". The former creates a new string instance each time, resulting in different memory references even for equal strings, while the latter utilizes string interning, allowing multiple references to the same instance for identical literals. This leads to more efficient memory usage. Developers are advised to prefer string literals for initialization to optimize performance.

PREREQUISITES
  • Understanding of Java string handling
  • Familiarity with Java memory management concepts
  • Knowledge of string interning in Java
  • Basic proficiency in Java programming
NEXT STEPS
  • Research Java String interning and its benefits
  • Learn about memory management in Java, focusing on object creation
  • Explore the performance implications of string handling in Java applications
  • Study best practices for string manipulation in Java
USEFUL FOR

Java developers, software engineers, and anyone interested in optimizing string handling and memory usage in Java applications.

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!
 

Similar threads

Replies
8
Views
3K
  • · Replies 39 ·
2
Replies
39
Views
8K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 4 ·
Replies
4
Views
4K
Replies
55
Views
7K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K