• Post Reply Bookmark Topic Watch Topic
  • New Topic

comparing char in if that i convert to string see code thanks  RSS feed

 
chrstian ferma
Ranch Hand
Posts: 72
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what will i compare in if statemet is the 1st letter of each if i have code="a" and name="Angelina" first letter of each is "a" and "A" then in convert it to string so that i can make it uppercase but when i compare it in if statement it always go into "not x" but the ouput that im getting is x=A y=A then it always direct me into else statement , thank you

 
chrstian ferma
Ranch Hand
Posts: 72
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i already solve it i just tried something and it works but im not fully understand on what i change cause it seems like for me it's the same what i did was change if(a==b) into if (a.equals(b)) thank you

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should never use the == operator on reference types (Strings).

What never?
Well, hardly ever.
The link should explain why your equality tests failed first time.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The == operator compares the value of the expression on the right to the value of the variable on the left.

When you are comparing primitives (ints, chars, booleans etc) then the == operator will do what you want and can be used in things like if-statements.

When comparing object references the == operator will only return true if the references both hold the same value, so both references point to the same object. It will not compare the content of the objects, so if you compare two different Strings it will always return false even if those Strings contain the same letters.

The .equals(Object other) method is intended to be used for logical comparison. It should return true if both objects are equivalent even if they are not the same object. For Strings this method will return true if they both have the same sequence of characters.

Beware that the default implementation of equals(...) acts in the same way as ==. The String class has overridden it to behave in a sensible way for Strings.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ha, if I'd have known about that link I could have saved myself some typing.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thikn the thread title gives us a hint towards the problem; it sady “char”. This suggests you (OP) are confusing the primitives, e.g. chars with single‑character Strings, which are totally different.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!