Eight
Eight

Reputation: 4284

String reference in java

class test
{
    public static void main(String args[])
    {
        test ob=new test();
        String st=new String("it is a string");

        System.out.println(ob);
    System.out.println(st);
    }
}

in this code,ob is a reference of test class and st is a reference of string class. while printing ob it shows the reference but st prints the value i.e."it is a string".. why?

Upvotes: 0

Views: 3581

Answers (9)

Jörj Svenssen
Jörj Svenssen

Reputation: 83

You'll see much clear with an example:

class test {

    @Override
    public String toString() {
        return "test={no attributes to print}";
    }

    public static void main(String args[]) {
        test ob = new test();
        String st = new String("it is a string");

        System.out.println(ob);
        System.out.println(st);
    }
}

and result in:

test={no attributes to print}
it is a string

BUILD SUCCESSFUL (total time: 3 seconds)

Upvotes: 1

r0ast3d
r0ast3d

Reputation: 2635

String is a special class in java where you can hold strings of data.

Every class by default extends java.lang.Object and you can override the toSring method to show what the data is in your class.

Since you have not overriden toString, it prints the reference.

On the other hand, String class provides an implementation of the toString(){ returning this ; /// which prints the contents of the string. }

Upvotes: 1

gigadot
gigadot

Reputation: 8969

Every Java class extends Object class automatically even if you don't explicitly declare it and the Object class contains a toStrong() method which is used to get a String representing the instance of the Object class. The toString() method of the Object class is implemented to show the reference of object but that is not the actual reference (it is just a text containing information about reference).

However, this method may be overriden by its subclass and this is what what happens to a String class. The toString() method of the String class is overriden to represent the content that the String object holds.

Now, when you use print or println methods on an object, it calls the toString() method of the object and print those values.

In your case, test class has not overriden the toString() method so it prints out whatever value the Object.toString() gives.

Upvotes: 1

Deer Womb
Deer Womb

Reputation: 83

This happens because Java provides support for the representation of strings as literals, with compiler support for the automatic conversion of string literals into String objects; and the automatic conversion from any reference type (i.e. any subclass of class Object) into a printable string by using Object.toString method.

In your example, Here the Java compiler notes the signature of the method calls to the print statement is System.out.toString(String arg). Hence it knows that the arguments must be converted to a String.

Upvotes: 1

Vlad
Vlad

Reputation: 18633

If you run println on an arbitrary object, it will print its string representation as produced by its toString() method (defined in the Object class), which your class does not override. The default implementation prints the reference (or something to that effect).

Upvotes: 4

Brian Roach
Brian Roach

Reputation: 76918

Because you haven't overridden toString() in your test class so it uses the one from Object (the parent class of all objects in java) which is simply going to output the class name and hashcode.

Josh Bloch outlines this in "Effective Java":

http://java.sun.com/developer/Books/effectivejava/Chapter3.pdf

(See: "Item 9: Always override toString")

Upvotes: 7

Sandeep Pathak
Sandeep Pathak

Reputation: 10767

Calling print on Java object merely calls the toString() method which is default inherited form the object class . Since String class has a overridden behavior of toString() to print the string value and your test class don't have such an implementation ,hence the difference.

Upvotes: 1

ibid
ibid

Reputation: 3902

Your test class does not override toString but String does. The Object implementation of test constructs a string from the class name and the hashCode.

Upvotes: 1

sushil bharwani
sushil bharwani

Reputation: 30217

because string class overrides a method toString of Object class which is supposed to print whats passed to the constructor. But the test class doesnt do that.

Upvotes: 5

Related Questions