在 Java 中将类型转换为字节

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26465633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:58:37  来源:igfitidea点击:

Type casting into byte in Java

javatypescasting

提问by user3788040

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

我是 Java 的初学者。我遇到了一个叫做类型转换的概念。我有以下片段-

    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 167

代码的输出是 167

Can anybody explain me the outputs.

任何人都可以向我解释输出。

Thanks in Advance!

提前致谢!

采纳答案by Dici

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

byte类型以 8 位编码,因此其值介于 -128 和 127 之间。在您的情况下,强制转换byte与计算模数并舍入为int. 试试下面的代码,输出是一样的:

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

回答by Eran

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

在这两种情况下,您都在进行缩小转换,这可能会导致信息丢失。

  1. Conversion of int to byte
  1. int 到 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.

您将一个值为 257 (00000000 00000000 00000001 00000001 二进制) 的 int 转换为一个字节。因此,只保留 int 的最低(右)字节。因此结果是二进制的 00000001,也就是 1。

  1. Conversion of double to byte
  1. 双字节到字节的转换

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.

  • 在第一步中 323.142 从 double 转换为 int,所以它变成了 323。
  • 第二步与第一次转换相同:

    323 是 00000000 00000000 00000001 01000011 二进制。将 323 转换为字节保留最低(右)字节,即为 67。

Here's what the JLSsays about this conversion :

以下是JLS关于此转换的说明:

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.

从浮点数到整数类型 T 的缩小转换需要两个步骤:

  1. 第一步,将浮点数转换为 long(如果 T 是 long)或转换为 int(如果 T 是 byte、short、char 或 int),如下所示:

    • 如果浮点数是 NaN(第 4.2.3 节),则转换的第一步的结果是 int 或 long 0。

    • 否则,如果浮点数不是无穷大,浮点值将被舍入为整数值 V,使用 IEEE 754 向零舍入模式(第 4.2.3 节)向零舍入。那么有两种情况:

      一个。如果T是long,并且这个整数值可以表示为long,那么第一步的结果就是long值V。

      湾 否则,如果这个整数值可以表示为一个int,那么第一步的结果就是int值V。

    • 否则,以下两种情况之一必须为真:

      一个。该值必须太小(大的负值或负无穷大),并且第一步的结果是 int 或 long 类型的最小可表示值。

      湾 该值必须太大(大的正值或正无穷大),并且第一步的结果是 int 或 long 类型的最大可表示值。

  2. 第二步:

    • 如果 T 是 int 或 long,则转换的结果是第一步的结果。

    • 如果 T 是 byte、char 或 short,则转换的结果是第一步结果的收缩转换到类型 T(第 5.1.3 节)的结果。

回答by Ruchira Gayan Ranaweera

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.142become 67). byte in Javais signed, so it has a range -2^7to 2^7-1- that is, -128to 127. Since 257is above 127, you end up wrapping around to 257-256=1. That is 256is added or subtracted until it falls into range.Same scenario happen in the second case too.

byte 数据类型是一个 8 位有符号二进制补码整数(这在第二种情况下很重要,为什么67.142变成67)。byte inJava是有符号的,所以它的范围是-2^7to 2^7-1- 也就是说,-128to 127。由于257是 上面127,您最终会环绕到257-256=1。256添加或减去它,直到它落入范围内。同样的情况也发生在第二种情况下。

回答by Jaya Ananthram

Byte can store between the range of -128 to 127which means 1 Byte (8 bit). Binary value of 257is 100000001after converting to byte which means 8 bit (from LSBto MSB) which will get value of 00000001which 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.

Byte 可以存储的范围介于-128 to 1271 Byte(8 位)之间。的二进制值257100000001在转换为字节后,这意味着 8 位(从LSBMSB)将得到的值00000001只是1. 在这里,您显式转换类型,因此在将较高的数据类型转换为较低的数据类型时会发生数据丢失。后面的也一样。希望这会帮助你。

回答by Mitesh Pathak

Hexadecimal representation of

的十六进制表示

  • (257) = 0x101
  • (323) = 0x143
  • (257) = 0x1 01
  • (323) = 0x1 43

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

byte仅存储一个字节的数据,正如您在上面的十六进制表示中看到的突出显示的部分:-

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

b = 0x01 = 1对于 257 和b = 0x43 = 67323.14 的整数部分

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

注意:-在 Java 中 adouble使用 52 位尾数,因此我们可以表示 32 位整数而不会丢失数据。

回答by Ludovic Feltz

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

因为 257 = 100000001b 但是当你把它转换成字节时,你只能得到这个数字的 8 位:00000001b = 1