Cover: school
Background: The reason for writing this is what I happened to see in the past two days. Although I have always had the
alibaba Java
development manual and read it many times, I have never paid attention to this problem.I've seen it, but I haven't seen it completely 👰
Let's take a look
hxdm, I can’t write a short story 😭, but can it be seen that I have written a lot, and it’s still practical, give me a thumbs up 👍.
Please drink xdm here🥤
1. Preface🚕
When I was writing a AdvertVO
today, I used the id to directly give long
without using the packaging type, and then the Alibaba Java Coding Guidelines
plug-in in the idea directly gave a yellow wavy line, ALT+ENTER
a look at the Alibaba Java Development Manual The prompt says:
The standards for the use of basic data types and packaging data types are as follows:
1) All POJO class attributes must use packaging data types.
2) The return value and parameters of the RPC method must use packaged data types.
3) Basic data types are recommended for all local variables.Description: POJO class property is not the initial alert the user when needed, you must explicitly assign their own, any NPE problem, or storage checks by the user to ensure .
When I first watched it, I didn’t fully understand the meaning a little bit (maybe it’s because I’m more culinary and I’ve never experienced such a scene), and then in order to understand the little doubts in my heart 😂, just 👇
Next, we will get a simple example to understand and understand, and then we will talk about the actual scene and the harm that will occur.
Two, example🛵
public class Main {
private static Integer a1;
private static int a2;
private static Boolean b1;
private static boolean b2;
public static void main(String[] args) {
System.out.println("Integer ==> a1:"+a1);
System.out.println("int ==> a2:"+a2);
System.out.println("Boolean ==> b1:"+b1);
System.out.println("boolean ==> b2:"+b2);
}
/**
* 结果:
* Integer ==> a1:null
* int ==> a2:0
* Boolean ==> b1:null
* boolean ==> b2:false
*/
}
All packaging types directly default to a null value when we do not assign a value, and the basic type will initialize a default value.
In other words, the default value of the packaging type is null, and the default value of the basic data type is a fixed value, such as boolean is false, byte, short, int, long are 0, float is 0.0f, etc.;
👨💻Maybe xdm usually notices, but not fully noticed, the difference between the basic type and the packaging type. Let's talk about the difference with the scene below: 👩🏫
Three, scene 🛫
[Positive example]: The query result of the database may be null, because automatic unboxing and receiving with basic data types have the risk of NPE. (NPE explained below)
[Counter example]: The transaction report of a certain business shows the increase and decrease of the total transaction amount, that is, plus or minus x%, x is the basic data type, the RPC service called, when the call is unsuccessful, the default value is returned, and the page displays 0% , This is unreasonable and should be displayed as a cross-line -. Therefore, the null value of the wrapped data type can represent additional information, such as: remote call failure, abnormal exit.
1) Scene one ⛵
Let's give another example of deduction. We build a deduction system. When deducting fees, we need to read the value of a rate from the external pricing system. We expect that the return value of the interface will contain a floating-point rate. Field. When we get this value, we use the formula: amount * rate = cost for calculation, and the calculation result is deducted.
If the billing system is abnormal, it may return a default value. If the field is of type Double, the default value is null, and if the field is of type double, the default value is 0.0.
If the deduction system does not perform special processing on the return value of the rate, it will directly report an error when the null value is obtained and the program will be blocked. If you get 0.0, you can directly perform calculations, and deduct the fee after the interface is 0. This abnormal situation cannot be sensed.
Some people say that I can make a special judgment on 0.0. If it is 0, it can block the error report. However, there will be a problem at this time. How to deal with the scenario where the allowable rate is 0? (The following example)
A small conclusion: the use of basic types may increase the complexity of the system to a certain extent, and make the pits become more and more. There is also this way of using packaging types to define variables, blocking the operation of the program through exceptions, and then this online problem can be immediately identified. But if we use basic data types, the system may think that there is no exception and continue to run. It can only passively test that there is a problem, and what is more, if such a problem occurs online, I think it might... all understand.
2) Scene two 🌆
Simply put, if we customize a Student class, one of the attributes is the score.
If you use Integer instead of int to define, one test, the student may not take the test, the value is null, or it may be tested, but the test is 0 points, the value is 0.
public class Student {
private Integer score;
private int score;
}
Please note: The two expression states of If we use the packaging type, null proves that there is no test, and 0 proves that the test scores 0; but if we use the basic type, the two cases are the same and cannot be distinguished.
Four, NPE problem🗽
[Recommended] Preventing NPE is the basic training of programmers. Pay attention to the scenes of NPE:
NPE, refers to return null values for basic types of data, to prevent NPE is a programmer's basic recuperation. All NPE scenes:
The return type is a basic data type. When returning an object of the package data type, automatic unboxing may produce NPE.
public int f() { return Integer 对象; } 如果为null,自动解箱抛NPE。
- The query result of the database may be null.
- Even if the elements in the collection are isNotEmpty, the data elements taken out may be null.
- When a remote call returns an object, a null pointer judgment is always required to prevent NPE.
- For the data obtained in the Session, it is recommended to perform NPE checks to avoid null pointers.
- Cascade calls obj.getA().getB().getC(); a series of calls can easily generate NPE.
Positive example: Use the Optional class of JDK8 to prevent NPE problems. Understand 👉 JDK8 Optional class
Five, talking to oneself🚏
Hello, I am a blogger
Ning : 161692702a89bc segmentfault homepage
I hope this article can make you feel rewarded! ! !
I wish
us: When we meet another day, we will have achieved something.
Welcome everyone to discuss the problem together😁, lie down🛌
Reference: Alibaba Java Development Manual
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。