简体   繁体   English

Java- String对象和用户定义对象之间的区别

[英]Java- difference between String object and user defined objects

This is a simple java program. 这是一个简单的java程序。 It contains a class "Student" and we are making its two objects stud,stud1. 它包含一个“学生”类,我们正在制作它的两个对象stud,stud1。 Similarly I have also made a String object "a" whose value is "Hello". 类似地,我也创建了一个String对象“a”,其值为“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? 正如我们所知,当我们创建一个类对象时,它只保存该对象的引用值。这就是为什么当我尝试打印stud和stud1时,我得到两个引用值。但是因为“a”是类String的对象我们应该期待参考值而不是值“Hello”。为什么会发生?

This line 这条线

System.out.println(stud);

is equivalent 1 to 等于1

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. 由于String 会覆盖 Object.toString方法,因此在打印字符串时,您会获得比一堆字符和数字更有意义的内容。

You could let your user-defined classes do this too. 您也可以让用户定义的类也这样做。 In your Student class it would look like this: 在您的Student课程中,它看起来像这样:

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 . 这是一个ideone.com演示,用于运行代码,其中Student覆盖toString

Further reading: 进一步阅读:

1) Unless stud equals null 1)除非螺栓等于零

When you call System.out.println(x) , the String output is the .toString() of the object passed to it. 当您调用System.out.println(x) ,String输出是传递给它的对象的.toString()

Of course the .toString() of a String is the string itself, so you'd expect "Hello" . 当然, String.toString()是字符串本身,所以你期望"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. 如果你的类没有定义.toString()方法(并且没有),则使用为其父类定义的.toString() (是Object类),它根据类型/类打印一个值。对象的hashCode()

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. 当您将System.out.println()作为一个对象时,它将调用该对象的toString()方法,以便能够打印出一些东西。 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. Object有一个默认的toString()方法,这就是你的Student对象调用的内容,因为你没有覆盖toString()方法。 Strings, however, have toString() defined in the obvious way, so it's printing out that custom string representation of the object; 但是,字符串必须以明显的方式定义toString(),因此它打印出对象的自定义字符串表示; namely, the value of the string. 即,字符串的值。

When you call System.out.println(Object) , the toString() method of that object is called. 当您调用System.out.println(Object) ,将调用该System.out.println(Object)toString()方法。 Since you dont have one implemented for Student , the Object.toString() is called which prints the reference value. 由于您没有为Student实现一个,因此调用Object.toString()来打印参考值。

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: 你的学生班只是不要覆盖每个java对象继承的toString()方法:

http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Object.html#toString () 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() . 简单地说, PrintStream类中有println(Object obj)方法(System.out是PrintStream的一个实例),在它的实现中有obj.toString() You can override toString() of any object to format the string yields from calling System.out.println(Object obj) . 您可以覆盖任何对象的toString() ,以通过调用System.out.println(Object obj)来格式化字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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