0

I have confusion in Number Wrapper Class in Java.

These two assignments look symmetric - a char is assigned to Integer, and an int is assigned to Character. However, the first assignment

Integer i = 'a';

gives Compilation Error, while the second assignment

Character ch2 = 97;

is allowed. Why?

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
Mathur
  • 71
  • 7
  • How do you expect the compiler to be able to convert `'a'` to an integer value? – ericbn Mar 31 '17 at 18:53
  • @ericbn [Like this](http://ideone.com/jvaWzh) Essentially, OP is using `'a'` as a different way of writing `97`. This question is surprisingly tricky. – Sergey Kalinichenko Mar 31 '17 at 19:15
  • @Sotirios Delimanolis This is not Duplicate. – Mathur Apr 03 '17 at 07:56
  • You haven't explained why you don't think so, so I'll leave it as is. Here are a couple more posts that ask about the exact same issue: [first](http://stackoverflow.com/questions/25294275/java-widening-conversions) and [second](http://stackoverflow.com/questions/24332413/java-auto-boxing-and-casting). – Sotirios Delimanolis Apr 03 '17 at 15:52

4 Answers4

5

Although int i = 'a' works fine, converting the same to Integer is not allowed, because it requires a boxing conversion.

Java's boxing conversion is defined only for eight cases:

  • From type boolean to type Boolean
  • From type byte to type Byte
  • From type short to type Short
  • From type char to type Character
  • From type int to type Integer
  • From type long to type Long
  • From type float to type Float
  • From type double to type Double

Since 'a' is a char literal, Java does not allow conversion from char to Integer: a character literal is always of type char.

However, when you write

Character ch2 = 97;

Java compiler sees that 97 is in the valid range for char (i.e. 0..65535), so it treats 97 as char, not int, and allows the boxing conversion. Trying the same with an out-of-range constant produces an error:

Character ch3 = 65536; // error: incompatible types: int cannot be converted to Character
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Oh those top 1% ers, always stealing our reputation ;-) ... but kudos, you figured it first ;-) ... may that blinken light be with you! If you allow the question: where is your nick name coming from? – GhostCat Mar 31 '17 at 19:34
  • @GhostCat It comes from [this pre-internet meme](https://en.wikipedia.org/wiki/Blinkenlights) in a slightly different "translation": "ACHTUNG! Das machine is nicht fur gefingerpoken und mittengrabben. Ist easy schnappen der springenwerk, blowenfusen und corkenpoppen mit spitzensparken. Ist nicht fur gewerken by das dummkopfen. Das rubbernecken sightseeren keepen hands in das pockets. Relaxen und vatch das blinkenlights!" – Sergey Kalinichenko Mar 31 '17 at 19:40
  • @GhostCat Just saw the country in your profile, and I hope that you don't find this meme too offensive. By the way, the cat in your profile (British shorthair?) looks very cool. – Sergey Kalinichenko Mar 31 '17 at 20:41
  • The language thing is absolutely fine. But calling my French Chartreux (or is it Chartreuse in English) a British shorthair... Woha, that is one big no go :-) – GhostCat Apr 01 '17 at 04:33
  • @GhostCat Oh my, I really sorry about that! In my defense, I read these two breeds are pretty hard to tell apart by looking at a picture. – Sergey Kalinichenko Apr 01 '17 at 10:44
  • I am glad you apologized in time. Those only duells can turn out pretty nasty,so it is good that we don't need to go there. :-) beyond that, Probably the best indicator for a Chartreux cat is the eye color btw. – GhostCat Apr 01 '17 at 10:51
4

The rules for boxing conversions can be found in the Java Language Spec, chapter 5.1.7

Boxing conversion converts expressions of primitive type to corresponding expressions of reference type. Specifically, the following nine conversions are called the boxing conversions:

... followed by a list of valid conversions from primitive types to reference types.

The point is: in any case, a conversion must take place.

If you had

int a = '97'

that is fine; as that is a widening conversion (sectin 5.12 in the JLS). But that case

Integer i = '97' 

isn't listed as "valid" conversion for Auto-boxing. In other words: the JLS doesn't allow for it; and this the compiler doesn't do it either. ...

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

Integer i = 'a' just wraps int i ='a'. Since 'a' is not an int it throws an error.

Likewise, Character ch2 = 97 wraps char ch2 = 97. However, 97 is an valid char! It represents the character "a". Example:

System.out.println((char) 97); //Output: a
Aaron N. Brock
  • 4,276
  • 2
  • 25
  • 43
0

This is because when setting a character to an integer it's returning the ASCII code number which is a primitive int.