The role of the ToString method in Java

Source: Internet
Author: User

Because it is a method already in object, and all classes inherit object, "All objects have this method".

It is usually just for the convenience of the output, such as System.out.println (XX), the parentheses inside the "XX" if it is not a string type, automatically call XX's ToString () method

All in all, it's just a way for the Sun company to develop Java in order to facilitate string manipulation of all classes.

Answer add:

The use of this method is to facilitate the operation, so in the file operation can not be used

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 has the following sentence:

A obj=new a ();

System.out.println (obj);

Will get output: this is A

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 ());

}

}

will get output: [email protected] class name plus address form

System.out.println (Obj.getstring ());

Will get output: this is A

See the difference, the advantage of ToString is that when encountering "println" such as the output method will be automatically called, do not explicitly hit out.

1 public class Zhang

3 {

5 public static void Main (string[] args)

7 {

9 StringBuffer MyStrBuff1 = new StringBuffer ();

mystrbuff1.append ("Hello, guys!");

System.out.println (mystrbuff1.tostring ());

Mystrbuff1.insert (6, 30);

System.out.println (mystrbuff1.tostring ());

+ }

21}

It is important to note that if you want to display stringbuffer on the screen, you must first call the ToString method to turn it into a string constant , because the PrintStream method println () Parameters of type StringBuffer are not accepted .

1 public class Zhang
2 {
3 public static void Main (string[] args)
4 {
5 String mystr = new StringBuffer ();
6 mystr = new StringBuffer (). Append (MyStr). Append ("guys!"). ToString ();
7 System.out.println (MYSTR);
8 }
3 ·

The ToString () method in this role is to convert the StringBuffer type to a string type.

1 public class Zhang
2 {
3 public static void Main (string[] args)
4 {
5 String mystr = new StringBuffer (). Append ("Hello"). toString ();
6 mystr = new StringBuffer (). Append (MyStr). Append ("guys!"). ToString ();
7 System.out.println (MYSTR);
8 }
3 ·

About string, the performance of StringBuffer

Blog Category: Java language

By using some assistive tools to find bottlenecks in the program, you can then optimize the code in the Bottleneck section. There are generally two scenarios: optimizing code or changing design methods. We generally choose the latter because it is better to improve the performance of the program than to call some optimized code without calling the following code. A well-designed program can streamline the code and improve performance.

Here are some of the methods and techniques that are often used in the design and coding of Java programs in order to improve the performance of Java programs.

1. The creation and resizing of objects.

A common problem in Java programming is the lack of proper use of the functions provided by the Java language itself, which often results in a large number of objects (or instances). Because the system not only takes time to generate objects, it may take time for them to be garbage collected and processed later. Therefore, generating too many objects will have a significant impact on the performance of the program.

Example 1: About String, stringbuffer,+ and append

The Java language provides operations on variables of type string. But if used improperly, it will affect the performance of the program. As in the following statement:

String Name=new string ("Huangweifeng");

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

It seems to have been very streamlined, but that is not the case. In order to generate the binary code, the following steps and actions are performed:

(1) A new string (str_1) is generated;

(2) Copy the string;

(3) Load string constant "Huangweifeng" (str_2);

(4) The frame that invokes the string (Constructor);

(5) Save the string in the array (starting at position 0);

(6) The static out variables are obtained from the Java.io.PrintStream class;

(7) Generate a new string buffer variable new StringBuffer (str_buf_1);

(8) Copy the string buffer variable;

(9) Invoking a string-buffered frame (Constructor);

(10) Save the string buffer into the array (starting at position 1);

(11) Call the Append method in the string buffer (StringBuffer) class with str_1 as the parameter;

(12) Load string constant "is my Name" (str_3);

(13) Call the Append method in the string buffer (StringBuffer) class with str_3 as the parameter;

(14) Execute ToString command for str_buf_1;

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

As you can see, these two lines of simple code generate str_1,str_2,str_3,str_4 and str_buf_1 five object variables. Instances of these generated classes are generally stored in the heap. The heap is to initialize the superclass of all classes, the instances of the class, and also to invoke the architect of the class extremely each superclass. These operations consume system resources very much. Therefore, it is absolutely necessary to restrict 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 do the following:

(1) Generate a new string buffer variable new StringBuffer (str_buf_1);

(2) Copy the string buffer variable;

(3) Load string constant "Huangweifeng" (str_1);

(4) Invoking a string-buffered frame (Constructor);

(5) Save the string buffer into the array (starting at position 1);

(6) The static out variables are obtained 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 string buffer (StringBuffer) instance with str_2 as the parameter;

(10) For str_buf_1 Execution ToString Command (str_3);

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

As you can see, the improved code generates only four object variables: Str_1,str_2,str_3 and str_buf_1. You may think that building an object less will not improve the performance of the program greatly. However, the following code snippet 2 will execute twice times as fast as the code Snippet 1. Because code snippet 1 generates eight objects, Code snippet 2 generates only four objects.

Code Snippet 1:

String name= New StringBuffer ("Huangweifeng");

name+= "is my";

name+= "Name";

Code Snippet 2:

StringBuffer name=new stringbuffer ("Huangweifeng");

Name.append ("Is my");

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

Therefore, it is very important to improve the performance of Java programs by fully utilizing the library functions provided by Java to optimize the program.

Related Article

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.