package objtransfering; public class ObjTransfering { public static void printEvrth(Object... args) { for (Object it : args) System.out.println(it); } public static void main(String[] args) { printEvrth(new Integer(1), new Double(5.00), new String("end.")); } } 

All classes inherit from Object . For example, the Integer wrapper inherits the Number class, and it, in turn, is Object . It turns out that the Integer class has fields and methods that the Object class does not have. Then why taking Objects the System.out.println() method distinguishes Integer , Double and String in them? Doesn’t these objects have a part that distinguishes them from Object ?

    3 answers 3

    In the source code, you can see that the println() method is overloaded and takes one of a variety of argument types.

    At runtime, the JVM determines which type of object is passed by reference of type Object and calls the appropriate method. In this case, OOP wrappers over primitives of type Integer or Double , and other types that inherit Object can have an overridden toString() method, which will be output to the console. Usually this method prints the class name and hashCode object. In the case of the classes you cited, there is a different code. For example, Integer displays only a number.

    In this case, of course, no information is lost about the specific type of object assigned to the link. The only restriction is that you cannot call methods of a particular class until you refer it to its actual type.

    • 2
      Those. in its case, this println overload is called, which calls the valueOf method of the String class, which in turn calls the Object class's toString method, then it calls the toString method overridden by the successor ( Integer , Double and String ) via the JVM virtual function table. - StateItPrimitive
    • one
      Thank you for your reply. In fact, only you wrote exactly what I expected to read: that the information is not lost, but I cannot access the methods / fields of the heir. Also very interesting is the addition of the companion @StateltPrimitive, it turns out that the JVM still calls its own .toString() method for each class. Is this valid for all classes or only for Object ? For example, I will create my own class, inherit it in another class and supplement it with new methods. And after converting the object of the second class to the first one, will it be possible to refer (through a link to the object of the first class) to the unique methods of the second? - heroys6
    • 2
      @ heroys6 If you pass the instance of the second class ( Child ) to a method whose parameter type is first class ( Parent ), then you explicitly cast the type of the instance with the Parent type that came to the method to Child (if the dynamic type of the parameter is true Child , then type conversion will be successful, otherwise the corresponding exception will be thrown) you will be able to refer directly to the methods of the heir. In general, even without type conversion, calling the methods of the parent will call the methods of the heir (if any overrides are defined in it) at the expense of the table of virtual functions. - StateItPrimitive
    • one
      @ heroys6 It's all about the virtual functions table (read the materials about it). An instance of a class in Java contains a reference to the virtual method table of its class (and in Java, by default, all methods are virtual). The class method table after loading and dynamically linking the class ultimately contains references to the methods of this class. - StateItPrimitive
    • one
      If the method is defined in this class, then it will be a reference to the method of this class, if defined in the heir, then a reference to the heir’s method, if in a superclass, it will be a reference to the superclass method. - StateItPrimitive

    The Object class has a toString() method. When using System.out.println() , this method is called. In turn, toString() overridden in the classes Integer , Double and String , and each of them has its own implementation of this method.

      If you look inside these objects, you will find a magic method like toString .

      This method serves to represent an object as a string.

      Each class has its own implementation of the method. For example, Integer following code:

       public static String toString(int i, int radix) { if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) radix = 10; /* Use the faster version */ if (radix == 10) { return toString(i); } char buf[] = new char[33]; boolean negative = (i < 0); int charPos = 32; if (!negative) { i = -i; } while (i <= -radix) { buf[charPos--] = digits[-(i % radix)]; i = i / radix; } buf[charPos] = digits[-i]; if (negative) { buf[--charPos] = '-'; } return new String(buf, charPos, (33 - charPos)); } 

      As you can see, the method returns a string (roughly) return new String(buf, charPos, (33 - charPos));

      Other classes have the same thing. In any class, you can also implement it.

      You can write anything there, even return "hello, world";

      And this very System.out.println implicitly calls this same toString method on an object.


      Perhaps add an example:

       import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Ideone { public static void main (String[] args) throws java.lang.Exception { Test test = new Test(); System.out.print(test); } public static class Test { public String toString() { return "Hello, world!"; } } } 

      https://ideone.com/jLvbDM

      • I was 2 seconds ahead))) - Yuriyyyy
      • @ YuriySPb I just sneezed))) - Alexey Shimansky
      • Bless you ) - YuriySPb
      • Eh long I wrote (( - Bleser