Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why will byte not take 0xff in java?

Tags:

java

byte

Why will java compiler not let me put 0xff into a byte, 0xff is 8 bits long which is just the size of byte datatype.

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

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);         } } 

EDIT I 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 1111 and not the 8 bits of that byte to 1.

like image 977
vikkyhacks Avatar asked Sep 15 '13 13:09

vikkyhacks


People also ask

Is 0xff one byte?

Representing 0xff With Different Data Types int x = 0xff; assertEquals(255, x); However, if we define a byte variable with the value 0xff, since Java represents a byte using 8 bits and because a byte is a signed data type, the value of 0xff is -1: byte y = (byte) 0xff; assertEquals(-1, y);

What is byte 0xff?

The signed byte 0xff represents the value -1 . This is because Java uses two's complement to represent signed values. The signed byte 0xff represents -1 because its most significant bit is 1 (so therefore it represents a negative value) and its value is -128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = -1 .

Can byte be null Java?

What you call a null byte is not the same as a null reference in Java. A null byte is typically a byte with all bits clear, i.e. 0. So all you have to do is read a byte and compare it to 0. Note that the read method in InputStream returns an int , and -1 means end of file.

Can a byte be negative Java?

In Java, byte is an 8-bit signed (positive and negative) data type, values from -128 (-2^7) to 127 (2^7-1) . For unsigned byte , the allowed values are from 0 to 255 .


2 Answers

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

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

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 -127 in your byte variable.


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 int to a byte without 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 without a cast. The literal's value is implicitly narrowed from int to byte.

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

"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."

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


FOLLOW-UP

I 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?

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

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

Because 0xff is 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.1 says:

"An integer literal is of type long if it is suffixed with an ASCII letter L or l (ell); otherwise it is of type int (§4.2.1)."

like image 113
Stephen C Avatar answered Sep 16 '22 14:09

Stephen C


0xff is a hexadecimal representation of a number. In other words the number is base 16.

f = 15 in hex.

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.

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.

This will work

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

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

like image 35
Sotirios Delimanolis Avatar answered Sep 16 '22 14:09

Sotirios Delimanolis