Home  >  Article  >  Java  >  What is the function of tostring method in java

What is the function of tostring method in java

coldplay.xixi
coldplay.xixiOriginal
2020-08-01 16:17:3313726browse

The function of the tostring method in Java is to return a [text representation] string of this object. The result is a concise but easy-to-read information expression.

What is the function of tostring method in java

The role of the tostring method in java is

The toString method will return a "text representation" of this object String. The result is a concise yet easy-to-read expression of information. It is recommended that all subclasses override this method.

The toString method of the Object class returns a string, which consists of the class name (the object is an instance of this class), the at tag "@" and the unsigned hexadecimal hash code of this object The system represents the composition. In other words, this method returns a string whose value is equal to:

getClass().getName()+'@'+Integer.toHexString(hashCode());

Returns: the string representation of the object

Because it is a method that already exists in Object, and All classes inherit Object, so "all objects have this method";

It is usually just for convenience of output, such as System.out.println(xx), the " in brackets xx" If it is not a String type, the toString() method of xx will be automatically called;

In short, it is just a special purpose designed to facilitate string operations of all classes when Sun developed Java. One way to join.

What is the function of tostring method in java

Extended information

tostring的使用示例
publicclassOrc
{
publicstaticclassA
{
publicStringtoString()
{
return"thisisA";
}
}
publicstaticvoidmain(String[]args)
{
Aobj=newA();
System.out.println(obj);
}
}

Related learning recommendations: Java video tutorial

##

The above is the detailed content of What is the function of tostring method in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn