This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer (Exam 1Z0-830) Java SE 17 Developer (Exam 1Z0-829) Programmer’s Guide and have Khalid Mughal and Vasily Strelnikov on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Conditional Operator

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all.
This is the first time I'm posting, so be nice to me please.
1. public class TestA
2. {
3. public static void main(String[] args)
4. {
5.
6. boolean flag = true;
7. byte b = (flag==true) ? 0 : 1;
8. System.out.println(b);
9. }
10.}
Understandably , I get a compile error on line 6 :
Incompatible type for declaration. Explicit cast needed to convert int to byte.
If I change line 6 to:
byte b = (true==true) ? 0 : 1;
This compiles okay. Is it because of compiler optimisation on the modified line no 6 making it equal to:
byte b = 0;
Any ideas?
 
Ali
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, any references I made to line 6 in my comments should read line 7.
Thanks.
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ali,
Welcome. Fell free to post your doubts here. One small suggestion. In this forum you can edit your messages by clicking the small icon at the top-right of your message (see.. there is a small icon with a paper-and-pencil) which is well used by Maha most of the time.
regds
maha anna
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1. Is compiler optimization in the objectives of SCJP2
Programmer certification ?
2. If it is, could someone explain this please ?
3. If not please do not post such topics in this forum. I
have no intention of being harsh or anything, but such
topics really scares me.
Regds.
- satya
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
satya,
Don't worry. Compiler optimization is NOT in the SCJP2 exam objectives. I left this thread because, we can consider this qstn as part of operators and assignamets. Take it easy.
regds
maha anna
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ali, Satya,
byte b = (flag==true) ? 0 : 1;
This is an example of assignment conversion;
From JLS here special type of narrowing conversion is also there when all of the foll conditions are true.

1. The expression is a constant expression of type int.
2. The type of the variable is byte, short, or char.
3. The value of the expression (which is known at compile time, because it is a constant expression) is representable in the type of the variable.

byte b = (flag==true) ? 0 : 1;
when we use a var(not constant) flag in an expression it is NOT COMPILE TIME CONSTANT. The variable's value may change at run time. We can't be sure. This means the right hand side of the above statement is NOT COMPILE TIME constant expression and the evaluation of the right hand side of the statement is common promoted type of the 2 operands between the : . Here both are 'int' So the compiler says Can't convert 'int' to 'byte' at compile time. IF the operands at both sides of the : WOULD HAVE BEEN int : float then the compiler will say can't convert float to byte. This rule is applicable when both operands are reference types also. One operand MUST be convertible to the other.
So, when you apply flag==true the resulting type of the above statement becomes int which CAN'T be assigned to a var of type byte.
At the same time when the WHOLE of right hand side expression involves ALL Compile time constants like true == true and the resulting int literal value is between the range of the left hand var type which here is 'byte' ,then this expression is ELIGIBLE for the above said special type of assignment conversion So the foll.
byte b = (true==true) ? 0 : 1;
same as
byte b = true ? 0 : 1;
same as
byte b= 0;
You can learn more about the terinary operator here in JLS
regds
maha anna

[This message has been edited by maha anna (edited April 19, 2000).]
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(Maha, I was composing my post at the same time you were revising yours, so I didn't see the above. I think you've confirmed my statement. Thanks.)
This is a good question. It kind of stumped me at first, and it brought out something about conditional expressions that I wasn't aware of.
What I think is happening here is that if you use a variable in the boolean part of the conditional expression, then the second and third operands are subject to (binary?) numeric promotion and an explicit cast is required.
If you use a boolean literal, then the second and third operands are treated just as they are (i.e., constants of type int whose value can fit into the byte variable and therefore no cast is required) and the conditional expression is treated as a simple assignment.
Can someone confirm?


[This message has been edited by Betty Reynolds (edited April 19, 2000).]
 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good question deserves a bump..
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, it returns int so instead of byte b use an int.
 
reply
    Bookmark Topic Watch Topic
  • New Topic