标签:android des style blog http java
1 String a = null; 2 Object b = null; 3 System.out.println(a instanceof String);// false 4 System.out.println(b instanceof Object);// false
1 // 在栈内存中创建一个引用变量b; 2 // 在堆内存中申请空间存储“Use Memory”字符串; 3 // 把字符串的堆内存地址赋给变量b 4 String b = new String("Use Memory"); 5 6 // 只在栈内存中创建一个引用变量a 7 String a = null;
【注3】在不必立马指定对象实例的前提下,使用null可以尽可能节省空间。
1 // 声明对象并开辟空间保存User对象的值 2 User user = new User(1, "张三", 23); 3 4 // ... 经过一番处理后不再使用 5 6 // 此时把user对象置为null,会告知JVM该对象不再被使用,可以进行回收 7 // JVM在必要的时机会把user对象对应的空间回收 8 user = null;
【注4】在确定对象不再使用的时候,使用null可以及时告知JVM等待回收的资源
1 /* 2 ********************************************************** 3 *** *** 4 *** Copyright(C) 2014 Miao-Nodin. All rights reserved. *** 5 *** *** 6 *** Author: Miao-Nodin *** 7 *** *** 8 *** This is a source file of a part of a huge project. *** 9 *** Firstly, you must thank a pretty princess named *** 10 *** Lady喵~喵~ for bestowing on you this honour that ge *** 11 *** -ts this file. Who is she? She is my student. *** 12 *** *** 13 ********************************************************** 14 */ 15 package com.miao.base; 16 17 /** 18 * @Created: 2014年5月28日 by Miao-Nodin 19 * @Description: 演示使用null访问对象方法 20 * 21 * @Version:1.0 22 * @Update: 23 * 24 */ 25 public class Null { 26 private static final int LEVEL_ONE = 1; 27 private static final int LEVEL_TWO = 2; 28 private static final int LEVEL_THREE = 3; 29 30 private static void say(int level) { 31 switch (level) { 32 case LEVEL_ONE: 33 System.out.println("请你放尊重点,不要碰我!"); 34 break; 35 case LEVEL_TWO: 36 System.out.println("你再这样我要生气了!!!"); 37 break; 38 case LEVEL_THREE: 39 System.out.println("讨厌了啦~~~臭流氓~~~"); 40 break; 41 } 42 } 43 44 public static void main(String[] args) { 45 Null x = null; 46 x.say(x.LEVEL_ONE); //正常输出 47 ((Null) x).say(x.LEVEL_TWO); //正常输出 48 ((Null) null).say(x.LEVEL_THREE); //正常输出 49 } 50 }
1 /* 2 ********************************************************** 3 *** *** 4 *** Copyright(C) 2014 Miao-Nodin. All rights reserved. *** 5 *** *** 6 *** Author: Miao-Nodin *** 7 *** *** 8 *** This is a source file of a part of a huge project. *** 9 *** Firstly, you must thank a pretty princess named *** 10 *** Lady喵~喵~ for bestowing on you this honour that ge *** 11 *** -ts this file. Who is she? She is my student. *** 12 *** *** 13 ********************************************************** 14 */ 15 package com.miao.base; 16 17 /** 18 * @Created: 2014年6月4日 by Miao-Nodin 19 * @Description:演示null作为String类型空对象时的‘+‘操作 20 * 21 * @Version:1.0 22 * @Update: 23 * 24 */ 25 public class NullPlus { 26 27 public static void plus() { 28 String a = null; 29 String b = null; 30 String c = a + b; 31 if("nullnull".equals(c)){ 32 System.out.println("This String-Object is not null."); 33 } 34 } 35 36 /** 37 * @param args 38 */ 39 public static void main(String[] args) { 40 plus(); 41 } 42 }
1 /** 2 * @see java.lang.String#valueOf(java.lang.Object) 3 * @see #append(java.lang.String) 4 */ 5 public StringBuilder append(Object obj) { 6 return append(String. valueOf (obj)); 7 } 8 public StringBuilder append(String str) { 9 super.append (str); 10 return this ; 11 }
看得出,问题在String.valueOf(obj),null通过这个方法被转化为了"null",然后才有"nullnull"这种奇怪的问题。
1 public static void plus(); 2 Code: 3 Stack=3, Locals=3, Args_size=0 4 0: aconst_null 5 1: astore_0 6 2: aconst_null 7 3: astore_1 8 4: new #15; //class java/lang/StringBuilder 9 7: dup 10 8: aload_0 11 9: invokestatic #17; //Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String; 12 12: invokespecial #23; //Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V 13 15: aload_1 14 16: invokevirtual #26; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 15 19: invokevirtual #30; //Method java/lang/StringBuilder.toString:()Ljava/lang/String; 16 22: astore_2 17 23: ldc #34; //String nullnull 18 25: aload_2 19 26: invokevirtual #36; //Method java/lang/String.equals:(Ljava/lang/Object;)Z 20 29: ifeq 40 21 32: getstatic #40; //Field java/lang/System.out:Ljava/io/PrintStream; 22 35: ldc #46; //String This String-Object is not null. 23 37: invokevirtual #48; //Method java/io/PrintStream.println:(Ljava/lang/String;)V 24 40: return 25 LineNumberTable: 26 line 28: 0 27 line 29: 2 28 line 30: 4 29 line 31: 23 30 line 32: 32 31 line 34: 40 32 33 LocalVariableTable: 34 Start Length Slot Name Signature 35 2 39 0 a Ljava/lang/String; 36 4 37 1 b Ljava/lang/String; 37 23 18 2 c Ljava/lang/String; 38 39 StackMapTable: number_of_entries = 1 40 frame_type = 254 /* append */ 41 offset_delta = 40 42 locals = [ class java/lang/String, class java/lang/String, class java/lang/String ]
另外,从上述字节码中也可以看出:源码中三行声明语句在Stack中一共创建了三个引用。至于创建了几个实例,请自行分析。
【喵"的Android之路】【番外篇】有关于null的一些知识点,布布扣,bubuko.com
【喵"的Android之路】【番外篇】有关于null的一些知识点
标签:android des style blog http java
原文地址:http://www.cnblogs.com/monodin/p/3841212.html