为什么理解Java中引用类型和原始类型之间的区别很重要?

时间:2021-05-11 16:26:01

I'm after a list of issues in Java that cannot be properly understood without first understanding the difference.

我正在阅读Java中的一些问题,如果没有先了解它们之间的差异就无法正确理解。

For example:

  • Passing parameters to methods
  • 将参数传递给方法

  • Precisely what limitations are imposed by using "final" on a variable declaration.
  • 确切地说,在变量声明中使用“final”会产生什么限制。

  • What == means
  • 什么==的意思

Any more?

EDIT: this question doesn't seem to make sense to people. The motivation behind it is that my experience as a beginner learning Java was that the difference between, for example:

编辑:这个问题似乎对人们没有意义。它背后的动机是我作为初学者学习Java的经验是两者之间的区别,例如:

int i = 1;

and

Set<Thing> set = new Set<Thing>();

was not obvious. Now I know once you picked Java up you take it for granted and don't think about it but I maintain that it's something that beginners struggle with, especially if they don't come from a programming background. I think it's something that will often need to be addressed when teaching Java.

并不明显。现在我知道,一旦你选择了Java,你就会把它视为理所当然,不要考虑它,但我认为这是初学者努力的事情,特别是如果他们不是来自编程背景。我认为在教授Java时经常需要解决这个问题。

EDIT: still no good. Feel free to close.

编辑:仍然没有好处。随意关闭。

3 个解决方案

#1


I think the most important point is to understand that references in Java do not contain the objects themselves but only an address (or pointer or whatever you want to call it) to the actual objet itself. Thus it is crucial to understand that you always have two things:

我认为最重要的一点是要理解Java中的引用不包含对象本身,而只包含实际objet本身的地址(或指针或任何你想要调用它的东西)。因此,了解您总是有两件事是至关重要的:

  1. The reference (which is stored in a variable)
  2. 引用(存储在变量中)

  3. The object (which cannot be stored in a variable - only referenced by a reference)
  4. 对象(不能存储在变量中 - 仅由引用引用)

If you understand this key concept, things like the == are quite easy to understand because now you know you're just comparing the references and not the objects.

如果你理解这个关键概念,那么像==这样的东西很容易理解,因为现在你知道你只是在比较引用而不是对象。

In summary: Before teaching how to use objects explain what the difference is between an object and its reference.

总结:在教授如何使用对象之前,解释对象与其引用之间的区别。

#2


I think your question is - If you were to explain a Java feature to someone then for better explaination you would need to compare it with other feature.

我认为您的问题是 - 如果您要向某人解释Java功能,那么为了更好地解释,您需要将其与其他功能进行比较。

For example,

== Vs .equals()

== Vs .equals()

That way I think -

那样我觉得 -

  • static Vs non-static
  • 静态Vs非静态

  • interface Vs class
  • 接口Vs类

  • synchronized Vs non-synchronized
  • synchronized Vs非同步

Is this what you looking for?

这是你在找什么?

#3


I agree with Benedikt Eger that the relationship between references and objects is the most important concept to understand.

我同意Benedikt Eger的观点,即引用和对象之间的关系是最重要的理解概念。

For your list of issues: the "=" operator can sometimes cause confusion in people, since to beginners it can appear to "copy" primitives but "share" objects. This is much less of a problem in Java than it is in C++, since "=" will never copy an object (Java does not put objects on the stack). However, I have seen "=" trip up beginners, especially when they think "=" will create a duplicate copy of an array.

对于你的问题列表:“=”运算符有时会引起人们的混淆,因为对于初学者来说,它似乎可以“复制”原语但“共享”对象。这在Java中比在C ++中要小得多,因为“=”永远不会复制对象(Java不会将对象放在堆栈上)。但是,我看到“=”绊倒了初学者,特别是当他们认为“=”会创建一个数组的副本时。

#1


I think the most important point is to understand that references in Java do not contain the objects themselves but only an address (or pointer or whatever you want to call it) to the actual objet itself. Thus it is crucial to understand that you always have two things:

我认为最重要的一点是要理解Java中的引用不包含对象本身,而只包含实际objet本身的地址(或指针或任何你想要调用它的东西)。因此,了解您总是有两件事是至关重要的:

  1. The reference (which is stored in a variable)
  2. 引用(存储在变量中)

  3. The object (which cannot be stored in a variable - only referenced by a reference)
  4. 对象(不能存储在变量中 - 仅由引用引用)

If you understand this key concept, things like the == are quite easy to understand because now you know you're just comparing the references and not the objects.

如果你理解这个关键概念,那么像==这样的东西很容易理解,因为现在你知道你只是在比较引用而不是对象。

In summary: Before teaching how to use objects explain what the difference is between an object and its reference.

总结:在教授如何使用对象之前,解释对象与其引用之间的区别。

#2


I think your question is - If you were to explain a Java feature to someone then for better explaination you would need to compare it with other feature.

我认为您的问题是 - 如果您要向某人解释Java功能,那么为了更好地解释,您需要将其与其他功能进行比较。

For example,

== Vs .equals()

== Vs .equals()

That way I think -

那样我觉得 -

  • static Vs non-static
  • 静态Vs非静态

  • interface Vs class
  • 接口Vs类

  • synchronized Vs non-synchronized
  • synchronized Vs非同步

Is this what you looking for?

这是你在找什么?

#3


I agree with Benedikt Eger that the relationship between references and objects is the most important concept to understand.

我同意Benedikt Eger的观点,即引用和对象之间的关系是最重要的理解概念。

For your list of issues: the "=" operator can sometimes cause confusion in people, since to beginners it can appear to "copy" primitives but "share" objects. This is much less of a problem in Java than it is in C++, since "=" will never copy an object (Java does not put objects on the stack). However, I have seen "=" trip up beginners, especially when they think "=" will create a duplicate copy of an array.

对于你的问题列表:“=”运算符有时会引起人们的混淆,因为对于初学者来说,它似乎可以“复制”原语但“共享”对象。这在Java中比在C ++中要小得多,因为“=”永远不会复制对象(Java不会将对象放在堆栈上)。但是,我看到“=”绊倒了初学者,特别是当他们认为“=”会创建一个数组的副本时。