Java Bytes

F

freesoft_2000

Hi everyone,

Consider the below program

public class ByteTest
{

public void ByteTest1()
{
byte byte1 = (byte)0x9C;
Byte byte2 = new Byte(byte1);

System.out.println(byte2.toString());
}

public void ByteTest2()
{
byte byte3 = 0x5A;

Byte byte4 = new Byte(byte3);

System.out.println(byte4.toString());
}

public static void main(String[] args)
{
ByteTest a = new ByteTest();
a.ByteTest1();
a.ByteTest2();
}

}

Now from what i understand 1 byte is 8 bits so the maximum is all 1's (ie.
255).

The above first function ByteTest1 i cast the hexadecimal a byte as so

byte byte1 = (byte)0x9C;
Byte byte2 = new Byte(byte1);

Now why do i have to cast it even when 0x9C is well below 255 but if i do
not cast it the compiler complains saying that it is an integer.

The above second function ByteTest2 i did not cast the hexadecimal a byte
as so

byte byte3 = 0x5A;
Byte byte4 = new Byte(byte3);

Now why is it that the compiler accepts the hexadecimal value 0x5A without
any casting but for 0x9C the compiler emit errors saying it is an integer?

Am i missing something?

Any help is greatly appreciated

Thank You

Yours Sincerely

Richard West
 
E

Eric Sosman

freesoft_2000 wrote On 10/14/05 14:34,:
[...]
The above first function ByteTest1 i cast the hexadecimal a byte as so

byte byte1 = (byte)0x9C;
Byte byte2 = new Byte(byte1);

Now why do i have to cast it even when 0x9C is well below 255 but if i do
not cast it the compiler complains saying that it is an integer.

Because the range of `byte' is -128 through 127. 0x9c
is 156, which is larger than 127 and hence outside the range
of `byte'.
The above second function ByteTest2 i did not cast the hexadecimal a byte
as so

byte byte3 = 0x5A;
Byte byte4 = new Byte(byte3);

Now why is it that the compiler accepts the hexadecimal value 0x5A without
any casting but for 0x9C the compiler emit errors saying it is an integer?

0x5A is 90, which is within the `byte' range. The compiler
knows the value will fit without alteration, so it lets you go
ahead and do it.
Am i missing something?

The fact that `byte' has a sign, apparently. (You're not
the only one who thinks the inventors of Java would have done
better to define `byte' as unsigned, like `char'. However,
they didn't; that's not the way the language turned out.)
 
F

freesoft_2000

Hi everyone,

In that case let's say if i have the number -8 and +10.
How do i convert them to signed bytes respectively?

Richard West
 
L

Lasse Reichstein Nielsen

freesoft_2000 said:
In that case let's say if i have the number -8 and +10.
How do i convert them to signed bytes respectively?

byte b1 = -8;
byte b2 = 10;

In both cases you have a compile time integer constant that is in the
range of a byte, so assignment doesn't need a cast.

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top