Java???й?Null??9????
???????????? ???????[ 2014/12/31 11:12:54 ] ????????Java ??????? ???
????5?? ?κκ???null?????????Java????????????????????????????????????????Щ??????????????????????????????null?????????????????????????????int?????0???????????????false????????????????????????????
????Integer iAmNull = null;
????int i = iAmNull; // Remember - No Compilation Error
?????????????????????????ε????????????????????????????????????????HashMap??Integer?????????????????????????????????????????????????
import java.util.HashMap;
import java.util.Map;
/**
* An example of Autoboxing and NullPointerExcpetion
*
* @author WINDOWS 8
*/
public class Test {
public static void main(String args[]) throws InterruptedException {
Map numberAndCount = new HashMap<>();
int[] numbers = {3?? 5?? 7??9?? 11?? 13?? 17?? 19?? 2?? 3?? 5?? 33?? 12?? 5};
for(int i : numbers){
int count = numberAndCount.get(i);
numberAndCount.put(i?? count++); // NullPointerException here
}
}
}
?????????
????Exception in thread "main" java.lang.NullPointerException
????at Test.main(Test.java:25)
??????δ??????????????????д?????????????????????????????????г????????Σ?????Java?????е???????????????????????????????????????????????????????Map?????????????????????put?????????????????????y?int????Interger????????????????????????м?????????HashMap??get()??????????null????????0?????Integer????????null??????0??????null?????????int???????????????佫??????????????????£??????δ????????if??????????QA?????????У????????????????????????BOOM:-)
????6?????????????null????????????????instanceof??????????false??
????Integer iAmNull = null;
????if(iAmNull instanceof Integer){
????System.out.println("iAmNull is instance of Integer");
????}else{
????System.out.println("iAmNull is NOT an instance of Integer");
????}
?????????
????i
????AmNull is NOT an instance of Integer
????????instanceof?????????????????????????????????????????
????7????????????????÷?????????????????null???????????????????????????????????????????????????????t????????????????null???????????????????????????t????????????????????????????????????
????public class Testing {
????public static void main(String args[]){
????Testing myObject = null;
????myObject.iAmStaticMethod();
????myObject.iAmNonStaticMethod();
????}
????private static void iAmStaticMethod(){
????System.out.println("I am static method?? can be called by null reference");
????}
????private void iAmNonStaticMethod(){
????System.out.println("I am NON static method?? don't date to call me by null");
????}
?????????
????I am static method?? can be called by null reference
????Exception in thread "main" java.lang.NullPointerException
????at Testing.main(Testing.java:11)
????8????????null????????????????????????????κ??????????????public void print(Object obj)????????????print(null)????????????????????????????????????????Null??????????????????????е?print??????????????????????????????????????????????????????????null??????????
????9??????????==????!=?????????null????????????????????????????????????С???????????SQL?????????Java??null==null??????true???????????
public class Test {
public static void main(String args[]) throws InterruptedException {
String abc = null;
String cde = null;
if(abc == cde){
System.out.println("null == null is true in Java");
}
if(null != null){
System.out.println("null != null is false in Java");
}
// classical null check
if(abc == null){
// do something
}
// not ok?? compile time error
if(abc > null){
}
}
}
?????????
????null == null is true in Java
???????????Java??null??????????Java?????Щ???????ü??????????????????????????????????null????????null????????????δ????????????????????????????????????????null???????????????????????????????????????????????null???κ?????????????????????????java?????????null???????????κε?instance????????instance??????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11