In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you what problems you will encounter when using ternary expressions in java. I hope you will get something after reading this article. Let's discuss it together.
First, the problem recur public class Test {
Public static void main (String [] args) {
Integer a = null
Boolean flag = false
Integer b = flag? 1mm 2: a
System.out.println ("Program ends")
}
}
The code is simple: an is the wrapper type Integer, and the initial value is null,b assigned through a ternary expression. Run this code and you will get a null pointer exception:
Why did this happen? Let's analyze it:
Second, problem analysis
Just now this null pointer phenomenon is very easy for us to think of type conversion, and the type conversion of ternary expressions also has to follow certain rules.
1. Conversion rules for ternary operator types:
(1) if the two operands are not convertible, no conversion will be performed, and the return value will be of Object type.
Such as Object b = flag? A: B, at this point, An and B are two different objects that cannot be converted, so assign the final result to Object
(2) if the two operands are clearly typed expressions (such as variables), then convert according to the normal binary number, int type to long type, long type to float type, and so on.
This is easier to understand, such as float b = flag? 1VR 1.0f. 1: turn upwards for the int type.
(3) if one of the two operands is the number S and the other is an expression, and its type is marked T, then, if the number S is within the range of T, it is converted to T type; if S is outside the range of T type, then T is converted to S type.
This is the example we just demonstrated, Integer b = flag? 1 * 2: a, and the result is brooma, but an is null, which is forced to Integer (type 1 * 2), so a null pointer appears.
(4) if both operands are direct quantitative numbers, the return value type is the one with a wide range.
This situation is similar to the second one.
Now the answer basically comes out, and the reason for the null pointer is that a=null wants to cast Integer, so there is a null pointer because the virtual machine can't find the object to be converted when it sees a null.
2. Decompilation analysis
Now we find the bytecode file for Test, enter the javap-c command, and decompile it:
The answer should be clear by now.
3. Why unpack the box (key point)
Why unpack an and assign a=null to b directly? This point requires us to pay attention to that prerequisite, that is, one is a number, the other is an expression, and the example just now verifies this point. Now I am not an expression, let's verify it again:
Now we can see that this error still occurs when it is not an expression. Don't worry, and to make you more dizzy, let's change the code again:
This Nima is disgusting. I have tested a lot of different cases, which are basically divided into these two cases. The reasons are as follows:
Conditional expression? Expression 1: expression 2, assuming that the result is expression 2
(1) expressions 1 and 2 are both null. If expression 2 sees that 1 is null, it will not unpack.
(2) expression 1 is a number or expression, and expression 2 is unpacked according to the type of 1.
That is to say, expression 2 mainly judges whether to unpack or not according to expression 1.
III. Problem solving
The solution is simple: whenever you encounter null in a ternary expression, try to convert it to an if structure.
After reading this article, I believe you have a certain understanding of "what problems you will encounter when using ternary expressions in java". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.