3

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.

2
  • 1
    A char is an unsigned 16 bit integer type. '2' is simply another way of specifying the integer 50. '2'==50 will return true. '2' is simply promoted to an int for Integer.valueOf(int) Commented Jul 5, 2019 at 2:57
  • A char is a numeric type, despite the fact that it doesn't look like a numeric type. Commented Feb 3, 2021 at 8:39

2 Answers 2

5

The ASCII representation should give you a clue about why you are getting this behaviour.

Remember that the Integer and Character classes are wrappers for the int and char primitive types. The reason why Integer.valueOf() works with a Character is that there is an implicit cast to Integer. When you cast a Character to Integer, the Integer will hold the ASCII code for that Character. The same thing happens with primitive types. For example

 Character yourChar = new Character('2');
 Integer yourInt = Integer.valueOf(yourChar);    //Implicit casting

 char biz = '2';
 int baz = (int)biz;                            //Explicit casting to int

 System.out.println(baz);
 System.out.println(yourInt);

Here, the value of both yourInt and baz will be 50, because that is the ASCII code for '2'. The implicit and explicit casting do the same thing.

If you actually want to convert a char which holds a digit to the correct integer, use Character.getNumericValue(). E.g.

 char myChar = '2';
 int myInt = Character.getNumericValue(myChar);

Now myInt holds the value 2.

As far as best-practices go, using this type of implicit conversion isn't a good idea. It doesn't make it obvious what you are trying to do.

Sign up to request clarification or add additional context in comments.

Comments

1

Integer.valueOf(char)) gives the ASCII value.

Integer.valueOf('2');  // >>> 50

If you want to get numeric value then,

Character.getNumericValue('2'));  // >>> 2

5 Comments

You are right, but I am asking why does java allow Integer.valueOf(Character)? This was not written in JDK documentation. docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/…
Well, Number 2 is passed as a Character not as a int. So that Integer.valueOf return the ASCII value of character '2'.
@JamesKPolk, char or Character really matter here ?
@Rowi: I'm not sure, maybe not with auto-unboxing.
@Code_Control_jxie0755 You are right, but I am asking why does java allow Integer.valueOf(Character) It's calling Integer.valueOf(int) because of widening primitive conversion (JLS-5.1.2 link) (char is widened to int). As for why that is allowed, 'a' + 1 == 'b' (which I hope helps).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.