If Person is class in java,then what is difference between 1)Person p; &2)Person p = new Person();?

huangapple go评论80阅读模式
英文:

If Person is class in java,then what is difference between 1)Person p; &2)Person p = new Person();?

问题

我是学生,正在学习Java。在解决问题时,我遇到了一个问题。我想知道这两个语句之间的区别。

  • 第二个语句是创建新对象的声明。

  • 我不了解关于第一个语句和两个语句之间的区别

另外,请告诉我何时使用这两个语句。

如果在Java中Person是一个类,那么以下两个语句之间的区别是什么?

  1. Person p;

  2. 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

  1. Person p;

  2. 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

huangapple
  • 本文由 发表于 2020年3月16日 00:02:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/60694825.html
匿名

发表评论

匿名网友

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

确定