java.lang.Object o = 1;//为什么会编译?

发布于 2024-08-23 22:03:18 字数 851 浏览 6 评论 0原文

我正在做其中一项在线 Java 测试,有人问我这个问题:

问:请指出正确的分配:

Long l = 1; 
Double d = 1;
Integer i = 1;
String s = 1;
Object o = "1";
System.out.println(o);
o = 1;
System.out.println(o);

请在继续之前先尝试一下。

好吧,我可以告诉你我错了,我调查了它并发现:

//Long l = 1; //cannot widen and then box
Long ll = 1L;//no need to widen, just box
//Double d = 1;//cannot widen and then box
Double dd = 1d;//no need to widen, just box
Integer i = 1;//no need to widen, just box
//String s = 1;//cannot do implicit casting here

Object o = "1";//this compiles and is just plain weird 
System.out.println(o);//output is 1
o = 1;//this also compiles and is also weird 
System.out.println(o);//output is 1

有人可以告诉为什么:

Object o = 1;Object o = "1";

编译并两种情况下都输出 1,这让我很困惑。

非常感谢

I was doing one of these online Java tests and I was asked this question:

Q: Indicate correct assignment:

Long l = 1; 
Double d = 1;
Integer i = 1;
String s = 1;
Object o = "1";
System.out.println(o);
o = 1;
System.out.println(o);

Please try it yourself before you go any further.

Well I can tell you I got it wrong, I investigated it and found:

//Long l = 1; //cannot widen and then box
Long ll = 1L;//no need to widen, just box
//Double d = 1;//cannot widen and then box
Double dd = 1d;//no need to widen, just box
Integer i = 1;//no need to widen, just box
//String s = 1;//cannot do implicit casting here

Object o = "1";//this compiles and is just plain weird 
System.out.println(o);//output is 1
o = 1;//this also compiles and is also weird 
System.out.println(o);//output is 1

Can someone tell why:

Object o = 1; and Object o = "1";

compile and output 1 in both cases, this is puzzling me.

Many thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

韬韬不绝 2024-08-30 22:03:19

"1" 是 String 类的实例,而 String 是 Java 中 Object 类的子类(与任何其他类一样)。 1 被装箱为一个 Integer,它也是从 Object 派生的。

"1" is an instance of String class, and String is a subclass of Object class in Java (as any other class). 1 is boxed into an Integer, which is also derived from Object.

帅气称霸 2024-08-30 22:03:19

因为 "1"String 的实例,并且自 1.5 起 1 可以自动装箱为 Integer ;这两种类型都是Object 的子类型。在引入自动装箱之前,Object o = 1; 无法编译。

要充分利用此学习体验,您应该了解 ObjectgetClass() 方法。通过添加System.out.println(o.getClass().getName()),还可以打印o引用的对象所属的类的名称到。在您的情况下,它们是 java.lang.String (对于 (Object) "1")和 java.lang.Integer (对于 <代码>(对象)1)。

为了完整起见,我应该提到您现在还可以执行 Object o = false;

Because "1" is an instance of a String, and since 1.5 1 is auto-boxable to an Integer; both types are subtypes of Object. Before autoboxing was introduced, Object o = 1; would not compile.

To get the most out of this learning experience, you should be aware of Object's getClass() method. By adding System.out.println(o.getClass().getName()), you can also print the name of the class that the object referred to by o belongs to. In your case, they are java.lang.String (for (Object) "1") and java.lang.Integer (for (Object) 1).

Just for completion, I should mention that you can now also do Object o = false;.

说不完的你爱 2024-08-30 22:03:19

好吧,第一种情况“1”是一个 String 文字,因此是对象的子类,因此可以分配给它。作为一个字符串,它输出1是比较简单的。

在第二种情况下,发生自动装箱。 Integer 是对象的子类,因此可以分配给它。类似地,输出 1 就完全有意义了。

Well, the first case "1" is a String literal, so a subclass of object, hence assignable to it. As a string, it output of 1 is relatively simple.

In the second case, auto-boxing is occurring. Integer is a subclass of object, hence assignable to it. Similarly, the output of 1 then makes perfect sense.

荒路情人 2024-08-30 22:03:19

这是因为 oObject 类型。 Java 中的每个对象都扩展了 Object 类。所以...当您说 Object o = 1 时,它会将 1 从 int 转换为 Integer,这是一个 Object< /代码>。同样,“1”是一个String,它是一个Object。在这两种情况下,在 Object 上调用 System.out.println 都会调用 ObjecttoString 方法。在这两种情况下,都会打印 1。

This is because o is of type Object. Every object, in java, extends the class Object. So... when you say Object o = 1, it converts 1 to from int to Integer, which is an Object. Similarly, "1" is a String which is an Object. In both cases, calling System.out.println on an Object invokes the Objects toString method. In both cases, it will print 1.

酒浓于脸红 2024-08-30 22:03:19

您可以将 Object o = everything; 放在其中 anything 是任何对象,因为所有类都派生自 Object 类。由于 java 1.5 中的自动装箱功能,它可以与基元一起使用。

You can put Object o = anything; where anything is any object because all classes derive from the Object class. It works with primitives because of autoboxing feature which came in java 1.5.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文