StringBuffer
Characteristics:
- is a string buffer.
- is a container whose length is variable and can be manipulated to add multiple data types.
- Finally, the ToString method becomes the string.
is modified by the final lock and therefore cannot be inherited.
Store:
Method 1:append () adds data to the buffer.
return type: StringBuffer
Method: Append (Specify data) adds the specified data to the end of the existing data.
Method 2:insert (index, data content)
return type: StringBuffer
Method: Insert (index, data content) inserts the data content into the specified index position.
Delete:
Method 1:delete (int start,int end) Deletes the data for the buffer.
return type: StringBuffer
Method: Delete (int start,int end) Removes the data from start to end
The containing header does not contain a tail, that is, the position that contains start, and does not contain the position of end.
Method 2:deletecharat (index) deletes the character at the specified index position
Replace:
Method 1:replace (position 1, Position 2, (string type) replace content)
return type is StringBuffer
Method: replace (position 1, Position 2, (string type) replaces content)
Replace the data from position 1 to position 2 (without location 2) with the string content you want to replace.
Method 2:setcharat (position, content to replace)
return type void
Method: Setcharat (int position, char to replace) the data from the specified position with the substituted character content.
Invert: Reverse () returns the type StringBuffer.
有关的代码实例:
1 classStringbufferdemo2 {3 Public Static voidMain (String args[])4 {5 //Function_add ();6 //Function_del ();7StringBuffer SB =NewStringBuffer ("ABCDEFG");8 Char[] ch =New Char[6];9Sb.getchars (1,4Ch1);Ten for(inti =0; I <=ch.length; i++) One { APrinter"CH ["+i+"]="+ch[i]+";"); - } - } the Public Static voidFunction_del () - { -StringBuffer SB =NewStringBuffer ("ABCDEFG"); - + //Sb.delete (0,sb.length ());//clears the buffer. - +Sb.delete (2,5);//removes data from the string position 2 through 5 in the buffer, and does not contain position 5. A printer (sb.tostring ()); at //(sb.delete); -Sb.deletecharat (1);//Deleting a data is equivalent to the code above: Sb.delete; - - printer (sb.tostring ()); - - } in Public Static voidFunction_add () - { toStringBuffer SB =NewStringBuffer (); +Sb.append ("ABC"). Append ("def"). Append (true). Append (123); - the //StringBuffer sb1 = Sb.append (a); * $Sb.insert (1,"QQQ");//Inserts the string "QQQ" from position 1 into the buffer's data.Panax Notoginseng - //Sb.insert ("QQQ"); When compiling, there will be an exception with a corner mark crossing. the //printer ("Sb==sb1?:" + (SB==SB1)); + A printer (sb.tostring ()); the //printer (sb1.tostring ()); + } - $ Public Static voidprinter (String str) $ { -System. out. println (str); - } the}
JDK1.5版本以后出现了StringBuilder
StringBuffer是线程同步.
StringBuidlder是线程不同步,单线程的时候效率高.快捷.
以后开发建议使用StringBuilder.
JDK升级一般三个方面:
1. 提高效率
2. 提高安全性
简化书写
The underlying data type Object wrapper class:
Lowercase is the base data type uppercase starts with the class
int Integer
BYTE byte
float float
Double Double
Boolean Boolean
Char Character
Short Short
Long Long
1. Get the maximum value of an integer variable: integer.max_value
For example: Printer ("int max =" +integer.max_value);
The most common applications are:
Converts the base data type to a string.
Method 1: Basic data type + ""
Method 2: Basic data Type class. toString (base data type)
For example: Integer.tostring (34); Converts the integer 34 to "34" of the string type.
To convert a string to a base data type
Turn a string into an integer:
(Static call mode)
Integer.parseint ("32"); Converts the string "32" to an integer of 32.
The format is:
xxx a = xxx.parsexxx (String);
For example: int a = Integer.parseint ("123");
Non-static method conversions (object invocation mode)
Integer a = new Integer ("123");
int num = A.intvalue ();
Conversion between binaries:
Decimal to other binary:
Integer.tobinaystring (number to be transferred); Turn into binary
Integer.tohexstring (number to be transferred); Turn into hex
Integer.tooctalstring (number to be transferred); Turn into eight-binary
Other binary into decimal:
Format:
xxx a = xxx.parsexxx (the number to convert, the binary to be transferred)
For example: int a = Integer.parseint ("110", 2); Convert 110 to decimal number in binary.
The number in parentheses and the binary of the conversion need to correspond to each other. Otherwise, the compilation will occur unexpectedly.
Related Simple code Examples:
1 classIntegerdemo2 {3 Public Static voidMain (String args[])4 {5 //get the maximum value of an integer variable6Printer"int max ="+integer.max_value);7 8 intnum = Integer.parseint ("123");//turns the string "123" into an integer 123.9num = num +4;//Add integer 4 and integer 123 firstTenPrinter"num ="+num);//The result of the output is 127. One APrinter (integer.tobinarystring (-6));//turn decimal numbers into binary numbers -Printer (integer.tohexstring ( -));//turn decimal number to hexadecimal number -Printer (integer.tooctalstring ( -));//turn decimal numbers into octal numbers the - intA = Integer.parseint (" the",2);//converts binary number 110 to decimal number by 2 binary//the decimal number corresponding to the binary number 110 is 6. -Printer"a="+A);//results a=6. - + } - + Public Static voidprinter (String str) A { atSystem. out. println (str); - } - - -}
1 classIntegerDemo12 {3 Public Static voidMain (String args[])4 {5 //function ();6 //integer a = new integer (4);7Integer A =4;//Automatic Boxing8a=a+2;//A in a+2 is equivalent to doing a a.intvalue () operation, and a automatic unpacking becomes an int type. Then, with 2, the automatic boxing operation is assigned to a.9Printer"a="+a);Ten One AInteger x = -; -Integer y = -; -Printer"x==y:?"+ (x==y));//the result is false. the -Integer m =127; -Integer n =127; -Printer"m==n:?"+ (m==n));//The result is true. m and N point to the same integer object + //because when the value is within the byte range, for a new version after JDK1.5, if the value already exists, it will not open up new space. - + } A Public Static voidfunction () at { -Integer x =NewInteger ("123"); -Integer y =NewInteger (123); -Printer"x==y:?"+ (x==y)); -Printer"x.equals (y):?"+x.equals (y)); - } in Public Static voidprinter (String str) - { toSystem. out. println (str); + } - the *}
Java Basic Note-string Class 2