Clarification on Ternary Operator Behavior in Java
Posted: Wed Sep 25, 2024 4:23 am
Hello everyone,
I hope you're all doing well! I'm currently preparing for the OCA/OCP Java Programmer Certification and I've come across a question regarding the behavior of the ternary operator that I find a bit confusing.
In the following code snippet:
I noticed that the ternary expression seems to return a Double instead of a Number. My understanding is that the result should be promoted to the highest type in the expression. However, I expected it to return a Number since by is of type Byte.
slice masters
Could someone please clarify why the expression behaves this way and how I can ensure it returns a Number? Any insights or explanations would be greatly appreciated!
Thanks in advance!
I hope you're all doing well! I'm currently preparing for the OCA/OCP Java Programmer Certification and I've come across a question regarding the behavior of the ternary operator that I find a bit confusing.
In the following code snippet:
Code: Select all
Double d = 10.0;
Byte by = 1;
Number n = a == b ? d : by;
slice masters
Could someone please clarify why the expression behaves this way and how I can ensure it returns a Number? Any insights or explanations would be greatly appreciated!
Thanks in advance!