• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

javaranch round up exam

 
ravi ckumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!!
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Judy YU
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Pete Pan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Judy. There is even a question about this with
byte a=1;
byte b=1;
byte c=a+b; //causes error because both are promoted

 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic