Marjer
Marjer

Reputation: 1403

What happens when printing an object in java

class Data {
    int a = 5;
}

class Main {
    public static void main(String[] args) {
        int b=5;
        Data dObj = new Data();
        System.out.println(dObj);
        System.out.println(b);
    }
}

I want to know what's happening when printing a object or number or string.

I ran the above code, I'm getting the result as "data@1ae73783" for System.out.println(dObj); and "5" for System.out.println(b);

Then I did debug to check whats really happening when printing a object, there was lot of parameter called in a debug mode(like classloader,theards)
I know for the first print the value represent class name followed by address. But don't know what's really happening in debug mode, for the 2nd print only variable assignment happened in the debug mode i.e b=5.

Please explain whats really happening?

Upvotes: 7

Views: 35737

Answers (7)

Vishal
Vishal

Reputation: 1414

In Addition to @JB Nizet answer,

To Provide our own string representation, we have to override toString() in our class which is highly recommended because

There are some classes in which toString() is overridden already to get the proper string representation.

Examle: String, StringBuffer, StringBuilder and all wrapper classes

    Integer ob1 = new Integer("10");
    String ob2 = new String("Doltan Roy");
    StringBuffer ob3 = new StringBuffer("The Rock");
    StringBuilder ob4 = new StringBuilder("The Joshua");

    System.out.println(ob1);
    System.out.println(ob2);
    System.out.println(ob3);
    System.out.println(ob4);

Output:

10

Doltan Roy

The Rock

The Joshua

Hope this would help!

Upvotes: 0

Raj Kumar Paswan
Raj Kumar Paswan

Reputation: 79

when we print object of any class System.out.print() gives string of class name along with memory address of object (ClassName@MemoryAdress)

Upvotes: 3

JB Nizet
JB Nizet

Reputation: 691755

You don't need a debugger to know what's happening. System.out is of type PrintStream. The javadoc of PrintStream.println(Object) says:

Prints an Object and then terminate the line. This method calls at first String.valueOf(x) to get the printed object's string value, then behaves as though it invokes print(String) and then println().

The javadoc of String.valueOf(Object) says:

if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.

And the javadoc of Object.toString() says:

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

 getClass().getName() + '@' + Integer.toHexString(hashCode())
 

Upvotes: 19

John Smith
John Smith

Reputation: 1

data dObj = new data() does not exist in the source code; you want to print the string value of the object (Data), you have to override the toString method;

try this

public class Program {
  public static void main(String[] args) {
    Data data = new Data();
    System.out.println(data);
  }
}

class Data {
  int a = 5;

  @Override
  public String toString() {
    return String.valueOf(a);
  }
}

Upvotes: 0

nhgrif
nhgrif

Reputation: 62062

Please explain whats really happening?

As other have told you, using System.out.println with an object will call to toString method on that object. If the class doesn't have it's own toString method, then it's a call to the super class's toString. If the super class call goes all the way back to java.lang.Object, the default toString method prints the name of the object's type (what class it is), followed by an @ sign, and the memory location of the object--the hexidecimal address of where that object is stored in memory.

ClassName@MemoryLocation

Upvotes: 4

MGorgon
MGorgon

Reputation: 2607

First, int isn't an Object. It's primitive type. Second, when Class haven't overrived toString() method, toString() from Object class is invoked.

Upvotes: 0

Thorn
Thorn

Reputation: 4057

All objects inherit from java.lang.Object which has a default implementation of toString. If an object overrides this method then out.print (obj) will put something useful on the screen.

Primitive data types are handled by a different, much simpler implementation of println. The println method is overridden for every data type in addition to Object.

Upvotes: 0

Related Questions