为什么字节不会在java中取0xff?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18812864/
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-08-12 11:17:07  来源:igfitidea点击:

Why will byte not take 0xff in java?

javabyte

提问by vikkyhacks

Why will java compiler not let me put 0xffinto a byte, 0xffis 8 bits long which is just the size of bytedatatype.

Can someone explain why 1 works and why 2 does not ?

为什么java编译器不让我放入0xff一个byte,0xff是 8 位长,这只是byte数据类型的大小。

有人可以解释为什么 1 有效而 2 无效吗?

class a
{
        public static void main(String[] args)
        {
                // 1 :: results in error
                byte a = 0xff;          
                System.out.printf("%x\n",a);

                //2 :: works fine
                byte a = (int)0xff              
                System.out.printf("%x\n",a);
        }
}

EDITI read the answer claiming that 0xff is 255, how so ? Is it not 1111 1111, What makes 0xff, -128 or 255 or anything for that matter. Why will it not just treat it as 1111 1111and not the 8 bits of that byte to 1.

编辑我阅读了声称 0xff 是 255 的答案,怎么会这样?是不是1111 1111,是什么使 0xff、-128 或 255 或任何与此相关的东西。为什么它不只是把它当作1111 1111而不是那个字节的 8 位到 1。

采纳答案by Stephen C

The Java bytetype is an 8 bit signed integral type with values in the range -128to +127. The literal 0xffrepresents +255which is outside of that range.

Javabyte类型是一个 8 位有符号整数类型,其值范围-128+127。文字0xff表示+255超出该范围的内容。

In the first example, you are attempting to assign a value that is out of range to a byte. That is a compilation error.

在第一个示例中,您尝试将超出范围的值分配给byte。那是编译错误。

In the second example, the (byte)cast is performing an explicit narrowing conversion, that removes the high order bits of the integer literal ... giving you the value -127in your bytevariable.

在第二个例子中,(byte)投正在执行一个明确的收缩转换,去除整数的高位文字......给你的价值-127在你的byte变量。



In fact, the situation with the first example is a bit more complicated than that. Consider this:

实际上,第一个示例的情况比这要复杂一些。考虑一下:

byte a = 1;         // OK
int i = 1;
byte b = i;         // Compilation error
byte c = (byte) i;  // OK

Under normal circumstances, you cannot assign an intto a bytewithout a cast. However, if the value are assigning is a literal, and the literal value is within the range of the target type, the Java language permits the assignment withouta cast. The literal's value is implicitly narrowed from intto byte.

在正常情况下,您不能在没有强制转换的情况下将 an 分配int给 a byte。但是,如果要分配的值是文字,并且文字值在目标类型的范围内,则 Java 语言允许无需强制转换的分配。文字的值从int到隐式缩小byte

This is described in JLS §5.2which defines the conversions that may be performed in an assignment:

这在JLS §5.2 中有描述,它定义了可以在赋值中执行的转换:

"A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable."

“如果变量的类型是 byte、short 或 char,并且常量表达式的值可以在变量的类型中表示,则可以使用缩小原始转换。”

And as you can see, this doesn't justapply to literals. It applies to all (compile-time) constant expressions!

正如您所看到的,这不仅适用于文字。它适用于所有(编译时)常量表达式!



FOLLOW-UP

跟进

I read the answer claiming that 0xffis 255, how so? Is it not 1111 1111, What makes 0xff, -128or 255or anything for that matter?

我读了声称0xff是的答案255,怎么会这样?这不是1111 1111,是什么让0xff-128255或任何与此有关?

The literal 0xffis an integer literalof type int. The intvalue of the literal 0xffis actually 0000 0000 0000 0000 0000 0000 1111 1111in binary or +255 in decimal. By contrast, the integer value -128has the bit pattern 1111 1111 1111 1111 1111 1111 1000 0000.

文字0xff是类型为 的整数文字intint文字的值0xff实际上0000 0000 0000 0000 0000 0000 1111 1111是二进制或十进制+255。相比之下,整数值-128具有位模式1111 1111 1111 1111 1111 1111 1000 0000

Why will it not just treat it as 1111 1111and not the 8 bits of that byte to 1?

为什么它不只是把它当作1111 1111而不是那个字节的 8 位到 1?

Because 0xffis an integer literal with type int. It is not an 8-bit literal, because 8-bit literals do not exist in Java. As JLS §3.10.1says:

因为0xff是一个类型为的整数文字int。它不是 8 位文字,因为 Java 中不存在 8 位文字。正如JLS §3.10.1所说:

"An integer literal is of type longif it is suffixed with an ASCII letter Lor l(ell); otherwise it is of type int(§4.2.1)."

“如果整数文字long以 ASCII 字母Ll(ell)为后缀,则它属于类型;否则它属于类型int( §4.2.1)。”

回答by nanofarad

0xffis 255. While this does fit into an 8-bit unsignedinteger, byteis signed(-128 to 127). The narrowing will remove the high bits and fit 8 into 8 without regard for sign.

0xff是 255。虽然这确实适合 8 位无符号整数,但它byte是有符号的(-128 到 127)。缩小将删除高位并将 8 放入 8 而不考虑符号。

回答by Sotirios Delimanolis

0xffis a hexadecimal representationof a number. In other words the number is base 16.

0xff是一个数字的十六进制表示。换句话说,数字是以 16 为底的。

f = 15in hex.

f = 15在十六进制。

The value is equal to

该值等于

15 * 16^1 + 15 * 16^0 = 255

This is an integer literal(uses 4 bytes), which exceeds byte's value range.

这是一个整数文字(使用 4 个字节),超出了byte的值范围。

Neither of the two examples you've posted will compile as neither fits into byte's value range of -128 to 127. You can read about primitive type value ranges here.

您发布的两个示例都不会编译,因为它们都不适合byte的值范围-128 to 127。您可以在此处阅读有关原始类型值范围的信息

This will work

这将工作

byte a = (byte)0xff; 
System.out.println(a);

and print -1, because a byte narrowing conversion of 255is -1.

和打印-1,因为字节缩窄转换255-1

回答by Philipp Seeger

First, if I try to run your code, the 2. case doesn't work either. You cast 0xff to the primitive datatype int (which has 32 bit) and want to store it in a 'byte' with only 8bit. Java treats your first case as int too, resulting in the same problem.

首先,如果我尝试运行您的代码,则 2. 情况也不起作用。您将 0xff 转换为原始数据类型 int(具有 32 位)并希望将其存储在只有 8 位的“字节”中。Java 也将您的第一种情况视为 int,从而导致同样的问题。

Because the java 'byte' type is an 8-bit signed integer value (from -128 to 127), to output / print the byte as hex, you can use Integer.toHexString('the primitive byte' & 0xFF) where ' & 0xFF ' is a bitmask to take into account that Integer uses a 32bit int and byte uses 8bit.

因为 java 'byte' 类型是一个 8 位有符号整数值(从 -128 到 127),要将字节输出/打印为十六进制,您可以使用 Integer.toHexString('the original byte' & 0xFF) where ' & 0xFF ' 是一个位掩码,考虑到 Integer 使用 32bit int 而 byte 使用 8bit。

    byte myByte = (byte)0xff;
    System.out.println(Integer.toHexString(myByte & 0xFF));

This outputs:

这输出:

ff