Analysis of the function of ToString method in Java _java

Source: Internet
Author: User
Tags constant static class stringbuffer

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

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

All in all, it's just a way for sun to add to the convenience of all class string operations while Java is being developed

Answer added:
The purpose of writing this method is to facilitate operation, so it can be used in file operation.
Example 1:

Copy Code code as follows:

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 sentences:
A obj=new a ();
System.out.println (obj);
Will get output: this is A



Example 2:
Copy Code code as follows:

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: Xxxx@xxxxxxx 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 the "println" and so on when the output method will automatically call, do not explicitly typed out.
Copy Code code 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 noteworthy thatIf you want to display stringbuffer on the screen, you must first call the ToString method to make it a string constant because the PrintStream method println () does not accept parameters of the StringBuffer type.

Copy Code code 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 does this by converting the StringBuffer type to a string type.

Copy Code code 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);
}
}

About String, stringbuffer performance

By using some auxiliary tools to find bottlenecks in your program, you can optimize the code for the Bottleneck section. There are generally two scenarios: optimizing the code or changing the design method. We typically choose the latter because not calling the following code can improve program performance better than calling some optimized code. A well-designed program can streamline code to improve performance.

Some of the methods and techniques used in Java program design and coding to improve the performance of Java programs are presented below.

1. The generation and sizing of objects.

a common problem with Java programming is that it does not take advantage of the functions provided by the Java language itself, which often generates a large number of objects (or instances). Since the system not only takes time to generate objects, it may also take time to garbage collect and process these objects later. Therefore, generating too many objects will have a significant effect on the performance of the program.

Example 1: About String, StringBuffer,+ and append

The JAVA language provides an operation for a string type variable. However, 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");

Seems to have been very streamlined, but it is not so. To generate binary code, the following steps and actions are performed:

(1) generate a new string (str_1);

(2) copying the string ;

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

(4) the frame (constructor) that invokes the string;

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

(6) Obtaining the static out variable from the Java.io.PrintStream class ;

(7) generate a new string buffer variable stringbuffer (str_buf_1);

(8) Copy the string buffer variable ;

(9) the frame (constructor) that invokes the string buffer;

(a) save the string buffer into the array (starting at position 1);

(one) call the Append method in the string buffer (StringBuffer) class with str_1 as the parameter ;

(a) load string constant "is my Name" (str_3);

(a) call the Append method in the string buffer (StringBuffer) class using str_3 as the parameter ;

(a) execute the tostring command for str_buf_1 ;

Call the println method in the out variable to output the result.

As you can see, these two simple lines of code generate the 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 initializes the superclass of all classes, instances of the class, and also invokes the schema of the class extremely each superclass. And these operations are very consuming system resources. Therefore, it is absolutely necessary to limit the generation of objects.

As modified, 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 stringbuffer (str_buf_1);

(2) Copy the string buffer variable ;

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

(4) the frame (constructor) that invokes the string buffer;

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

(6) Obtaining 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) the Append method in the string buffer (StringBuffer) instance is invoked with str_2 as the parameter ;

(a) for the str_buf_1 execute 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 might think that fewer builds an object will not greatly improve the performance of the program. But the following code snippet 2 will execute at twice times 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 I";

name+= "Name";

Code Snippet 2:

StringBuffer name=new stringbuffer ("Huangweifeng");

Name.append ("is I");

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

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

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.