Functions of the toString method in JAVA

Source: Internet
Author: User

The following is a detailed analysis of the function of the toString method in JAVA. For more information, see

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

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

All in all, it is just a method specifically added by sun when developing java to facilitate string operations of all classes.
 
Answer:
The purpose of writing this method is to facilitate operations, so it is not available in file operations.
Example 1:

Copy codeThe Code is as follows:
Public class Orc
{
Public static class
{
Public String toString ()
{
Return "this is ";
}
}
Public static void main (String [] args)
{
A obj = new ();
System. out. println (obj );
}
}


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



Example 2:

Copy codeThe Code is as follows:
Public class Orc
{
Public static class
{
Public String getString ()
{
Return "this is ";
}
}
Public static void main (String [] args)
{
A obj = new ();
System. out. println (obj );
System. out. println (obj. getString ());
}
}


The Class Name and address format of xxxx @ xxxxxxx will be output.
System. out. println (obj. getString ());
The output is: this is.



Do you see the difference? The advantage of toString is that it will be automatically called when an output method like "println" is encountered, and it does not need to be explicitly typed out.

Copy codeThe Code is as follows:
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 is worth noting that,If you want to display StringBuffer on the screen, you must first call the toString method to change it to a String constant, because the PrintStream method println () does not accept StringBuffer parameters.

Copy codeThe Code is as follows:
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 is used to convert the StringBuffer type to the String type.

Copy codeThe Code is as follows:
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 );
}
}

Performance of String and StringBuffer

You can use some auxiliary tools to locate the bottleneck in the program and then optimize the code of the bottleneck. There are generally two solutions: optimize the code or change the design method. We usually select the latter, because not calling the following code is better than calling some optimized code to improve the performance of the program. A well-designed program can streamline code and improve performance.

The following provides some methods and techniques that are often used in the design and coding of JAVA programs to improve the performance of JAVA programs.

1. Object generation and size adjustment.

A common problem in JAVA programming is that the functions provided by the JAVA language are not used properly, so a large number of objects (or instances) are often generated ). Because the system not only takes time to generate objects, it may take time to recycle and process these objects. Therefore, generating too many objects will greatly affect the program performance.

Example 1: String, StringBuffer, +, and append

The JAVA language provides operations for String-type variables. However, improper use may affect the program performance. The following statement:


String name = new String ("HuangWeiFeng ");

System. out. println (name + "is my name ");

It seems to have been quite streamlined, but not actually. To generate binary code, perform the following steps and operations:

(1) generate a new String (STR_1 );

(2) copy the string;

(3) load the String constant "HuangWeiFeng" (STR_2 );

(4) Constructor );

(5) Save the string to the array (starting from position 0 );

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

(7) generate a new string buffer variable new StringBuffer (STR_BUF_1 );

(8) copy the buffer variable of the string;

(9) Call the Constructor );

(10) Save the string to the array (starting from position 1 );

(11) using STR_1 as the parameter, call the append method in the string buffer class;

(12) load the String constant "is my name" (STR_3 );

(13) using STR_3 as the parameter, call the append method in the string buffer class;

(14) execute the toString command for STR_BUF_1;

(15) Call the println method in the out variable to output the result.

The two simple lines of code generate five object variables STR_1, STR_2, STR_3, STR_4, and STR_BUF_1. The instances of these generated classes are generally stored in the heap. The heap needs to initialize the super classes and class instances of all classes, and also call the framework of every super class of the class. These operations consume a lot of system resources. Therefore, it is necessary to limit the generation of objects.

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

StringBuffer name = new StringBuffer ("HuangWeiFeng ");

System. out. println (name. append ("is my name."). toString ());


The system will perform the following operations:

(1) generate a new string buffer variable new StringBuffer (STR_BUF_1 );

(2) copy the buffer variable of this string;

(3) load the String constant "HuangWeiFeng" (STR_1 );

(4) Call the Constructor );

(5) Save the string to the array (starting from position 1 );

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

(7) Load STR_BUF_1;

(8) load the String constant "is my name" (STR_2 );

(9) using STR_2 as the parameter, call the append method in the string buffer instance;

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

(11) Call the println method in the out variable to output the result.

It can be seen that the improved code only generates four object variables: STR_1, STR_2, STR_3 and STR_BUF_1. you may think that generating less than one object will not greatly improve the program performance. However, the execution speed of code segment 2 below will be twice that of code segment 1. Because code segment 1 generates eight objects, while code segment 2 generates only four objects.

Code Segment 1:

String name = new StringBuffer ("HuangWeiFeng ");

Name + = "is my ";

Name + = "name ";

Code Segment 2:

StringBuffer name = new StringBuffer ("HuangWeiFeng ");

Name. append ("is my ");

Name. append ("name."). toString ();

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.