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