14

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?

Pimgd
  • 5,983
  • 1
  • 30
  • 45
Carlo Luther
  • 2,402
  • 7
  • 46
  • 75

4 Answers4

38

System.out.println(someobj) is always equivalent to:

System.out.println(String.valueOf(someobj));

And, for non-null values of someobj, that prints someobj.toString();

In your case, you are doing println(obj.getClass()) so you are really doing:

System.out.println(String.valueOf(obj.getClass()));

which is calling the toString method on the class.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
rolfl
  • 17,539
  • 7
  • 42
  • 76
  • 2
    +1, most accurate. From the API doc for PrintStream.println( Object ): "This method calls at first String.valueOf(x) to get the printed object's string value ..." – Andy Thomas May 05 '13 at 14:27
  • 1
    Sorry for the sub-question, but is there any reason why `String.valueOf()` is used rather than `object.toString()`? I noticed other parts of the Java API also use `String.valueOf`, whereas I would usually use the latter. – devrobf May 05 '13 at 14:49
  • 6
    @jazzbassrob: The standard definition of `String.valueOf(Object obj)` is `return (obj == null) ? "null" : obj.toString();`, so it is basically the same thing, plus `null` handling. – Keppil May 05 '13 at 14:53
20

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 Classes) are Objects, anything can be converted into a string using its toString method.

Classes can override this 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.

nullptr
  • 2,244
  • 1
  • 15
  • 22
4

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).

Bonus for 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 :)

Maroun
  • 94,125
  • 30
  • 188
  • 241
3

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.

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]".

devrobf
  • 6,973
  • 2
  • 32
  • 46