标签:style blog color io ar java sp div on
1 import java.util.ArrayList; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 StringBuffer buffer = new StringBuffer("Hello World."); 7 8 StringBuffer buffer_other = buffer; 9 10 buffer.append("Me too."); 11 12 if (buffer == buffer_other) { 13 System.out.println("OK."); 14 } 15 16 17 //buffer.append(null); 报错 18 String str = null; 19 System.out.println(buffer.append(str)); 20 //输出:Hello World.Me too.null 当字符串为null时,StringBuffer的append()会把null 转化为字符串null加在结尾 21 22 ArrayList<String> list = new ArrayList<>(); 23 list.add(str); 24 System.out.println(list.get(0));//输出为null 25 list.add(null); 26 System.out.println(list.get(1));//输出为null 27 28 /********************************/ 29 String string = null; 30 String string2 = null; 31 ArrayList<String> arrayList = null; 32 if (string == string2) { 33 System.out.println("Y");//输出Y 34 } 35 if (string == null) { 36 System.out.println("Y");//输出Y 37 } 38 if (null == null) { 39 System.out.println("Y");//输出Y 40 } 41 //string == arrayList 是语法错误 42 43 //if (string.equals(string2)) {//NullPointerException 44 // System.out.println("Y"); 45 //} 46 47 } 48 }
标签:style blog color io ar java sp div on
原文地址:http://www.cnblogs.com/wanghui390/p/3998975.html