英文:
If Person is class in java,then what is difference between 1)Person p; &2)Person p = new Person();?
问题
我是学生,正在学习Java。在解决问题时,我遇到了一个问题。我想知道这两个语句之间的区别。
-
第二个语句是创建新对象的声明。
-
我不了解关于第一个语句和两个语句之间的区别。
另外,请告诉我何时使用这两个语句。
如果在Java中Person是一个类,那么以下两个语句之间的区别是什么?
-
Person p;
-
Person p = new Person();
英文:
I am student learning about java.While solving problems I faced the issue.I want to know what is difference between these two statements.
-The second statement is declaration of new object.
-I don't know about the first statement and difference between two statements.
Also plz let me know when to use both the statements.
If Person is class in java,then what is difference between
-
Person p;
-
Person p = new Person();
答案1
得分: 3
Person p;
意味着你创建一个没有人对象的变量。
但是 Person p = new Person();
意味着你创建一个变量并将其引用为一个人对象。
英文:
Person p;
means you create a variable with no person object.
But Person p = new Person();
means you create a variable and reference it a person object
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论