"151 Suggestions for Improving Java Programs" Reading Notes Value Ternary Operator Type Problem

Recommendation 3: The type of the ternary operator must be consistent. The
ternary operator is a simplified way of writing if-else. It is used in many places in the project, and it is very easy to use, but
easy to use and simple things do not mean that you can just do it. Use, let's take a look at the following code:
public class Client{
public static void main
( String[]args ) {
This document is generated by the extremely fast PDF editor,
if you want to remove this prompt, please visit and download:
http:// www.jisupdfeditor.com/
2017/10/20
9/396
int i=80 ;
String s=String.valueOf ( i < 100?90 : 100 );
String s1=String.valueOf ( i 100?90 : 100.0 ) ;
System.out.println ( " Are both equal: "+s.equals ( s1 ));
} }
Analyze this program: i is 80, then of course it is less than 100, the return value of both must be 90, and then converted to
String type, its value is absolutely equal, no doubt. Well, the analysis makes some sense, but
the second operand of the ternary operator in the variable s is 100, and the second operand of s1 is 100.0, doesn't it matter? It can't have an impact
, right? The conditions of the ternary operator are all true, and only the first value is returned. Does it have a dime relationship with the second value? It seems
reasonable.
Is that true? Let's verify it by the result. The result of the operation is: "are the two equal: false",
what ? Not equal, why?
The problem lies in the two numbers 100 and 100.0. In the variable s, the first operand
(90) and the second operand (100) in the ternary operator are both of type int and have the same type. The returned result is 90 of the int type,
but the situation of the variable s1 is a little different. The first operand is 90 (int type), but the second operand is 100.0,
and this is a floating point number, that is to say The types of the two operands are inconsistent, but the ternary operator must return a data,
and the type must be determined. It is impossible to return the int type when the condition is true, and return the float type when the condition is false. The compiler does
not allow this. So it will perform type conversion, int type is converted to floating point number 90.0, that is to say, the return value of ternary operator
is floating point number 90.0, which of course is not equal to 90 of integer type. Some readers may be confused here: Why is the
integer converted to floating point, not the floating point converted to integer? This involves the conversion rules of the ternary operator type:
if the two operands are not convertible, no conversion is performed, and the return value is of type Object.
If the two operands are expressions of clear types (such as variables), they are converted according to normal binary numbers,
int type is converted into long type, long type is converted into float type, etc.
If one of the two operands is a number S and the other is an expression, and its type is marked as T, then if the number
S is within the range of T, it is converted to type T; if S is beyond the range of type T , then T is converted to type S (refer to "
Proposal 22", which will describe the problem).
If both operands are literal numbers (Literal), the return value type is the larger of the range.
Knowing the reason, the corresponding solution is also available: ensure that the two operand types in the ternary operator -
the document is generated by the extremely fast PDF editor,
if you want to remove the prompt, please visit and download:
http: //www.jisupdfeditor.com/
2017/10/20
10/396
, you can reduce the occurrence of possible errors. 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325849206&siteId=291194637