java java中class数据类型的意义是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5630966/
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 12:00:19  来源:igfitidea点击:

What is the significance of class data type in java?

javatypes

提问by

Noob question.

菜鸟问题。

I have been programming at basic level for quite a while but I have trouble understanding class data type.

我已经在基础级别编程了很长一段时间,但我无法理解类数据类型。

when we say int a = 9;it means a is of data type int meaning it can contain only integers.

当我们说它int a = 9;意味着 a 是 int 数据类型时,意味着它只能包含整数。

like wise for String, boolean, double, float etc.

对 String、boolean、double、float 等也是如此。

But consider the following code:

但请考虑以下代码:

Class Node {

Node next = null;
int data;

public Node(int d){ data = d; }

void append(int d)
    {
        blah blah blah
        ..............
    }
}

What does Node next = null;mean? I can understand the effort to create an object with

什么Node next = null;意思?我可以理解创建一个对象的努力

Node next = new Node();

and then try to manipulate the next object.

然后尝试操作下一个对象。

采纳答案by Jon Skeet

This code:

这段代码:

Node next = null;

declares a variable of type Node. As Nodeis a class, the value of nextis always a reference - either to an object of type Nodeor a subclass, or the nullreference which doesn't refer to any object at all... and in this case the variable starts off with a value of null.

声明一个类型的变量Node。作为Node一个类, 的值next始终是一个引用 - 一个类型的对象Node或一个子类,或者null根本不引用任何对象的引用......在这种情况下,变量以一个值开始的null

It's really important to understand that the value of nextis nevera Nodeobject itself... it's only ever a reference. So suppose we have:

这是真正重要的是要认识到的价值next从来没有一个Node对象本身......这是只有永远的参考。所以假设我们有:

Node next = new Node(10);
Node foo = next;

Here, nextand fooare separate variables, each with independent values... but we've assigned the value of nextas the initial value of foo, which means they both refer to the same object. So if we print out foo.data, it will be 10.

在这里,nextfoo是单独的变量,每个变量都有独立的值……但是我们已经将 的值分配next为 的初始值foo,这意味着它们都指向同一个对象。所以如果我们打印出来foo.data,它将是 10。

I like to think of variables as pieces of paper - and in the case of variables of reference types, what's written on the piece of paper is the address of a house, or the word "null". If two pieces of paper have the same address written on them, they refer to the same house - but the two pieces of paper themselves are independent. Changing the valueof one variable (crossing out the current address and writing another one) doesn't change anything about the other variable... but changes to the house itself(e.g. painting a door red) are visible whichever piece of paper you use to get there.

我喜欢把变量想象成一张纸——对于引用类型的变量,纸上写的是房子的地址,或者“空”这个词。如果两张纸上写着相同的地址,它们指的是同一所房子——但两张纸本身是独立的。更改一个变量的(划掉当前地址并写入另一个)不会改变另一个变量的任何内容……但是无论您使用哪张纸,都可以看到房屋本身的更改(例如将门涂成红色)到那里。

Note that in your question, you've lumped Stringin with int, doubleand boolean... but whereas int, doubleand booleanare primitivetypes (where the value of a variable is simply the data itself - the number etc), Stringis a class, so it's a reference type. The value of a string variable isn't the text itself, but a reference.

请注意,在您的问题中,您将,和...混为一谈String,但是,和是原始类型(其中变量的值只是数据本身 - 数字等),是一个类,因此它是一个参考类型。字符串变量的值不是文本本身,而是引用。intdoublebooleanintdoublebooleanString

回答by Howard

The line

线

Node next = null;

means that you define a variable nextwhich holds only references to objects of type (the class) Node. Additionally you initialize it with the nullvalue which means no object created yet.

意味着你定义了一个变量next,它只保存对类型(类)对象的引用Node。此外,您使用null值对其进行初始化,这意味着no object created yet.

Those variables actually hold references to objects and nullis a special value valid only for objects which indicates that your variable is empty. This something like next.append()will fail with a nullpointer exception if nextis still null.

这些变量实际上持有对对象的引用,并且null是一个特殊值,仅对表明您的变量为空的对象有效。next.append()如果next仍然是,这会失败并出现空指针异常null

回答by Heisenbug

Node next = null; is a sort of pointer to a NodeObject. It's like declaring a variable not still initialized. In this case the object will probably be instantiated later. If you put an already existing object to a null value, if there aren't other references to the same object, then the garbage collector deletes it.

下一个节点 = null; 是一种指向 NodeObject 的指针。这就像声明一个尚未初始化的变量。在这种情况下,对象可能会在稍后实例化。如果您将一个已经存在的对象设置为空值,如果没有其他对同一对象的引用,则垃圾收集器将删除它。

I have been programming at basic level for quite a while but I have trouble understanding class data type. when we say int a = 9; it means a is of data type int meaning it can contain only integers. like wise for String, boolean, double, float etc.

我已经在基础级别编程了很长一段时间,但我无法理解类数据类型。当我们说 int a = 9; 这意味着 a 是数据类型 int 意味着它只能包含整数。对 String、boolean、double、float 等也是如此。

Pay attention that int and Integer are different in Java. int is a primitive type and it isn't an object. Integer is an object you can instantiate with new operator.

注意 Java 中的 int 和 Integer 是不同的。int 是一种原始类型,它不是一个对象。Integer 是一个可以使用 new 运算符实例化的对象。

回答by StKiller

Node next = nullmeans that this variable is initialized with null value, and doesn't point to any memory location.

Node next = null意味着这个变量被初始化为空值,并且不指向任何内存位置。

So there isn't yest added the next "neighbor" node.

所以没有添加下一个“邻居”节点。