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