对象 obj = new Obj(); 和对象 obj; 之间有什么区别?

huangapple 未分类评论50阅读模式
英文:

What is the difference between Obj obj =new Obj (); and Obj obj;

问题

在Java中,`Obj obj = new Obj();``Obj obj;`(未进行实例化之间有什么区别

```java
public class Obj {

	int a = 1;
	
	public static void main(String[] args) {
		
		Obj obj = new Obj();
		Obj obj1;
	
		// TODO Auto-generated method stub

	}
}
英文:

What is the difference between Obj obj =new Obj (); and Obj obj; (without instantiation)

in java

public class Obj {

	int a =1;
	
	public static void main(String [] args) {
		
		Obj obj =new Obj ();
		Obj obj1;
	
		// TODO Auto-generated method stub

	}
}

答案1

得分: -1

Obj obj = new Obj(); - 在这里创建了对象,并且创建了对 Obj 的引用,即 obj。由于我们使用了 new Obj(),在这种情况下将分配内存。

Obj obj1; - 在这里创建了 Obj 的引用,即 obj1。在这种情况下不会分配内存。

希望这能帮助您。

英文:

Obj obj = new Obj(); - Here the object is created and the reference of Obj is created which is obj. Since we have used new Obj(), memory will be allocated in this case.

Obj obj1; - Here reference of Obj is been created which is obj1. In this case memory will not be allocated.

Hope this helps

huangapple
  • 本文由 发表于 2020年4月8日 23:42:32
  • 转载请务必保留本文链接:https://java.coder-hub.com/61104616.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定