java.lang.Object o = 1;//为什么会编译?
我正在做其中一项在线 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
"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.因为
"1"
是String
的实例,并且自 1.5 起1
可以自动装箱为Integer
;这两种类型都是Object
的子类型。在引入自动装箱之前,Object o = 1;
无法编译。要充分利用此学习体验,您应该了解
Object
的getClass()
方法。通过添加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 aString
, and since 1.51
is auto-boxable to anInteger
; both types are subtypes ofObject
. 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
'sgetClass()
method. By addingSystem.out.println(o.getClass().getName())
, you can also print the name of the class that the object referred to byo
belongs to. In your case, they arejava.lang.String
(for(Object) "1"
) andjava.lang.Integer
(for(Object) 1
).Just for completion, I should mention that you can now also do
Object o = false;
.好吧,第一种情况“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.这是因为
o
是Object
类型。 Java 中的每个对象都扩展了Object
类。所以...当您说Object o = 1
时,它会将 1 从int
转换为Integer
,这是一个Object< /代码>。同样,“1”是一个
String
,它是一个Object
。在这两种情况下,在Object
上调用System.out.println
都会调用Object
的toString
方法。在这两种情况下,都会打印 1。This is because
o
is of typeObject
. Every object, in java, extends the classObject
. So... when you sayObject o = 1
, it converts 1 to fromint
toInteger
, which is anObject
. Similarly, "1" is aString
which is anObject
. In both cases, callingSystem.out.println
on anObject
invokes theObject
stoString
method. In both cases, it will print 1.您可以将
Object o = everything;
放在其中anything
是任何对象,因为所有类都派生自Object
类。由于 java 1.5 中的自动装箱功能,它可以与基元一起使用。You can put
Object o = anything;
whereanything
is any object because all classes derive from theObject
class. It works with primitives because of autoboxing feature which came in java 1.5.