Java 中的对象与引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5160485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:45:28  来源:igfitidea点击:

Object vs Reference in Java

java

提问by ajay

In Java, if I declare,

在 Java 中,如果我声明,

MyClass obj;

Is obj called a "reference" or an "object". I am not instantiating class here.

obj 是称为“引用”还是“对象”。我不是在这里实例化类。

回答by rfeak

obj is a Reference to an instance of MyClass.

obj 是对 MyClass 实例的引用。

Currently, that Reference is NULL because you haven't assigned it to refer to any instance.

目前,该 Reference 为 NULL,因为您尚未将其分配给任何实例。

Technically MyClass must be a subclass of Object, so it is possible to say that obj is a Reference to an instance of Object as well.

从技术上讲,MyClass 必须是 Object 的子类,因此可以说 obj 也是对 Object 实例的引用。

回答by Durai Amuthan.H

Reference: A variable that points to some object in memory. It is stored in stack they can be contained in other objects (then they are not really variables, but fields), which puts them on the heap also.

引用:指向内存中某个对象的变量。它存储在堆栈中,它们可以包含在其他对象中(然后它们不是真正的变量,而是字段),这也将它们放在堆中。

Object: An instance of class that is created dynamically. It is stored in heap

Object:动态创建的类的实例。它存储在堆中

Example:

例子:

MyClassI aObj,aObj1;

aObj=new MyClass2();

At first line aObj and aObj1 are references

第一行 aObj 和 aObj1 是引用

At second line aObj referencing to object of MyClass2(New operator creates an object of Myclass2 and its address is assigned to aObj).

在第二行 aObj 引用了 MyClass2 的对象(New 操作符创建了一个 Myclass2 的对象,并且它的地址被分配给了 aObj)。

To understand even better consider a class Car which has driverName as a member.

为了更好地理解,考虑一个以 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);

回答by Jean-Christophe Fortin

In computer science, a reference is a value that enables a program to indirectly access a particular data item, such as a variable or a record, in the computer's memory or in some other storage device. The reference is said to refer to the data item, and accessing that data is called dereferencing the reference.

In computer science, an object is any entity that can be manipulated by the commands of a programming language, such as a value, variable, function, or data structure. (With the later introduction of object-oriented programming the same word, "object", refers to a particular instance of a class)

在计算机科学中,引用是一个值,它使程序能够间接访问计算机内存或某些其他存储设备中的特定数据项,例如变量或记录。引用被称为引用数据项,访问该数据称为取消引用引用。

在计算机科学中,对象是可以由编程语言的命令操作的任何实体,例如值、变量、函数或数据结构。(与后面介绍的面向对象编程同一个词,“对象”,指的是一个类的特定实例)

so obj is a reference and new MyClass()can be seen as an object

所以 obj 是一个引用,new MyClass()可以看作是一个对象

回答by Jeff

Sometimes you'll hear people say "Design an method that takes an object as a parameter and..."

有时你会听到人们说“设计一个方法,将一个对象作为参数,然后……”

If you're new to programming, and especially with Java, such statements can lead to some confusion. These people are using the word "object" to refer to an instance of a class in very general OOP terms, not necessarily Java specific.

如果您不熟悉编程,尤其是 Java,则此类语句可能会导致一些混淆。这些人使用“对象”这个词来指代非常通用的 OOP 术语中的类的实例,不一定是特定于 Java 的。

When we're talking specifics about Java and the code you have there, it is a reference to an instance of MyClass, which is NULL.

当我们谈论有关 Java 的细节以及您在那里拥有的代码时,它是对 的实例的引用,该实例MyClass为 NULL。

回答by user207421

'obj' is a variable. It holdseither a reference or null. If it holds a reference, that refers to an object.

'obj' 是一个变量。它持有一个引用或空值。如果它持有一个引用,那是指一个对象。

回答by Ian Dallas

obj is a Reference of type MyClass. The current reference does not point to anything (ie: null).

obj 是 MyClass 类型的引用。当前引用不指向任何内容(即:null)。

回答by MahmoudPrime

  • In Java, all objects are accessed by reference, and you never have direct access to the object itself.
  • reference :- is a variable that has a name and can be used to access the contents of an object, A reference can be assigned to another reference passed to a method, or returned from a method. All references are the same size, no matter what their type is Like "Object object ;".

  • object:- is an entity that's exists in memory allocated by the Java run time environment, An object sits on the heap and does not have a name Like "Object object=new Object();".

  • so MyClass obj Here is A referencereferred to Null.

  • We can summarize this principle with the following two rules:

    1. The type of the object determines which properties exist within the object in memory.
    2. The type of the reference to the object determines which methods and variables are accessible to the Java program.
  • 在 Java 中,所有对象都是通过引用访问的,您永远无法直接访问对象本身。
  • 引用 :- 是一个有名字的变量,可以用来访问对象的内容,一个引用可以分配给另一个传递给一个方法的引用,或者从一个方法返回。所有引用的大小都相同,无论它们的类型是“Object object ;”。

  • object:- 是存在于由 Java 运行时环境分配的内存中的实体,一个对象位于堆上并且没有名称,如“Object object=new Object();”。

  • 所以 MyClass obj 这里是一个引用Null的引用

  • 我们可以用以下两条规则来总结这个原则:

    1. 对象的类型决定了内存中对象内存在哪些属性。
    2. 对象引用的类型决定了 Java 程序可以访问哪些方法和变量。

回答by Vladimir

The referenceis a variable that has a name and can be used to access the contents of an object. A reference can be assigned to another reference, passed to a method, or returned from a method.

引用是一个有名字的变量,可以用来访问对象的内容。一个引用可以分配给另一个引用,传递给一个方法,或者从一个方法返回。

All referencesare the same size, no matter what their type is. An object sits on the heap and does not have a name. Therefore, you have no way to access an object except through a reference. Objects come in all different shapes and sizes and consume varying amounts of memory. An object cannot be assigned to another object, nor can an object be passed to a method or returned from a method. It is the object that gets garbage collected, not its reference.

所有参考的大小都相同,无论它们的类型是什么。对象位于堆上并且没有名称。因此,您无法通过引用访问对象。对象有各种不同的形状和大小,消耗的内存也各不相同。对象不能分配给另一个对象,也不能将对象传递给方法或从方法返回。垃圾收集的是对象,而不是它的引用。