[HD-OCP17/21-Fundamentals Pg 283, Sec. 12.2.1 - type-of-a-ternary-conditional-expression]

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

[HD-OCP17/21-Fundamentals Pg 283, Sec. 12.2.1 - type-of-a-ternary-conditional-expression]

Post by raphaelzintec »

Double d = 10.0;
Byte by = 1;
Number n = a == b? d : by;

here ternary send back a Double, not directly a Number

Because if it was directly a Number i wont be able to do this: Double n = a == b? d : by;

scoffnike
Posts: 2
Joined: Wed Sep 25, 2024 4:19 am
Contact:

Re: [HD-OCP17/21-Fundamentals Pg 283, Sec. 12.2.1 - type-of-a-ternary-conditional-expression]

Post by scoffnike »

In this scenario, the ternary conditional expression is returning a Double because d is a Double and by is a Byte. In Java, the ternary operator will promote the result to the highest type, which is Double in this case. To make your expression work as intended, you can cast by to Number explicitly, like this:
Number n = a == b ? d : (Number) by;
This way, both branches of the ternary expression return a Number, allowing you to assign it to a Number variable without issues. If you want to keep the variable as a Double, you can cast n afterward:
Double n = (Double) (a == b ? d : (Number) by);hill climb racing
This ensures type safety and resolves the issue you're encountering.
Last edited by scoffnike on Thu Feb 13, 2025 3:27 am, edited 1 time in total.

admin
Site Admin
Posts: 10385
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD-OCP17/21-Fundamentals Pg 283, Sec. 12.2.1 - type-of-a-ternary-conditional-expression]

Post by admin »

Did you read the complete topic "Type of a ternary conditional expression" inside section 12.2.1? It explains exactly what you are getting at in quite detail.
Specifically, this part,
The compiler solves this problem by deciding to pick the most specific common superclass of the two types as the type of the expression. In this case, that class is java.lang.Object. By selecting the most specific common super class, the compiler ensures that irrespective of the result of the condition, the value returned by this expression will always be of the same type

raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

Re: [HD-OCP17/21-Fundamentals Pg 283, Sec. 12.2.1 - type-of-a-ternary-conditional-expression]

Post by raphaelzintec »

i have read all of this and i dont have any issues

i am just saying that in this scenario ternary doesnt send directly a Number but a double

raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

Re: [HD-OCP17/21-Fundamentals Pg 283, Sec. 12.2.1 - type-of-a-ternary-conditional-expression]

Post by raphaelzintec »

even more interesting it send back a double and not a Double

im just sharing here what i learned

admin
Site Admin
Posts: 10385
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD-OCP17/21-Fundamentals Pg 283, Sec. 12.2.1 - type-of-a-ternary-conditional-expression]

Post by admin »

oh ok, no issue then :cheers:

Post Reply

Who is online

Users browsing this forum: No registered users and 15 guests