Thinking in Java note 13 string operations

Source: Internet
Author: User

* ** Unchangeable string

Every method in the string class that seems to modify the string value is actually creating a new String object. * ** For simple string operations in stringbuilder, you can use Java operators +, complex String concatenation operations (such as loops). stringbuilder should be used first, because if the default operators are used, in the loop body, the Java compiler will create a new stringbuilder to operate the string at each loop, which is less efficient. * ** Format the output.
Public class simpleformat {
Public static void main (string [] ARGs ){
Int x = 5;
Double Y = 5.332542;
// The old way:
System. Out. println ("Row 1: [" + x + "" + Y + "]");
// The new way:
System. Out. Format ("Row 1: [% d % F] \ n", x, y );
// Or
System. Out. printf ("Row 1: [% d % F] \ n", x, y );
}
}/* Output:
Row 1: [5, 5.332542]
Row 1: [5, 5.332542]
Row 1: [5, 5.332542]

Java The. util. formatter class implements the new Formatting Function string. Format () to format strings.
* ** Regular Expression

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.