Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can somebody help me? i don't understand. 1

Status
Not open for further replies.

UGrad

Programmer
Oct 15, 2002
40
CA
Hi,
I have a question. why is d equals to TRUE and e equals to FALSE??

boolean d = "abc" == "abc";

boolean e = "a" == "a";
 
It makes no sense to me. Two identical things MUST be equivalent; it's a tautology.

e should be TRUE.

Why you're getting FALSE is a mystery to me.

Sorry I can't help,

Greg _______________________________________
Constructed from 100% recycled electrons
 
Bizaar ... however you do realise that this is not comparing 'String equality' - it is comparing if the objects are equal. 'String equality' is done thus
Code:
 boolean a = StringOne.equals(StringTwo);


Maybe this has something to do with it ?!!

Ben
 
Oh man, I totally forgot about that. So, if his statement is comparing two objects, what condition has to be met for the statrement to be true?

Do they have to be the same object? If so, then his two String literals are not the same object, even though they have the same literal value. This would explain it. I'll look this up, but don't have itme right now. _______________________________________
Constructed from 100% recycled electrons
 
Technically, they should still be true. Strings are highly weird when checking equality, and it depends how the String was made and how you check for equality.

Example 1 "String literal":
Code:
    String someString1 = "MarsChelios";
    boolean equals1 = (someString1 == "MarsChelios");
    boolean equals2 = someString1.equals ("MarsChelios");

Result: equals1 is true, equals2 is true
Reason: When String literals are defined, they are stored in a String Table somewhere. Any other String literal will equal true when compared to it either way because for all intents and purposes, they are the same object.

Example 2 "Instantiated Strings":
Code:
     String someString1 = new String ("MarsChelios");
     boolean equals1 = (someString1 == "MarsChelios");
     boolean equals2 = someString1.equals ("MarsChelios");

Result: equals1 is false, equals2 is true
Reason: When Objects are instantiated, they are given a unique id, their refernce id. This is how the equality operator
Code:
==
check equality of objects. The instantiated
Code:
String
has a different id than the String literal, and so fails the equality operator check.
However, the
Code:
equals ()
method of
Code:
String
checks the chracters in a String for equality, so the check passes here.

I've tried the posted code under JDK 1.4 and it works as expected, both are true. I would suggest reinstalling the JDK you're using if you continue to get the results you've posted.

Let me know if you need anymore clarification.

Hope this helps,
MarsChelios


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top