programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# javaranch round up exam

Greenhorn
Posts: 18
in the round up exam it was given that if 2 primitives are compared then the lower is automatically get converted to the higher, so the answer was given to be true,
but boolean is also a primitive in java, but if u compare a boolean with any primitive then a compiler err is got
so i feel the answer is false
what say !!!

Ranch Hand
Posts: 1070
boolean can never be promoted to anything and you can't convert anything to a boolean. The rules for narrowing and widening apply to the other primitives.
Bill

Ranch Hand
Posts: 30
If the 2 primitive datatypes are byte and short, according to the answer, byte(lower) should be promoted to short(the higher), which is obviously wrong since both of them should be promoted to int. The answer should be false.
Judy

Ranch Hand
Posts: 44
byte a=1;
byte b=1;
byte c=a+b; //causes error because both are promoted

Ranch Hand
Posts: 1492
Hi,
Judy and Peter. Read the question again. You both are confusing arithmetic with comparison. If the question had stated the following:
if 2 primitive are used in an arithmetic statement ...
But the question doesn't say that ...
Comparison is handled between 2 primitives by promoting the lower to the upper. This, of course, excludes boolean!
Regards,
Manfred.

Pete Pan
Ranch Hand
Posts: 44
I didn't mention boolean.
I am NOT confusing them?
byte b=1;
short s=2;
if(b==s)
{
// b and s are promoted to int
// s=s+b fails for loss of precission
// so if b are promoted for addition
// then they are both promoted for comparison
}

Originally posted by Manfred Leonhardt:
Hi,
Judy and Peter. Read the question again. You both are confusing arithmetic with comparison. If the question had stated the following:
if 2 primitive are used in an arithmetic statement ...
But the question doesn't say that ...
Comparison is handled between 2 primitives by promoting the lower to the upper. This, of course, excludes boolean!
Regards,
Manfred.