简体   繁体   中英

Java- difference between String object and user defined objects

This is a simple java program. It contains a class "Student" and we are making its two objects stud,stud1. Similarly I have also made a String object "a" whose value is "Hello".

class Student{

  int age;

  public void setAge(int age){
    this.age=age;
  }

  public int getAge(){
    return age;
  }
}


class Hello{

  public static void main(String args[]){

    Student stud= new Student();
    Student stud1= new Student();
    stud.setAge(15);
    int i=stud.getAge();
    String a=new String("Hello");
    System.out.println(stud);
    System.out.println(stud1);
    System.out.println(a);
  }

}

As we know when we create a class object, it just holds a reference values for that object.That's why when I tried to print stud and stud1 I am getting two reference values.But since "a" is an object of class String we should expect a reference value instead of value "Hello".Why its happening?

This line

System.out.println(stud);

is equivalent 1 to

System.out.println(stud.toString());

Since String overrides the Object.toString method you get something more meaningful than a bunch of characters and digits when printing strings.

You could let your user-defined classes do this too. In your Student class it would look like this:

class Student{

    int age;

    public void setAge(int age){
        this.age=age;
    }

    public int getAge(){
        return age;
    }

    @Override
    public String toString() {              // Called for instance when
        return "Student with age " + age;   // the student should be printed
    }
}

Here's an ideone.com demo for a run with your code where Student overrides toString .

Further reading:

1) Unless stud equals null

When you call System.out.println(x) , the String output is the .toString() of the object passed to it.

Of course the .toString() of a String is the string itself, so you'd expect "Hello" .

If your class doesn't define a .toString() method (and it doesn't), the .toString() defined for its parent (is the Object class) is used, which prints a value based on type/class and the hashCode() of the object.

When you hand System.out.println() an object, it will call that object's toString() method in order to be able to print something out. Object has a default toString() method, and that's what's getting called for your Student objects, because you did not override the toString() method. Strings, however, have toString() defined in the obvious way, so it's printing out that custom string representation of the object; namely, the value of the string.

When you call System.out.println(Object) , the toString() method of that object is called. Since you dont have one implemented for Student , the Object.toString() is called which prints the reference value.

To print meaningful values, override it like so --

@Override
public String toString() {
    return "Age = " + age;
}

调用a的toString()方法并打印字符串“Hello”。

Your student class just don't override toString() method, which is inherited by every java object:

http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Object.html#toString ()

Simply, there is println(Object obj) method inside the PrintStream class (System.out is an instance of PrintStream), and inside its implementation there is obj.toString() . You can override toString() of any object to format the string yields from calling System.out.println(Object obj) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM