Actually, this is one of the strangest things about Java--all integral literals are considered to be int (32 bits) and all floating point literals are considered to be double (64 bits) (1st "anomaly").
For integral types, you don't have to "cast down" the literal int into a smaller type such as byte, short, char so:
byte b = 2; // ok although 2 is an int
char c = 2; // ok although 2 is an int
short s = 2; // ok although 2 is an int
int i = 2; // ok
long l = 2; // this is okay although expected compiler to complain
long l2 = 2L; // this is the normal way
float f = 2.0; // BAD compiler barfs: "Explicit cast needed to convert double to float"
float f = 2.0f; // correct
double d = 2.0; // ok
If a cast is need to "narrow" for floating point variables why isn't it needed for integral types? (2nd "anomaly")
byte b1 = 2, b2 = 3, b3 = 3;
b3 = b1 + b2; // compiler barfs: "Incompatible type for =. Explicit cast needed to convert int to byte."
b3 = (byte)( b1 + b2 ); // this works
Where did the int come from? Arithmetic promotion--all operands to binary operators are promoted to int automatically if they are not int, float or double. So why use byte or short? (3rd "anomaly")
So...
1. The "yardstick" types are different bitwidths for integral types and floating point types (int & double) // not a big deal
2. Integral literal assignments do not follow casting/conversion rules but floating point literal assignments do.
3. Arithmetic promotion GUARANTEES that you have to cast back down to the smaller type if you use binary operands on anything smaller than an int even if they are all of the same type.
And this language was "designed" from scratch?
Steve
sgwbutcher@aol.com