Java 爪哇。getClass() 返回一个类,我怎么也能得到一个字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16385467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java. getClass() returns a class, how come I can get a string too?
提问by Luther
When I use System.out.println(obj.getClass())
it doesn't give me any error. From what I understand getClass()
returns a Class type.
Since println()
will print only strings, how come instead of a class, println
is getting a String?
当我使用System.out.println(obj.getClass())
它时,它不会给我任何错误。据我所知,getClass()
返回一个 Class 类型。既然println()
只会打印字符串,那么为什么会println
得到一个字符串而不是一个类呢?
采纳答案by nullptr
All objects in Java inherit from the class Object. If you look at that document, you'll see that Object
specifies a toString
method which converts the object into a String. Since all non-primitive types (including Class
es) are Object
s, anything can be converted into a string using its toString
method.
Java 中的所有对象都继承自Object类。如果您查看该文档,您会看到它Object
指定了一个toString
将对象转换为字符串的方法。由于所有非原始类型(包括Class
es)都是Object
s,因此可以使用其toString
方法将任何内容转换为字符串。
Classes can overridethis method to provide their own way of being turned into a string. For example, the String
class overrides Object.toString
to return itself. Class
overrides it to return the name of the class. This lets you specify how you want your object to be output.
类可以覆盖此方法以提供自己的转换为字符串的方式。例如,String
类重写Object.toString
以返回自身。Class
覆盖它以返回类的名称。这使您可以指定希望如何输出对象。
回答by rolfl
System.out.println(someobj)
is always equivalent to:
System.out.println(someobj)
总是等价于:
System.out.println(String.valueOf(someobj));
And, for non-null values of someobj, that prints someobj.toString();
并且,对于 someobj 的非空值,打印 someobj.toString();
In your case, you are doing println(obj.getClass())
so you are really doing:
在您的情况下,您正在这样做println(obj.getClass())
,您确实在这样做:
System.out.println(String.valueOf(obj.getClass()));
which is calling the toString
method on the class.
这是toString
在类上调用方法。
回答by devrobf
As you probably know, every class in Java inherits from the Object
class. This means that every class automatically has the method toString()
, which returns a representation of the object as a String
. When you concatenate a string with an object, or if you pass an Object
into an argument where there should be a String
, Java automatically calls the toString()
method to convert it into a String
. In your case, the toString()
method has been overridden to return the name of the class.
您可能知道,Java 中的每个类都继承自Object
该类。这意味着每个类都自动具有方法toString()
,该方法将对象的表示形式返回为String
。当您将一个字符串与一个对象连接起来时,或者如果您将 an 传递Object
给一个应该有 a 的参数String
,Java 会自动调用该toString()
方法将其转换为 a String
。在您的情况下,该toString()
方法已被覆盖以返回类的名称。
You can see this happen with other objects, too:
您也可以在其他对象上看到这种情况:
Set s = new Set();
s.add(1);
s.add(3);
s.add(5);
System.out.println(s);
will output "[1, 3, 5]".
将输出“[1, 3, 5]”。
回答by Maroun
See the code:
看代码:
787 public void println(Object x) {
788 String s = String.valueOf(x);
789 synchronized (this) {
790 print(s);
791 newLine();
792 }
793 }
Note the String.valueOf(x)
.
请注意String.valueOf(x)
.
Bonusfor asking a good question:
提出好问题的奖励:
632 public void print(String s) {
633 if (s == null) {
634 s = "null";
635 }
636 write(s);
637 }
That's why it prints null
when the object is null :)
这就是为什么它null
在对象为空时打印的原因:)