A brief analysis of the role of the toString method in JAVA

  • 2020-04-01 02:10:21
  • OfStack

Because it is an existing method in Object, and all classes inherit from Object, "all objects have this method."

It is usually just for the sake of output, such as system.out.println (xx), where the "xx" in the parentheses automatically calls xx's toString() method if it is not of type String

All in all, it's just a method that sun deliberately added to Java to facilitate string manipulation of all classes
 
Additional answers:
The purpose of writing this method is to facilitate operation, so it can be used or not used in file operation
Example 1:


public class Orc
{
       public static class A
       {
              public String toString()
              {
                     return "this is A";
              }
       }
       public static void main(String[] args)
       {
              A obj = new A();
              System.out.println(obj);
       }
}

If a method contains the following sentence:
A obj = new A ();
System. The out. Println (obj);
The output is :this is A

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201307/2013071811491621.jpg ">

Example 2:

public class Orc
{
       public static class A
       {
              public String getString()
              {
                     return "this is A";
              }
       }
       public static void main(String[] args)
       {
              A obj = new A();
              System.out.println(obj);
              System.out.println(obj.getString());
       }
}

You get the output: xxx@xxxxxxx in the form of the class name and address
System. The out. Println (obj. Get string ());
The output is :this is A

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201307/2013071811491622.jpg ">

See the difference? The nice thing about toString is that it's automatically called when it encounters an output method like println, and it doesn't have to be typed out explicitly.

public class Zhang
{
       public static void main(String[] args)
       {
              StringBuffer MyStrBuff1 = new StringBuffer();
              MyStrBuff1.append("Hello, Guys!");
              System.out.println(MyStrBuff1.toString());
              MyStrBuff1.insert(6, 30);
              System.out.println(MyStrBuff1.toString());
       }
}

It's worth noting that, If you want to display a StringBuffer on the screen, you must first call the toString method to make it a string constant, because PrintStream's method println() does not take arguments of type StringBuffer.

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201307/2013071811491623.jpg ">


public class Zhang
{
    public static void main(String[] args)
    {
        String MyStr = new StringBuffer();
        MyStr = new StringBuffer().append(MyStr).append(" Guys!").toString();
        System.out.println(MyStr);        
    }
}

The toString() method here converts the StringBuffer type to the String type.

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201307/2013071811491624.jpg ">


public class Zhang
{
    public static void main(String[] args)
    {
        String MyStr = new StringBuffer().append("hello").toString();
        MyStr = new StringBuffer().append(MyStr).append(" Guys!").toString();
        System.out.println(MyStr);        
    }
}
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201307/2013071811491625.jpg ">

About String,StringBuffer performance

By using some helper tools to find bottlenecks in your program, you can then optimize the code for those bottlenecks. There are generally two scenarios: optimizing the code or changing the design approach. We tend to choose the latter because not calling the following code will improve the performance of the program more than calling some optimized code. A well-designed program can improve performance by streamlining the code.

Here are some of the methods and techniques that are commonly used in the design and coding of JAVA programs to improve their performance.

1. Object generation and resizing.

A common problem in JAVA programming is that the functions provided by the JAVA language itself are not taken advantage of, resulting in the generation of many objects (or instances). Since the system not only takes time to generate objects, it may also take time to garbage collect and process them later. Therefore, generating too many objects can have a significant impact on the performance of your program.

Example 1: for String,StringBuffer, +, and append

The JAVA language provides operations on variables of type String. However, if not used properly, the performance of the program will be affected. The following statement:


HuangWeiFeng String name = new String (" ");

System. The out. Println (name + "is my name");

It seems to have been pretty streamlined, but it's not. To generate binary code, follow these steps and actions:

(1) generate new String (STR_1);

(2) copy the string;

(3) loading string constant "HuangWeiFeng" (STR_2);

(4) call the string Constructor;

(5) save the string into the array (starting from position 0);

(6) get the static out variable from the java.io.PrintStream class;

(7) generate new StringBuffer(STR_BUF_1);

(8) copy the string buffer variable;

(9) invoke the string buffer Constructor;

(10) save the string buffer into the array (starting from position 1);

(11) call the append method in the StringBuffer class with STR_1 as the parameter;

(12) load string constant "is my name"(STR_3);

(13) call the append method in the StringBuffer class with STR_3 as an argument;

(14) execute the toString command for STR_BUF_1;

(15) call the println method in the out variable and print the result.

As you can see, these two simple lines of code generate five object variables, STR_1,STR_2,STR_3,STR_4, and STR_BUF_1. Instances of these generated classes are generally stored in the heap. The heap initializes all superclasses, instances of classes, and calls the class and the architect for each superclass. And these operations are very resource intensive. Therefore, it is absolutely necessary to restrict the generation of objects.

After modification, the above code can be replaced with the following code.

HuangWeiFeng StringBuffer name = new StringBuffer (" ");

System. The out. Println (name, append (" is my name. "). The toString ());


The system will perform the following operations:

(1) generate new StringBuffer(STR_BUF_1);

(2) copy the string buffer variable;

(3) loading string constant "HuangWeiFeng"(STR_1);

(4) call the string buffer Constructor;

(5) save the string buffer into the array (starting from position 1);

(6) get the static out variable from the java.io.PrintStream class;

(7) loading STR_BUF_1;

(8) load string constant "is my name"(STR_2);

(9) call the append method in the StringBuffer instance with STR_2 as the parameter;

(10) execute the toString command (STR_3) for STR_BUF_1;

(11) call the println method in the out variable and print the result.

As you can see, the improved code only generates four object variables: STR_1,STR_2,STR_3, and STR_BUF_1. But the following code segment 2 will execute twice as fast as segment 1. Because segment 1 generates eight objects and segment 2 generates only four.

Code segment 1:

String name = new StringBuffer (" HuangWeiFeng ");

The name + = "is my";

The name + = "name";

Code segment 2:

HuangWeiFeng StringBuffer name = new StringBuffer (" ");

The name, append (" is my ");

Name. Append (" name "). The toString ();

Therefore, making full use of the library functions provided by JAVA to optimize the program is very important to improve the performance of JAVA programs.


Related articles: