Java自动型促销 [英] Java automatic type promotions

查看:103
本文介绍了Java自动型促销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我引用Herbert Schildt的话:JAVA,The Complete Reference 8th Edition。

I quote from Herbert Schildt : JAVA, The Complete Reference 8th Edition.

第4章:运算符

当您移动字节和短值时,Java的自动类型促销会产生意外结果。如您所知,在计算表达式时,字节和短值将提升为int。此外,这种表达式的结果也是一个int。这意味着一个字节或一个短值左移的结果将是一个int,并且向左移位的位将不会丢失,直到它们移位到位31之后。

Java’s automatic type promotions produce unexpected results when you are shifting byte and short values. As you know, byte and short values are promoted to int when an expression is evaluated. Furthermore, the result of such an expression is also an int. This means that the outcome of a left shift on a byte or short value will be an int, and the bits shifted left will not be lost until they shift past bit position 31.

此外,当负字节或短值被提升为int时,它将被符号扩展。因此,高位将填充1。由于这些原因,对字节或短执行左移意味着必须丢弃int结果的高位字节。例如,如果左移一个字节值,则必须首先将该值提升为int然后移位。这意味着如果您想要的是移位字节值的结果,则必须丢弃结果的前三个字节。最简单的方法是简单地将结果转换回一个字节。

Furthermore, a negative byte or short value will be sign-extended when it is promoted to int. Thus, the high-order bits will be filled with 1’s. For these reasons, to perform a left shift on a byte or short implies that you must discard the high-order bytes of the int result. For example, if you left-shift a byte value, that value must first be promoted to int and then shifted. This means that you must discard the first three bytes of the result if what you want is the result of a shifted byte value. The easiest way is to simply cast the result back into a byte.

    byte a = 64, b;
    int i;
    i = a << 2;
    b = (byte)(a<<2);
    System.out.println("Binary Equivalent of b : "+Integer.toBinaryString(b)); // gives 0 

    byte i1 = -5,i2;
    int s;
    s = i1<<2;
    i2 = (byte)(i1<<2);

    System.out.println(Integer.toBinaryString(i2)); //gives 11111111111111111111111111101100

1111 1111 1111 1111 1111 1111 1110 1100是答案。我声明i2是字节的。因此它应该包含8位(一个字节)。为什么32位。或者根据前面提到的最后一段,我是否需要丢弃结果的前3个字节。詹姆斯·戈斯林为什么这样设计它。这恰好是在负数(这里是-5)的情况下。不会发生在64.我想我不清楚Herbert在第2段中说的是什么。请详细说明他希望解释的内容。我是Java的新手。

How comes 1111 1111 1111 1111 1111 1111 1110 1100 is the answer. I have declared i2 to be of byte. Hence it should contain 8 bits(a byte). Why 32 bits. Or according to the last paragraph aforementioned, do I need to discard the top 3 bytes of the result. Why James Gosling designed it like this. This happens to be only in case of negative numbers(here -5). Doesn't happens with 64. I think I am not clear what Herbert says in the 2nd paragraph. Kindly elaborate in detail what he wishes to explain. I am a new bie to Java.

推荐答案

i2 的价值是(二进制) 1110 1100 或(十进制) -20

The value of i2 is (in binary) 1110 1100 or (in decimal) -20.

参数类型 Integer.toBinaryString(int) int ,所以 i2 升级为 int 。这涉及符号扩展,这意味着符号位(负数的前导 1 或前导 0 为正数或零)被复制到所有更高位,产生(二进制) 1111 1111 1111 1111 1111 1111 1110 1100 或(十进制) -20

The parameter-type of Integer.toBinaryString(int) is int, so i2 gets promoted to int. This involves sign extension, meaning that the "sign bit" (the leading 1 for a negative number, or the leading 0 for a positive number or zero) gets copied to all higher bits, producing (in binary) 1111 1111 1111 1111 1111 1111 1110 1100 or (in decimal) -20.

当你看二进制表示时看起来很奇怪,但它有意义当你看十进制表示。符号扩展的目的是保留实际的数值。

It may seem strange when you look at the binary representations, but it makes sense when you look at the decimal representations. The purpose of sign extension is to preserve the actual numeric value.

这篇关于Java自动型促销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆