• Post Reply Bookmark Topic Watch Topic
  • New Topic

Ternary Operator  RSS feed

 
Ransika deSilva
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have this problem.
byte y = 20;
short s = 10;
boolean b = true;
byte x = b?y:s;
Found out that the result is a short value. Why don't the operands get promoted to int?
 
Ray Stojonic
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your ternary statement is the equivilent of

as such, with x = s you're trying to assign a short to a byte, which is a narrowing conversion and not allowed without an explicit cast.
I'm confused though: Why do think the operands would be cast to int? Further, why do you think that would help?
 
Ransika deSilva
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok,
Thanks for the answer, The understand the mistake that I made.
Thank you..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!