Self-summary, what is wrong or not in place, please point out, thank you!
Objective: To master the occurrence of null values in Java and avoid nullpointerexception
Code Environment Readiness: JUnit needs to be introduced and the code package part changed to its own package name. Unit tests can then be done.
Concepts that need to be understood:
Packing class: For example: Integer,long,double,boolean, etc., corresponding basic type: Int,long,double,boolean etc.
Auto-Boxing: Java provides the ability to automatically convert variables of the underlying type into variables of the wrapper type.
Automatic Unpacking: Java provides the ability to automatically convert a variable of a wrapper type into a variable of the basic type.
On code: Javanull.java
Packagecom.core.test;Importorg.junit.Test; Public classJavanull {/*Java version:1.6*/ Public StaticString name; //simulate boxing (this case is only used to help understand the automatic boxing process)@Test Public voidtest001 () {intA = 10 ; Integer A=integer.valueof (a); Put (A); } //Simulate unpacking (this case is only used to help understand the automatic unpacking process)//if the wrapper type variable value is null, converting to a variable of the basic type will not be assigned a corresponding initial value, it will be reported nullpointexception@Test Public voidtest002 () {Integer A=NULL; intA = A.intvalue ();//nullpointexceptionput (a); } //object of type string determines non-null and non-empty string, as shown in case test003@Test Public voidtest003 () {String name=NULL ; if(NULL!=name &&!Name.isempty ()) {Put (NULL); } } //The static variable is not initialized, but the null pointer exception is not reported because the virtual machine initializes the static variable value to null while loading the class@Test Public voidtest004 () {put (name); } //When the value of an integer object exceeds 127, the reference object no longer points to the original memory address, so the case test005 prints the result: "A = = B", and test006 prints the result: "A! = B"//when the variable is a reference type, "= =" Determines whether the variable points to the same memory address, ". Equal ()" Determines whether the value of the variable is equal@Test Public voidtest005 () {Integer a= 100; Integer b= 100; if(A = =b) {Put ("A = = B"); }Else{put ("A! = B"); }} @Test Public voidtest006 () {Integer a= 128; Integer b= 128; if(A = =b) {Put ("A = = B"); }Else{put ("A! = B"); } } //conversion (null value is the default value for all reference types and can be cast to any object type)//conjecture: There is a potential null class concept in Java, a subclass of variables of all reference types, and test008 prints the result: "Test--null", which indicates that there is a potential null.tostring method@Test Public voidtest007 () {String a= (String)NULL; Double b= (Double)NULL; } @Test Public voidtest008 () {String temp=NULL; Put ("test--" +temp); } //a static method can be called directly by the class name, just to test//if the object test value is null, nullpointexception is not reported when a static method is called with the object, but a non-static method is called Nullpointexception@Test Public voidtest009 () {javanull test=NULL; Test.getstaticmethod (); Test.getnotstaticmethod ();//nullpointexception } //Null value comparison returns true@Test Public voidtest010 () {if(NULL==NULL) {put ("Yes"); }Else{put ("No"); } } //Case test011 and test012 for the use of instanceof//instanceof Description: If object is an instance of class, the instanceof operator returns TRUE. Returns False if object is not an instance of the specified class, or if object is null@Test Public voidtest011 () {//Integer num = null;Integer num =NewInteger (0);; if(numinstanceofInteger) {Put ("Yes"); }Else{put ("No"); } } //instanceof can match array@Test Public voidtest012 () {String str[]= {"abc", "BCD"}; if(strinstanceofstring[]) {Put ("Yes"); }Else{put ("No"); } } Public Static voidGetstaticmethod () {put ("Staticmethod"); } Public voidGetnotstaticmethod () {put ("Notstaticmethod"); } Public Static voidput (Object obj) {System.out.println (obj); } }
The above is my summary, reference article: http://www.importnew.com/14229.html
Http://www.cnblogs.com/danne823/archive/2011/04/22/2025332.html
Summary of NULL values in Java