Java Fundamentals Strengthen the StringBuffer of 44:stringbuffer class and the transformation of string

Source: Internet
Author: User

1. cross-conversion of string and StringBuffer

The idea is: a-----b conversion, we convert A to B, in fact, to use the function of B, b-----A conversion, we may be the result of a type, so also to convert back

2. Case Demo:

1  Packagecn.itcast_07;2 3 /*4 * Why we are going to explain the conversions between classes:5 * A--B conversion6 * We convert A to B, actually to use the function of B. 7 * B--A conversion8 * We may want the result is a type, so we have to turn back. 9  * Ten * String and StringBuffer are converted to each other? One  */ A  Public classStringbuffertest { -      Public Static voidMain (string[] args) { -         //String --StringBuffer theString s = "Hello"; -         //Note: You cannot assign the value of a string directly to StringBuffer -         //stringbuffer sb = "Hello"; -         //StringBuffer sb = s; +         //Mode 1: Through the construction method -StringBuffer SB =NewStringBuffer (s); +         //Mode 2: Pass the Append () method AStringBuffer SB2 =NewStringBuffer (); at Sb2.append (s); -System.out.println ("SB:" +SB); -System.out.println ("SB2:" +SB2); -System.out.println ("---------------"); -  -         //StringBuffer --String inStringBuffer buffer =NewStringBuffer ("Java"); -         //String (stringbuffer buffer) to         //Mode 1: Through the construction method +String str =NewString (buffer); -         //Mode 2: Through the ToString () method theString str2 =buffer.tostring (); *System.out.println ("str:" +str); $System.out.println ("str2:" +str2);Panax Notoginseng     } -}

The results of the operation are as follows:

Java Fundamentals Strengthen the StringBuffer of 44:stringbuffer class and the transformation of string

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.