Byte, char, int in Java - bit representation

I am confused with byte and char in Java. I have the following program segment:

byte b = 65; char c = 65; short s = 65; System.out.println("b:"+b+" c:"+c+ " s:"+s); // outputs b:65 c:A s:65 

The bit representation of b, c and s is the same, all of them: 00000000 01000001. My question is whether the bits are the same, how do they act differently - b like byte, c like char and s like short? Another question: char c = 65; why is this the correct statement? it does not give an error, although I am assigning an int char value.

Thank.

+2
java char byte
Jun 18 '13 at 10:19
source share
5 answers

how do they act differently?

Refer JLS :

Integral types are short, int, and long bytes whose values ​​are 8-bit, 16-bit, 32-bit, and 64-bit signed two padding characters, respectively, and char, whose values ​​are 16-bit unsigned integers, representing code units UTF-16.

The values ​​of integral types are integers in the following ranges:

  • For bytes from -128 to 127 inclusive

  • In short, from -32768 to 32767 inclusive

  • For int from -2147483648 to 2147483647 inclusive

  • Long time from -9223372036854775808 to 9223372036854775807 inclusive

  • For char, from '\ u0000' to '\ uffff' inclusive, i.e. from 0 to 65535

Another difference is that their Wrapper classes are different: from byte to byte , char to Character and short to short .

char c = 65; why is this the correct statement?

char whose values ​​are unsigned 16-bit integers representing the code units of UTF-16 (Β§ 3.1).

+3
Jun 18 '13 at 10:21
source share

byte is placed in the byte field

char is placed in the Character field

short is placed in the short field

These 3 classes have 3 different toString() methods. That is why they have different displays.

Then conversions from int to byte , char , short are performed automatically for you, then they are placed in the corresponding boxing class.

+2
Jun 18 '13 at 10:23
source share

1) their bit representation is not the same, it is 01000001 for byte and 0000000001000001 for short and char.

2) the difference is that the byte and short are integers, and char is a 16-bit Unicode character

3) 65 is not int, it is constant, if you try to assign int, you will get an error

  int x = 65; char c = x; <-- error 
+2
Jun 18 '13 at 10:28
source share

They do not behave differently, since the representation of the data is considered, the only difference is that they can store values ​​at different intervals.

As for char, each character has an integer representation, so it is a valid syntax in java.

+1
Jun 18. '13 at 10:21
source share

All primitive types ( boolean, char, short, int, ... ) are actually bit arrays in memory.

The type of a variable determines only in which range this variable can take a value:

  • boolean 1 bit [range 0-1]
  • char 16 bits [range 0 to 216-1 or \ u0000 to \ uFFFF]
  • byte 8 bits [range from -128 to 127]
  • short 16 bits [-32768 to 32767]
  • int 32 bits [-2147483648 to 2147483647]
  • ...



char is represented by bits or optional hexadecimal, dec, oct-noobmer. It does not matter. This is why you can assign a number to it, and this nubmer matches the Unicode representation of that number.

+1
Jun 18 '13 at 10:26
source share



All Articles