Why does char to int casting works and not char to Integer in Java?

You're right that primitives are treated differently. The following would work.

You're right that primitives are treated differently. The following would work: (Integer)(int)input. CharAt(i); The difference is that when the argument is an int, (Integer) boxes the integer.It's not actually a cast even though it looks like it.

But if the argument is a char, then it would be a cast attempt; but primitives can't be cast to objects and therefore it doesn't work. What you can do is to first cast the char to int - this cast is okay since both are primitive types - and then the int can be boxed. Of course, char -> Integer boxing could have been made working.

"Why not? " is a good question. Probably there would have been little use for such feature, especially when the same function can be achieved by being a little bit more explicit.

(Should char -> Long work too, then? And char -> Short? Chars are 16-bit, so this would be most straightforward.

) Answer to edit: the advantage of wrapper classes is that wrapped primitives can be treated like objects: stored in a List, for example. List would not work, because int is not an URL2 maybe even more relevant question would be, what are primitive non-objects doing in an OO language? The answer is in performance: primitives are faster and take less memory.

The use case determines whether the convenience of objects or the performance of primitives is more important.

– gizgok Dec 16 at 9:12 @gizgok: There's no way to use primitives in collections because primitives aren't objects. That is, you can't construct a List, it doesn't compile. Primitive arrays are objects though, so a List would work, and yes, depending on what you do with it and how, it would be at least as or more performant as List.

– Joonas Pulakka Dec 16 at 9:45.

Because Integer is an Object. And char is not. You cant cast a non Object thing to some Object.

Infact you cannot cast some Object to any other class Object which is not in the hierarchy of that Object. Eg you cannot do this Integer g = (Integer) s; where s is object of String. Now why chat to int works, because every character is represented as unicode in java, so you can think of it as at backend char is a smaller version of int.(int is 32 bits and char is 16 bits).

True but I thought that auto boxing would have happened. – gizgok Dec 15 at 10:45 auto boxing is true only for ints. This means that you will not have to do explicitly casting when you are converting an int to Integer.

@gizgok – Zohaib Dec 15 at 10:49.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions