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

  • Java
  • Thread starter estro
  • Start date
  • Tags
    Java Test
In summary, the main difference between String a = new String("test") and String b = "test" is that the former creates a new string instance while the latter references an interned string instance. The former approach will result in different instances even if the strings themselves are equal, while the latter will reference the same instance for multiple uses of the same literal. In most cases, it is recommended to use the latter approach for initializing strings from string literals in Java.
  • #1
estro
241
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
  • #2


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()
 
  • #3


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!
 

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

1. What is the difference between "Java String a = new test" and "Java String b = test"?

The main difference between these two statements is the use of the keyword "new". In the first statement, the "new" keyword is used to create a new instance of the string class and assign it to the variable "a". In the second statement, the variable "b" is simply assigned the value "test" without creating a new instance of the string class.

2. Which statement is more efficient in terms of memory usage?

In terms of memory usage, both statements are essentially the same. The only difference is that the first statement takes up slightly more memory since it creates a new instance of the string class. However, the difference in memory usage is negligible and should not impact performance.

3. Can I use either statement interchangeably?

Yes, in most cases, you can use either statement interchangeably. However, there may be certain situations where you may need to use the "new" keyword, such as when creating a new instance of a custom class or when using certain methods that require a new instance of the string class.

4. Is there a difference in terms of functionality between the two statements?

No, there is no difference in functionality between the two statements. Both statements will create a new string variable and assign it the value "test".

5. Which statement should I use for better coding practices?

In terms of coding practices, it is generally recommended to use the second statement, "Java String b = test". This is because it is more concise and does not unnecessarily create a new instance of the string class. However, in certain situations, such as the ones mentioned in question 3, using the "new" keyword may be necessary. Ultimately, it is up to the programmer's discretion and the specific needs of the code.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
2
Replies
39
Views
6K
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
19
Views
1K
  • Programming and Computer Science
Replies
3
Views
871
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top