Java se basics -- toString() method in Object class

catalogue

1. What is the toString() method

  • The toString() method is defined in the Object class. Its return value is String type and returns the class name and its
    Reference address of.
  • The toString() method is automatically called when connecting a String with other types of data
Date now=new Date();
System.out.println("now="+now); amount to
System.out.println("now="+now.toString()); 
  • You can override the toString() method in user-defined types as needed
    For example, the String class overrides the toString() method to return the value of the String.
s1="hello";
System.out.println(s1);
//Equivalent to System.out.println(s1.toString()); 
  • When the basic type data is converted to String type, the toString() method of the corresponding wrapper class is called
int a=10; System.out.println("a="+a);
  • It is usually just for the convenience of output, such as System.out.println(xx). If the "xx" in the bracket is not of String type, it will automatically call the toString() method of xx

2. Use of tostring() method

  1. When we output a reference to an object, we actually call the toString() of the current object
  2. Definition of toString() in Object class:
  public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
     }
//The toString() method of the Object class returns a string consisting of the class name (the Object is an instance of the class), the at flag "@" and the unsigned hexadecimal representation of the hash code of the Object. In other words, the method returns a string whose value is equal to:
//getClass().getName() + '@' + Integer.toHexString(hashCode())
  1. String, Date, File, wrapper class, etc. all override the toString() method in the Object class. This enables the "entity content" information to be returned when the toString() of the Object is called.
    The following code is shown:
public class ToStringTest {
	public static void main(String[] args) {
	
		String str = new String("MM");
		System.out.println(str);//MM
		
		Date date = new Date(4534534534543L);
		System.out.println(date.toString());//Mon Sep 11 08:55:34 GMT+08:00 2113
		
	}
}
  1. A custom class can also override the toString() method, which returns the "entity content" of the object when called.

3. Why rewrite the toString() method

First look at the following code:

public class toStringTest {
	public static void main(String[] args){
		Object A = new Object(); 
		System.out.println(A);
	    System.out.println(A.toString());  
	}  
} 


Output results:
This is what is returned when tostring is used directly.

java.lang.Object@15db9742
java.lang.Object@15db9742
  • If the toString method is not overridden in the class, the output format of the toString method of Object is used by default. At this time, the Object will directly display the hash code.

  • If the toString method is overridden in the class, the output format will output the object in the way we define. Therefore, it can be understood as follows:
    Rewriting toString is a format for objects when they are printed out. This is in line with business logic and humanized display results.

4. Example code of tostring() method

Example code 1:

class ToStringExe{
    @Override
    public String toString() {
        return "toString The method has been rewritten";
    }
}


public class toStringTest{
    public static void main(String[] args) {
    	ToStringExe A = new ToStringExe();
        System.out.println(A);
        System.out.println(A.toString());
    }
}

Output results:

toString The method has been rewritten
toString The method has been rewritten

5. Rewrite the shortcut key of toString () method in eclipse



After checking the variable to be output, the following code will be automatically added to the code

	@Override
	public String toString() {
		return "ToStringExe [name=" + name + ", age=" + age + "]";
	}

Example code 2:

class ToStringExe{
	
	String name;
	int age;
	@Override
	public String toString() {
		return "ToStringExe [name=" + name + ", age=" + age + "]";
	}
	

}



public class toStringTest{
    public static void main(String[] args) {
    	ToStringExe A = new ToStringExe();
        System.out.println(A);
        System.out.println(A.toString());
    }
}

Output results:

ToStringExe [name=null, age=0]
ToStringExe [name=null, age=0]

Tags: Java

Posted on Sat, 30 Oct 2021 08:45:13 -0400 by chaffinator