Results 1 to 10 of 10
Thread: What is the different ?
- 12-19-2007, 02:01 PM #1
- 12-19-2007, 02:40 PM #2
Nothing different. Just syntax is different. 2 is the short hand version of 1 but does the same thing.
- 12-19-2007, 03:02 PM #3public static void main(String[] args)
{
String a = "java String1";
String b="java String1";
if(a == b)
System.out.println("Both Java Strings are equal.");
else
System.out.println("Java Strings are not equal.");
if(a.equals(b))
System.out.println("Both Java Strings are equal.");
else
System.out.println("Java Strings are not equal.");
}
}
Both Java Strings are equal.
Both Java Strings are equal.
if write this pattern
public static void main(String[] args)
{
String a = new String("java String1");
String b=new String("java String1");
if(a == b)
System.out.println("Both Java Strings are equal.");
else
System.out.println("Java Strings are not equal.");
if(a.equals(b))
System.out.println("Both Java Strings are equal.");
else
System.out.println("Java Strings are not equal.");
}
Java Strings are not equal.
Both Java Strings are equal.
any body help me ? what is the reason out put is differ?
- 12-19-2007, 06:29 PM #4
For comparing strings, you should use equals method.
If you use to compare two objects, their referenced will be compared which means their addresses are compared.
And my interpretation about your question is that it is the result of implementation difference. The JDK you are using, looks like cheking previous strings before allocating a new object when you use the String s = "aaa" syntax. and if it finds an existing string with that content, it returs that strings reference instead of creating a new object and allocate memory for it. This should be to have a faster implementation. But it creates a new object when it sees the second syntax because you explicitly request to have a new string object with that syntax.
- 12-19-2007, 06:49 PM #5
thanks a lot
- 12-31-2007, 04:11 AM #6
I'm not entirely sure this is correct JavaBean. sivasayanth, also here's the reason why
Java Code:String a = new String("Java String");
Java Code:String a = "Java String";
- 12-31-2007, 04:46 AM #7
Member
- Join Date
- Dec 2007
- Posts
- 12
- Rep Power
- 0
No. There is no such thing in Java. All objects are on the heap.
The two things shown above are essentially the same. Except for a minor technicality with strings. String maintains an internal pool of strings. All string literals are automatically interned. (See String.intern()) So when you have a string literal in the code, it implicitly calls String.intern(). As a result, if you have a string literal that has the same content as another string literal that was evaluated earlier, it will evaluate to a String reference that references the same String object as the other one.
When you do "new String(...)", it creates a new String object that is not the same as any other object, so it is guaranteed not to be == to another object.
That is one reason you should not use == to compare strings.
You can manually intern strings with String.intern() if you really need to use == to compare strings.
- 12-31-2007, 06:21 AM #8
Excuse me? I believe you have a misunderstanding. There are, absolutely, local variables in Java, and
Java Code:String a = "Java String";
When you do "new String(...)", it creates a new String object that is not the same as any other object, so it is guaranteed not to be == to another object.Last edited by CaptainMorgan; 12-31-2007 at 06:24 AM.
- 12-31-2007, 06:50 AM #9
Member
- Join Date
- Dec 2007
- Posts
- 12
- Rep Power
- 0
Java Code:String a = new String("Java String"); String b = "Java String";
- 12-31-2007, 06:56 AM #10
Bookmarks