byte, char, int in Java - bit representation

偶尔善良 提交于 2019-12-11 04:39:03

问题


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 are same they all are :: 00000000 01000001 . My question is if the bits are same then how they act differently - b as byte, c as char and s as short ? Another question is : char c = 65; why this is a correct statement ? it does not give error though I am assigning int value to a char.

Thanks.


回答1:


how they act differently ?

Refer the JLS:

The integral types are byte, short, int, and long, whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers, respectively, and char, whose values are 16-bit unsigned integers representing UTF-16 code units.

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

  1. For byte, from -128 to 127, inclusive

  2. For short, from -32768 to 32767, inclusive

  3. For int, from -2147483648 to 2147483647, inclusive

  4. For long, from -9223372036854775808 to 9223372036854775807, inclusive

  5. For char, from '\u0000' to '\uffff' inclusive, that is, from 0 to 65535

Another difference will be their Wrapper Classes differ : byte to Byte , char to Character and short to Short.

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

char, whose values are 16-bit unsigned integers representing UTF-16 code units (§3.1).




回答2:


byte is boxed to Byte

char is boxed to Character

short is boxed to Short

Those 3 classes have 3 different toString() method. That's why they have different displays.

Then the conversions from int to byte, char,short are done automatically for you, then it is boxed to their corresponding boxing class.




回答3:


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

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

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

   int x = 65;
   char c = x;   <-- error



回答4:


They do not behave differently as far data reprezentation is considered, the difference is only that they can hold values in different intervals.

As for char every character has it's integers representation that is why it is a valid syntax in java.




回答5:


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

The type of the variable only defines in what range this variable can take value:

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

char is represented by bits, or complementary hex, dec, oct nubmer. It does not matter. That's why you can assign a number to it and this nubmer is matched to Unicode representation of that number.



来源:https://stackoverflow.com/questions/17166393/byte-char-int-in-java-bit-representation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!