当前位置:网站首页>Equals null pointer exception
Equals null pointer exception
2022-06-21 18:51:00 【LvhaoIT】
equals Null pointer exception
One 、bug describe
When looking for modified and unmodified items to compare , For before null Call the value of equals When comparing method with other values , Will cause a null pointer exception to be thrown
Two 、 Knowledge supplement
About null, A few things you have to know :
1、null yes Java Keywords in , image public、static、final. It's case sensitive , You can't put null It's written in Null or NULL, The compiler will not recognize them and report an error .
2、 Just like every primitive type has a default value , Such as int The default value is 0,boolean The default value is false,null Is the default for any reference type , Not strictly speaking, all object Default value for type . It's like you create a boolean variable , It will false As your default value ,Java Any reference variable in will null As default . This applies to all variables , Such as member variables 、 local variable 、 Instance variables 、 Static variables ( But when you use an uninitialized local variable , The compiler will warn you ). To prove this fact , You can observe the reference variable by creating a variable and printing its value .
3、null It's neither an object nor a type , It's just a special value , You can assign it to any reference type , You can also null Into any kind of .
4、null Can be assigned to a reference variable , You can't put null Assign to basic type variables , for example int、double、float、boolean. If you do that , The compiler will report an error .
5、 Anything that contains null The wrapper class of value is in Java A null pointer exception will be thrown when unboxing a basic data type .( for example Integer Unpack into int when )
6、 If the belt is used null Reference type variable for value ,instanceof The operation will return false.(instanceof: Used to indicate at run time whether an object is an instance of a particular class , for example :Integer num = null, that , call num instanceof Integer Will return to false)
7、 You cannot call a non static method to use a value of null Reference type variable of , It will throw a null pointer exception ; You can use static methods to use a value of null Reference type variable of , Because static methods use static binding , No null pointer exception will be thrown .
8、 You can use = = perhaps != Operation to compare null value , But you can't use other algorithms or logical operations , For example, less than or greater than . Follow SQL Dissimilarity , stay Java in null==null Will return true.
give an example
Suppose we now have a class , for example String, For the following code :
String str = null;
if (str.equals("Hello World!")){
System.out.println("Yes");
}else {
System.out.println("No");
}
Will throw a null pointer exception , however , If we change the conditional judgment in the second line to :
if ("Hello World!".equals(str))
It doesn't throw a null pointer exception , because String Of equals The method is not Static Method
perhaps
It can be used Objects.equals(a,b) To prevent the parameter from being null, If the two parameters are null Then return to true; One of them is null return false; If neither is null, Automatically called a.equals(b)
3、 ... and 、 reflection
To avoid memory overflow , We do not need to call methods outside the class , Generally, it is not added static keyword ( Because static methods are resident in memory , Its life cycle is consistent with that of the whole project ) therefore , When we assign initial values to variables , Try not to use null To give the initial value , If it must be null As an initial value , Then, during the operation , Be sure to Assert.isNull once , Try to avoid being right null To operate . go back to equals On the way , If an object instance whose initial value may be empty , call equals When the method is used , Be sure to follow “ Constant ”.equals( Variable ) perhaps After the input of .equals( Previous ). In this way, null pointer errors can be avoided as much as possible , Pay more attention at ordinary times , Develop habits , To prevent future hidden dangers .
Your efforts today , Just to make your suffering more valuable
边栏推荐
猜你喜欢
随机推荐
markdown写作软件:Ulysses v27
How typescript is constructed
缓存设计问题
Initialization of typescript class objects
让接口自动化测试灵动起来
[HCTF 2018]WarmUp
基于mitmproxy的录制回放接口测试工具
一篇文章彻底学会画数据流图
【Go语言】Go语言我们应该这样学~全网较全的学习教程
如何通过 dba_hist_active_sess_history 分析数据库历史性能问题
TypeScript编译生成文件对比
Module import method of node
左右两侧垂直带序号的时间轴
Unity3D-后期处理 Post-process Volume Profile
Servlet规范(一)
互联网通信流程
Stata quick check backup (personal notes)
Generics of typescript
存储器分级介绍
Six steps of JDBC programming









