gpt4 book ai didi

java - 原始类型作为 "normal"变量?

转载 作者:行者123 更新时间:2023-12-02 08:49:01 25 4
gpt4 key购买 nike

At this point, you might be wondering why you do not need to use new operator for primitives data types. The answer is that Java’s primitive types are not implemented as objects. Rather, they are implemented as “normal” variables. This is done in the interest of efficiency. For object versions of the primitive data types, refer Wrapper Classes.

“正常”变量是什么意思?为什么 Java 将原始类型视为“普通”变量?

最佳答案

我们确实使用 new 来使用构造函数实例化引用类型。我们不使用 new 来表示原始类型。我认为这就是引用的文本试图传达的内容。

但是:您不用 new 来声明整数和 float 之类的东西,不是因为它们不是作为对象实现的,而是因为该语言提供了文字语法供您使用。

在创建字符串时不要使用 new (除非你故意做一些奇怪的事情),因为该语言接受一种特殊的语法来描述字符串文字。但字符串绝对是引用类型。

有些语言中的一切实际上都是对象,这与 java 不同。它们看起来与 java 原始类型类似,但它们是对象,并且不需要 new 来实例化它们。

最好了解引用类型和原始值之间的区别。有人试图用自动装箱来掩盖它,但它仍然可能会导致问题,例如在使用流时,某些操作元素的方法可能会更好,因为它们不会强制对每个值进行装箱和拆箱。

关于java - 原始类型作为 "normal"变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60904653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com