Page 1 of 1

[HD-OCP17/21-Fundamentals Pg 234, Sec. 10.1.5 - numeric-promotion-and-casting]

Posted: Mon Sep 23, 2024 5:03 pm
by raphaelzintec
the only operator that doesnt apply binary numeric promotion is ternary right?

i mean if one operand is byte and second is short then ternary will convert both into higher type so short, not int

Re: [HD-OCP17/21-Fundamentals Pg 234, Sec. 10.1.5 - numeric-promotion-and-casting]

Posted: Tue Sep 24, 2024 4:09 am
by admin
No, the concept of numeric promotion in relation to operators is applicable only to arithmetic operators. Ternary operator is not an arithmetic operator. So the question of numeric promotion does not arise here. Ternary operator is like an if/else statement. For example,

Code: Select all

short m(){
    byte aByte = 10;
    byte aShort = 20;
    if(true){
       return aByte;
   }else{
       return aShort;
}
The return type of the method has to be the larger of the two. Nothing to do with numeric promotion.

Re: [HD-OCP17/21-Fundamentals Pg 234, Sec. 10.1.5 - numeric-promotion-and-casting]

Posted: Tue Sep 24, 2024 1:40 pm
by raphaelzintec
yes i know but if one operand is byte and other short than ternary will return a short no matter if true or false

i was just curions about this... i dont know how to call it... just promotion?

Re: [HD-OCP17/21-Fundamentals Pg 234, Sec. 10.1.5 - numeric-promotion-and-casting]

Posted: Wed Sep 25, 2024 12:44 am
by admin
There are two things:
1. Arithmetic operators (such a +, -. * etc.) apply binary numeric promotion to int even if both the operands are smaller than an int. Which is why the result of byte*short is an int.

2. Ternary operator is not an arithmetic operator but it may have numeric expressions as 2nd and 3rd operands. In this case, it applies a binary numeric promotion of the smaller operand to the type of the larger operand (which is not necessarily an int). Which is why the return type of condition?byte:short is a short and not an int.

That is why I said that the type of numeric promotion that you mentioned earlier (i.e. promotion to int) is not applicable to ternary operator because it is not an arithmetic operator.

Technically, both are "binary numeric promotions" but the rules are a bit different. Ternary operator does the same thing when 2nd and 3rd operands are reference types. In that case, it is called "least upper bound".

Re: [HD-OCP17/21-Fundamentals Pg 234, Sec. 10.1.5 - numeric-promotion-and-casting]

Posted: Wed Sep 25, 2024 12:56 am
by raphaelzintec
oh ok thank you for this

except arithmetic and ternary, is there anything else in java that does "least upper bound" or numeric promotion?

Re: [HD-OCP17/21-Fundamentals Pg 234, Sec. 10.1.5 - numeric-promotion-and-casting]

Posted: Wed Sep 25, 2024 1:16 am
by admin
If you have multiple return statements in a method then the return type of that method will be lub of the two.
Same thing happens with a switch expression.
I don't have a list but if you see any situation where you are treating two different type of objects as the same object, then you have to use LUB.