3

Out of blue, I came across this:

public class demo {
    void multiply(){
       System.out.println("HELLO WORLD!")
       }
}

public static void main(String args[]){
        demo e=new demo();
        demo e1=new demo();
        System.out.println(e);
        System.out.println(e1);
    }
}

The weird output that I got when I executed the code was:

demo@6e1408

demo@e53108

Or

demo@1888759

demo@6e1408

Can someone please explain to me what's happening? The value I got, is this a default value for an object, or am I missing something?

Community
  • 1
  • 1
  • http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/java/lang/Object.java#Object.toString%28%29 – sp00m Dec 13 '13 at 16:49
  • Take a look at [this question](http://stackoverflow.com/questions/1961146/memory-address-of-variables-in-java) to understand what's going on, and not just a solution – Paul Samsotha Dec 13 '13 at 16:52

6 Answers6

3

It is the toString() returned value you are seeing

default Object's implementation

public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

there is no default value for an Object but its member variable has default value based on its type

jmj
  • 237,923
  • 42
  • 401
  • 438
2

You have to override the toString() method in your classes which you are printing.

Right now it's printing the default implementation of Object class toString() method.

From Object class Source code

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

Returns:
a string representation of the object.


    public String  toString() {
         return getClass().getName() + "@" + Integer.toHexString(hashCode());
     }

So in your Demo class Ovveride toString() method to get desired O/P.

    public class Demo{

    ----

       @Override
       public String toString() {
             //return something 
       }    
}
jelford
  • 2,625
  • 1
  • 19
  • 33
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

This is a result of the object's default toString() method which prints out the class name and the hash code. You can actually change this by overriding the toString() method in the class of the object that you want to print out. However, instead of printing the object itself, you'd have to call toString() on the object. For example:

public class demo{

    void multiply(){
        System.out.println("HELLO WORLD!")
    }

    public String toString() {
        return "This is the class, demo.";
    }


    public static void main(String args[]){
        demo e=new demo();
        demo e1=new demo();
        System.out.println(e.toString());
        System.out.println(e1.toString());
    }
}

P.S: The "default value" of an object is null when the object is not initialized. However, the default values within the object are defined by whatever is in the constructor. I suggest you look into objects and classes more.

1

The term 'default value for an object' isn't really correct. Objects don't default, references do and they default to null as described in the the documentation on Data Types.

What you are seeing output by your program is the String returned by the default implementation of toString(). When you call PrintStream.println(Object), which is what System.out.println(demo) is doing, the PrimeStream needs to convert the object passed, in this case your Demo object to a String, which it does by simply calling its toString() method.

If you haven't overridden toString() in you class then you get the default behavior.

Nick Holt
  • 33,455
  • 4
  • 52
  • 58
0

The value you see is the Objects address in memory, the fact the values are different shows it is not the same object. If you want to change what comes out, override the .toString() method.

M21B8
  • 1,867
  • 10
  • 20
0

actually by default a object's toString method returns someObjectClassname@hashcodenumber. to print your desired output you have to override the toString method

stinepike
  • 54,068
  • 14
  • 92
  • 112