Are there any advantages/disadvantages between declaring a String without creating an object:
String str = "Hello World"
and creating an object:
String str = new String("Hello World")
?
Printable View
Are there any advantages/disadvantages between declaring a String without creating an object:
String str = "Hello World"
and creating an object:
String str = new String("Hello World")
?
Hi
In a Java program if you do the following :
String str = "One";
String str1 = "One";
There is only ONE actual String Object,
but there are two references pointing to this Object, where as if you do this :
String str = "One";
String str1 = new String("One");
There are two String Objects, because you
have created a "new String".
You can test this with the "==" operator,
if you test the first example, it will result
in true, whereas the second example will
result in false.
Whether this is an advantage or not depends
on how you use it.
Phill.