Type casting into byte in Java

倖福魔咒の 提交于 2019-12-12 09:44:13

问题


I am a beginner in Java. I came across a concept called Type Casting. I have the following snippet-

class Demo
{
    byte b;
    int a=257;
    double d= 323.142
    b=(byte)a;
      System.out.println(b);
    b=(byte)d;
      System.out.println(b);

}

The output for the code is- 1 67

Can anybody explain me the outputs.

Thanks in Advance!


回答1:


The byte type is encoded on 8 bits, so it takes its values between -128 and 127. In your case, casting by byte is the same as computing a modulo and rounding to an int. Try the following code, the output is the same:

int a = 257;
double d = 323.142;
System.out.println(a % 128);
System.out.println((int) d % 128);



回答2:


In both cases you are doing a narrowing conversion, which may result in loss of information.

  1. Conversion of int to byte

you convert an int whose value is 257 (00000000 00000000 00000001 00000001 in binary) to a byte. Therefore, only the lowest (right) byte of the int is kept. Therefore the result is 00000001 in binary, which is 1.

  1. Conversion of double to byte

This conversion is more complicated.

  • In the first step 323.142 is converted from double to int, so it becomes 323.
  • The second step is the same as the first conversion :

    323 is 00000000 00000000 00000001 01000011 in binary. Converting 323 to byte keeps the lowest (right) byte, which gives you 67.

Here's what the JLS says about this conversion :

A narrowing conversion of a floating-point number to an integral type T takes two steps:

  1. In the first step, the floating-point number is converted either to a long, if T is long, or to an int, if T is byte, short, char, or int, as follows:

    • If the floating-point number is NaN (§4.2.3), the result of the first step of the conversion is an int or long 0.

    • Otherwise, if the floating-point number is not an infinity, the floating-point value is rounded to an integer value V, rounding toward zero using IEEE 754 round-toward-zero mode (§4.2.3). Then there are two cases:

      a. If T is long, and this integer value can be represented as a long, then the result of the first step is the long value V.

      b. Otherwise, if this integer value can be represented as an int, then the result of the first step is the int value V.

    • Otherwise, one of the following two cases must be true:

      a. The value must be too small (a negative value of large magnitude or negative infinity), and the result of the first step is the smallest representable value of type int or long.

      b. The value must be too large (a positive value of large magnitude or positive infinity), and the result of the first step is the largest representable value of type int or long.

  2. In the second step:

    • If T is int or long, the result of the conversion is the result of the first step.

    • If T is byte, char, or short, the result of the conversion is the result of a narrowing conversion to type T (§5.1.3) of the result of the first step.




回答3:


byte b;
int a=257;
double d= 323.142
b=(byte)a; // 257-256=1
  System.out.println(b); // now b is 1
b=(byte)d; // 323.142-256=67
  System.out.println(b); // now b is 67

byte data type is an 8-bit signed two's complement integer(This is important in second case, why 67.142 become 67). byte in Java is signed, so it has a range -2^7 to 2^7-1 - that is, -128 to 127. Since 257 is above 127, you end up wrapping around to 257-256=1. That is 256 is added or subtracted until it falls into range.Same scenario happen in the second case too.




回答4:


Byte can store between the range of -128 to 127 which means 1 Byte (8 bit). Binary value of 257 is 100000001 after converting to byte which means 8 bit (from LSB to MSB) which will get value of 00000001 which is nothing but 1. Here you are explicitly converting the type so data loss will take place while converting higher data type to lower data type. Similarly for the later one. Hope this will help you.




回答5:


Because 257 = 100000001b but when you cast it in byte you get only 8 bit of this number: 00000001b = 1




回答6:


Hexadecimal representation of

  • (257) = 0x101
  • (323) = 0x143

byte stores only one byte of data and as you see the highlighted part in above hex representation:-

b = 0x01 = 1 for 257 and b = 0x43 = 67 for integer part of 323.14

Note:- In Java a double uses a 52 bit mantissa, hence we can represent a 32 bit integer without lost of data.



来源:https://stackoverflow.com/questions/26465633/type-casting-into-byte-in-java

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