Why char works in int casting and not char for Integer in Java

I ran into a problem when I ran into this.

(int)input.charAt(i) //works
(Integer)input.charAt(i) // Does not work
// input being a string

The first thought that I have is primitives, which are processed differently, and therefore this does not work. But then it’s hard for me to understand why they should have an Integer Wrapper class.

Edit: What are the benefits of having wrapper classes? Is it just to not have a primitive presence and be more OO in design? It’s hard for me to understand how useful they are. New doubt.

+5
source share
2 answers

You are right that primitives are handled differently. The following would be done:

(Integer)(int)input.charAt(i);

, int, (Integer) . , . char, ; , . , char int - , , int .

, charInteger . " ?" . , , , . ( charLong ?), charShort? Chars - 16 , .)

: - , : , List<Integer>. List<int> , int . , , - OO? : . , .

+7

Integer Object. char - . Non Object .

Infact, - Object, .

,

Integer g = (Integer) s;

s - String.

int , unicode java, char - int. (int 32 char - 16 )

+2

All Articles