I have to say that this was found by a mistake, as I was trying to convert a String to an Integer, but I didn't realize that it is a Character type instead of Integer.
But somehow, the Integer.valueOf can still run with a Character parameter although this was not mentioned in JDK documentation?
System.out.println(Integer.valueOf("2")); // >>> 2
System.out.println(Integer.valueOf('2')); // >>> 50
While it works fine for String argument of "2", it doesn't provide the expected result for Character argument of '2'. Why does this happen?
I am trying to understand if I miss anything? Why does it return 50 instead of 2?
Thanks
OK, I checked the ASCII table, it is returning 50 because the index for Character '2' is 50.
Integer.valueOf(int)charis a numeric type, despite the fact that it doesn't look like a numeric type.