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
[HD-OCP17/21-Fundamentals Pg 234, Sec. 10.1.5 - numeric-promotion-and-casting]
Moderator: admin
-
- Posts: 167
- Joined: Sun Apr 21, 2024 10:43 am
- Contact:
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD-OCP17/21-Fundamentals Pg 234, Sec. 10.1.5 - numeric-promotion-and-casting]
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,
The return type of the method has to be the larger of the two. Nothing to do with numeric promotion.
Code: Select all
short m(){
byte aByte = 10;
byte aShort = 20;
if(true){
return aByte;
}else{
return aShort;
}
-
- Posts: 167
- Joined: Sun Apr 21, 2024 10:43 am
- Contact:
Re: [HD-OCP17/21-Fundamentals Pg 234, Sec. 10.1.5 - numeric-promotion-and-casting]
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?
i was just curions about this... i dont know how to call it... just promotion?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD-OCP17/21-Fundamentals Pg 234, Sec. 10.1.5 - numeric-promotion-and-casting]
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".
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".
-
- Posts: 167
- Joined: Sun Apr 21, 2024 10:43 am
- Contact:
Re: [HD-OCP17/21-Fundamentals Pg 234, Sec. 10.1.5 - numeric-promotion-and-casting]
oh ok thank you for this
except arithmetic and ternary, is there anything else in java that does "least upper bound" or numeric promotion?
except arithmetic and ternary, is there anything else in java that does "least upper bound" or numeric promotion?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD-OCP17/21-Fundamentals Pg 234, Sec. 10.1.5 - numeric-promotion-and-casting]
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.
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.
Who is online
Users browsing this forum: No registered users and 34 guests