Integer a = 10 在Java中是有效的语句。

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

Why is Integer a=10 a valid statement in java?

问题

根据我所知,Integer是int的包装类。因此,必须创建一个对象来初始化a的值。

Integer x = new Integer(10);

但是Integer x = 10;似乎也可以正常工作。有人可以解释一下吗?

英文:

As far as I know, Integer is a wrapper class for int. So an object must be created for initializing value of a.

Integer x=new Integer(10);

But Integer x=10; seems to work perfectly. Can someone please explain how?

答案1

得分: 3

由于自动装箱。

自动装箱是Java编译器在基本数据类型和它们对应的对象包装类之间进行的自动转换。例如,将int转换为Integer,将double转换为Double,依此类推。如果转换反向进行,这被称为拆箱。

查看文档https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html

英文:

Thanks to autoboxing.

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this is called unboxing.

See docs https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html

答案2

得分: 2

自动装箱 在大多数情况下自由地在原始类型和它们的包装类之间进行转换,并在JLS,§5.1.7. 装箱转换中有描述。

装箱转换将原始类型的表达式转换为相应的引用类型的表达式。具体来说,以下九种转换称为装箱转换:
[...]

  • 从类型 int 到类型 Integer

在运行时,装箱转换如下进行:

  • 如果 p 是类型为 int 的值,则装箱转换将 p 转换为类和类型为 Integer 的引用 r,使得 r.intValue() == p
    [...]

如果被装箱的值 p 是类型为 int 的整数字面值,其范围在 -128 到 127 之间(§3.10.1),或者是布尔字面值 true 或 false(§3.10.3),或者是位于 '\u0000' 和 '\u007f' 之间的字符字面值(§3.10.4),那么假设 a 和 b 是 p 的任意两个装箱转换的结果,总是成立 a == b。

英文:

Autoboxing freely converts between primitives and their wrapper classes in most contexts and is described in
JLS, §5.1.7. Boxing Conversion.

> Boxing conversion converts expressions of primitive type to corresponding expressions of reference type. Specifically, the following nine conversions are called the boxing conversions:
[...]
> * From type int to type Integer

> At run time, boxing conversion proceeds as follows:

> * If p is a value of type int, then boxing conversion converts p into a reference r of class and type Integer, such that r.intValue() == p
[...]

> If the value p being boxed is an integer literal of type int between -128 and 127 inclusive (§3.10.1), or the boolean literal true or false (§3.10.3), or a character literal between '\u0000' and '\u007f' inclusive (§3.10.4), then let a and b be the results of any two boxing conversions of p. It is always the case that a == b.

答案3

得分: 1

自动装箱

装箱 是将原始值包装为等效类型的类对象的过程。拆箱 指的是相反的过程,从对象转换为原始值。

例如,一个 int 原始类型可以被包装成一个 Integer 对象。

自动装箱 是Java编译器在幕后执行这个包装/解包过程。

自动装箱为我们应用程序开发人员简化了生活。在日常工作中,我们经常可以将原始类型和对象视为相同。但要知道它们并不相同。有些情况下,程序员必须意识到手头是原始类型还是对象。享受自动装箱的便利,但不要盲目操作。

您的示例代码

在您的示例中:

Integer x = 10 ;

编译器将 10 解析为一个 int 原始类型。然后,通过自动装箱,该原始类型被包装为一个 Integer 对象。 Integer 对象的实例化在幕后透明地进行。然后,将对该对象的引用分配给名为 x 的变量。

更多信息

请参考上面的链接到维基百科。并查看由Oracle提供的教程

纯粹的面向对象编程

请理解Java 并不 是纯粹的面向对象编程。如果是的话,我们将没有原始类型;我们只会有对象。因此,我们将不会有装箱。那么为什么Java的设计者包括了原始类型,给我们的生活增加了这种复杂性呢?这是为了便于从C和其他C类似语言移植代码。在Java发明的时候,容易移植是一个至关重要的要求。

英文:

Auto-boxing

Boxing is the process of wrapping a primitive value as an object of a class of the equivalent type. Unboxing refers to the opposite direction, going from object to primitive.

For example, an int primitive can be boxed as a Integer.

Auto-boxing is the Java compiler doing this wrapping/unwrapping behind the scenes.

Auto-boxing simplifies life for us app programmers. In our daily work we can often think of the primitive and object as being the same. But know that they are not the same. There are some situations where a programmer must be aware of whether a primitive or an object is in hand. Enjoy the convenience of auto-boxing, but do not operate blindly.

Your example code

In your example:

Integer x = 10 ;

…the 10 is parsed by the compiler as a int primitive. Then, via auto-boxing, that primitive is wrapped as an Integer object. The instantiation of the Integer object is happening behind the scenes, transparently. A reference to that object is then assigned to the variable named x.

More info

See the links above to Wikipedia. And see the tutorial by Oracle.

Pure OOP

Understand that Java is not purely object-oriented. If it were, we would have no primitives; we would have only objects. And therefore we would have no boxing.

So why did the designers of Java include primitives, adding this wrinkle of complexity to our lives? To facilitate porting code from C and other C-like languages. Easy porting was a crucial requirement at the time of Java’s invention.

huangapple
  • 本文由 发表于 2020年8月5日 00:21:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63251074.html
匿名

发表评论

匿名网友

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

确定