Luke Vo
Luke Vo

Reputation: 20748

Strange String comparation

I don't know why the comparation of CellType to "Hamster" is false. Wonder why it happened!

enter image description here

They are exactly the same, even in case sensitive.

Please help me.

Upvotes: 0

Views: 238

Answers (4)

Edwin Buck
Edwin Buck

Reputation: 70949

You need to use

if (CellType.equals("Hamster")) {
  ...
}

The other comparison checks to see if they are the same string Object, not the same string by value.

It is also a good time to lookup the differences between reference equality and Object equality.

Upvotes: 4

gmhk
gmhk

Reputation: 15960

CellType.equals("Hamster")
or
CellType.equalsIgnoreCase("Hamster")

Use the above formats, it will be taken care

Upvotes: 0

Adithya Surampudi
Adithya Surampudi

Reputation: 4454

Please use

 CellType.equals("Hamster");

If you want to ignore case then use,

 CellType.equalsIgnoreCase("Hamster");

Upvotes: 0

unholysampler
unholysampler

Reputation: 17341

== is the identity comparison operator (same object). You should use equals() when you want to compare equivalence.

Upvotes: 0

Related Questions