mercredi 27 mai 2020

If (x == "0") issue [duplicate]

I have a string variable where the value is often "0" it is also often not "0".

Whenever I update this value, it needs to be parsed. I know that this string, when it is zero it cannot be parsed.

However I do not understand why the if statement comparing two strings isn't working, there must be some logic behind it that I do not understand and I would appreciate it if someone could explain what I'm missing.

I have solved the issue by using .equals("0"), but I have no idea why this method works and the other one doesn't. Thank you in advance.

if (x == "0") {
  integer = 0;
} else {
  integer = Integer.parseInt(x);
}

Aucun commentaire:

Enregistrer un commentaire