0

why we can assign both int value and a char value to Character Wrapper type. Autoboxing means boxing for the corresponding wrapper but Character is not the corresponding wrapper of int. It is Integer

why both of these statements are possible

Character character = 'a';
Character character2 = 3;
RoHaN
  • 1,356
  • 2
  • 11
  • 21
  • Without looking up the Java Language Spec (which I recommend as the primary source of information on such questions) I would guess that there is an implicit conversion from int to char. You can write `char c = 3`, too. – GhostCat Jun 10 '15 at 06:21
  • Because char is an integral type. char b = 'a'+1; – Elliott Frisch Jun 10 '15 at 06:24
  • Why you can assign a value up to 65535 to a Wrapper Character class. what are all 65535 represent ?? – RoHaN Jun 10 '15 at 06:35

3 Answers3

0

It is treated as an ASCII value, if you assign int value to Character.

Below 4 approach result in same output.

Character character2 = 'e';

Character character2 = 101;

int i = 101;
Character character2 = (char)i; // casting int to char i.e. treat it as ASCII value

Character character2 = (char)101;  

System.out.println(character2); // Prints e

Note: You can refer this ASCII Table

Naman Gala
  • 4,670
  • 1
  • 21
  • 55
  • you can assign a combined value of 32,767 (lower-) + 32,768(upper+) which is 65535. so you can assign something like this Character character =65535; then what why are all the 65535 values for – RoHaN Jun 10 '15 at 06:34
  • Added the link into the answer. 65535 will result in `?` when you print it. As it is not assigned to any character. – Naman Gala Jun 10 '15 at 06:38
0

3 is not necessarily an int. it is short type. Both char and short are 16 bit in length

Sharon Ben Asher
  • 13,849
  • 5
  • 33
  • 47
0

It is the ASCII value assigned to the character.

In the first case

Character character1 = 'a';

The character1 is directly assigned a character value.

But in your second statement:

Character character2 = 3;

character2 is assigned the ASCII value of 3 which is ?

Sashi Kant
  • 13,277
  • 9
  • 44
  • 71