Sunday, 15 September 2013

Why will byte not take 0xff in java?

Why will byte not take 0xff in java?

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
byte a = 0xff; //results in error
System.out.printf("%x\n",a);
//2
byte a = (int)0xff // works fine
System.out.printf("%x\n",a);
}
}

No comments:

Post a Comment