|
-
October 3rd, 2000, 06:08 PM
#1
Basic String question
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")
?
-
October 4th, 2000, 08:07 AM
#2
Re: Basic String question
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|