虽然是C#技术栏目,但对JAVA完全适用。
但所以答案没有一个是正确且完整的。
下面是我的答案,我有足够的自信说这是真正正确的答案,除非JVM和CLR规范重新修改了。
正常情况下生成一个对象后主动让 obj = null;
没有任何作用,但也没有任何害处(除了说明程序员对编译器的无知和代码看起来比较幼稚)
编译器比你更清楚在什么位置对象已经失去引用。
object o = new object();
stmt1;
o.xxx();
stmt2;
stmt3;
o = null;
其实编译器在为o.xxx()后面就知道它(原来o指向的那个对象)是可回收的,
比你在两个语句后再o=null更准确!
什么时候需要 o = null;?
o = null;有时是必须的,就是在底层容器中保存对象然后外部封装类pop出来的时候。
比如一个Queue,它的实现是用一个成员变量数组来保存对象
class MyQueue{
private object[] arr = new object[100];
private int tail;
private int head;
private int count;
............;
}
当压入元素时:
if(count >= 100) throw 一个没有空间的异常
this.arr[this.tail] = o;
tail = (tail + 1) % this.arr.length; //如果到数组最后则从头开始
count++;
但当弹出时:
if(count <= 0) return null;
object o = this.arr[this.head];
this.arr[this.head] = null;
this.head = (this.head + 1) % this.arr.length;
count--;
this.arr[this.head] = null;这里如何不主动设置null.则这个对象被pop出去后
arr中始终有一个引用指向这个对象而使对象不能回收,如果你只是临时放一个对象到
Queue中马上就POP出来使用,本来你以为使用后可以被回收,却因为arr中同时有一个
引用指向它,如果这个引用不被其它元素替换掉,则原来那个对象一直不能被回收。
凡是类似的有底层容器和外部封装同时对一个对象持有多个引用的,在你用完了这个对象后
你应该把其它指向它的引用手工置为null;其它情况(指正常的只有一个引用时)不应该置为null
(完全是多余)
其实上面的情况相当于: object o = new object();
o1 = o;
o.xxx();//使用完了,使用完了的意思是我知道o1也不需要再操作这个对象
//我本来想在这个位置对象进入可回收的状态,但因为o1还没有超出作用域,
//所以不能被回收,所以要手工设置o1 = null;
//这样这个对象才会被回收。
}
}
分享到:
相关推荐
主要介绍了还在用if(obj!=null)做非空判断,带你快速上手Optional,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
代码如下: function dealNull(obj){ for(var i in obj){ if(null == obj[i] || ‘null’ == obj[i]){ obj[i]=”; }else if(‘object’ == typeof obj[i]){ dealNull(obj[i]); } } }; // 测试的对象 var a={}; a.aa=...
private static $obj = NULL; private function __construct() { } private function __clone() { } public static function instance() { if(!is_object(self::$obj)) { self::$obj = new self(); } return self::$...
函数如下: 代码如下: foreach = function (obj, insp){ if(obj== null && obj.constructor != Array){ return []; } //obj是要处理的数组,obj==null 表示对象尚未存在;obj.constructor != Array 表示对象obj的...
后台实现代码如下: public bool Login(string name, string pass) // 判断用户名和密码是否正确 { string sqlString = "select count(*) ...=null && obj.ToString()!="0") return true; else return false; }
Object obj = null; try { Statement stat = conn.createStatement(); ResultSet re = stat.executeQuery(sql); if (re.next()) { obj=re.getString(1); } } catch (Exception ex) { ex....
if(img&&typeof(img.ctrlid)==="string") {var ele,obj=img.parentNode; img.onmousedown=null; document.onmousemove=null; document.onmouseup=null; ele=document.getElementById(img.overid); if(ele) ...
Object obj = null; if (classzz == Integer.class) { if (src.contains(".")) { src = src.substring(0, src.indexOf(".")); obj = Integer.valueOf(src); } } if (classzz == Double.class) obj = ...
=null && Convert.ToBoolean(obj)==true){ //Do something}上面的代码的意图很明显,先判断obj 是不是null,如果不是null 的话,再调用Convert.ToBoolean方法,判断是不是True。 上面的方法没有任何问题,但有一点...
if(obj== null && obj.constructor != Array){ return []; } //obj是要处理的数组,obj==null 表示对象尚未存在;obj.constructor != Array 表示对象obj的属性的构造函数不是数组; //constructor属性始终指向...
obj = request == null ? arr:(request.getParameter((String)key) == null ? arr:arr[0]); } else { obj = map.get(key); } return obj; } public static boolean hasVal(PageData pd,String key){ return...
return obj===null&&type===’Null’|| obj===undefined&&type===’Undefined’|| toString.call(obj).slice(8,-1)===type; } //原文中有小括号包裹每个逻辑与运算,但根据运算符优先级,括号可以省略 //第一行声明...
方法一 代码如下: function clone(obj){ var o; switch(typeof obj){ case ‘undefined’... case ‘object’ : if(obj === null){ o = null; }else{ if(obj instanceof Array){ o = []; for(var i = 0, len = obj.leng
存储过程的应用+数据库 SqlParameter[] pr ={new SqlParameter("@name",SqlDbType.VarChar,50) ,new SqlParameter("@pwd",SqlDbType....= null) return Convert.ToInt32(obj.ToString()); else return 0; } }
Face normalFace = null; // Options opt = new Options(); opt.ComputeReferences = true; opt.DetailLevel = Autodesk.Revit.DB.DetailLevels.Medium; // GeometryElement e = wall.get_Geometry(opt); ...
" where DepCd = '" + SqlObject.Encode(obj2.DepCd) + "' and CreatedSite = '" + obj2.CreatedSite + "'"; return SQLAPI.ExecuteScalar(SQLAPI.ENCRYPT_CONN_STRING(), s, null, CommandType.Text); } else ...
fz = null; } catch (Exception) { throw; } } /// <summary> /// 解压缩 /// </summary> /// <param name="file">待解压文件名(包含物理路径)</param> /// <param name="dir"> 解压到哪个目录中...
这是在阅读underscore(1.3.3)源码中看到的,它的each方法 代码如下: var each = _.each = _.forEach = function(obj, iterator, context) { if (obj == null) return; if (nativeForEach && obj.forEach === ...
return(typeof(obj)=='undefined'||obj==''||obj==null); } //直接基于jQuery的扩展,去除html标签,保留内容 $.htmlContent = function(tag){ var reTag = /<(?:.|\s)*?>/g; return tag.replace(reTag,)...
javascript中一些util方法汇总 ... if (obj == undefined || obj == null || new String(obj).trim() == '') { return true; } else { return false; } } /** * 判断非空 * @param obj * @retu