在Java中,如果我声明,
MyClass obj;
Run Code Online (Sandbox Code Playgroud)
obj被称为"参考"或"对象".我不是在这里实例化课程.
rfe*_*eak 19
obj是对MyClass实例的引用.
目前,该引用为NULL,因为您尚未将其指定为引用任何实例.
从技术上讲,MyClass必须是Object的子类,因此可以说obj也是对Object实例的引用.
Dur*_*n.H 14
参考:指向内存中某个对象的变量.它存储在堆栈中,它们可以包含在其他对象中(然后它们不是真正的变量,而是字段),这也将它们放在堆上.
对象:动态创建的类的实例.它存储在堆中
例:
MyClassI aObj,aObj1;
aObj=new MyClass2();
Run Code Online (Sandbox Code Playgroud)
在第一行aObj和aObj1是参考
在第二行aObj引用MyClass2的对象(New运算符创建Myclass2的对象,其地址分配给aObj).
要了解甚至更好地考虑具有driverName作为成员的类Car.
Car c1,c2;
c1.driverName="Andrew"
c2.driverName="Gabriel"
System.out.println(c1.driverName);//gives Andrew
System.out.println(c2.driverName);//gives Gabriel
c1=c2;
c2=null;
// gives gabriel because the address of c2 is copied to reference c1.
// the object is not nullified because c2 is just a reference when
// assigning null the address that is stored on c2 in nullified not
// the object it points..
system.out.println(c1.driverName);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17520 次 |
| 最近记录: |