I am receiving a numeric value input parameter that is a String data type (something that I cannot change) and I need to check that this value received is of a certain number. I.e. Check if the value is 5.
Knowing that the received value of String data type, but at all times it will should contain only numbers. I would first convert the value into an integer data type, and then perform the equality test. I.e. Case B illustrated below.
Case A:
String expected = "3";
if(expected.equals(actual)) //...
Case B:
int expected = 3;
int actualInt = Integer.parseInt(actual);
if(expected == actualInt) //...
I would like to find out if there will be any cons in performing the equality test using Case B as I am more inclined to do it that way as a correct way out.
If you are only going to do equality check then I think case A is fine, since you are not doing any operations that will require you the number instead of a string.
Only cons I could see with case B, is that you would have to parse the number out of the string for the equality check.
But the advantage of case B is that if you needed to make sure that the string is actually an integer, in which case, you would have to go with case B.