StringBuffer, StringBuilder, and wrapper types in Java

Source: Internet
Author: User

1. How do I use the StringBuffer class?

Answer: 1). The StringBuffer class is used to represent a string that can be modified, called a string buffer object;

2). Strings that use operators will automatically create string buffer objects;

Example: STR1 + str2 operation, in fact, str1 and STR2 are created StringBuffer class objects;

What are the common overloaded ways of constructing a 2.StringBuffer class?

A: StringBuffer () creates an empty StringBuffer object that retains 16 characters of buffer space by default

StringBuffer (String str) creates a StringBuffer object based on the contents of the string str and reserves the buffer space of 16 characters by default

StringBuffer (int capacity) creates an empty StringBuffer object, buffer space size specified by capacity

What are the common methods of the 3.StringBuffer class?

Answer: *stringbuffer Insert (int index, x x);

Insert X into position indexed to index, x can be any type of data

*int length ();

Gets the length of the current StringBuffer object

*void setcharat (int index,char ch);

Replaces the character at the position specified by index with the new value specified by CH

*string toString ();

Convert to String form

*stringbuffer reverse ();

Inverts the sequence of characters in the current StringBuffer object

*stringbuffer Delete (int start, int end);

Deletes the character sequence from the start position in the current object until the index position specified by end

*stringbuffer deletecharat (int index);

The character at index specified by will be deleted

*stringbuffer replace (int start, int end, String str);

This method replaces another set of characters with one set of characters. The replacement string will be replaced with the position specified by start until the end specified by end

What is the difference between a 4.String class and a StringBuffer class?

A: 1). Once a string is created in Java, it cannot be changed directly, which is the invariance of character string;

2) The StringBuffer class is the variable sequence of characters provided for this problem;

3). StringBuffer is the same class as String, and the only difference is that it can be changed;

5. What is StringBuilder?

Answer: 1). After JDK 1.5, a new character buffer is provided: StringBuilder;

2). Provide and StringBuffer similar APIs;

3). Non-thread safe

What is the difference between 6.StringBuffer and StringBuilder?

For:

StringBuffer

StringBuilder

Thread is secure

Safety

Not safe

Efficiency

Relatively low

Relatively high

7. How do I use the wrapper class (wrapper class)?

A: 1). Variables declared with the base data type, such as:

int num = 10;

Num Here is just a variable, not an object;

2). In some cases where the object must be manipulated, such a variable cannot be used;

3). Java provides a series of wrapper classes to manipulate basic data types as objects;

4). In the Java.lang package, there is a corresponding wrapper class for each base data type.

8. What is the wrapper class for each basic data type?

For:

Basic data types

Packing class

Boolean (Boolean)

Boolean

Byte (byte type)

Byte

char (character type)

Character

Short (shorter integer type)

Short

int (shaping)

Integer

Long (integer)

Long

Float (float type)

Float

Double (dual-precision floating-point type)

Double

9. How do I use the valueof method? Examples Show

A: Each wrapper class has a static valueof method that converts a string into an object of the corresponding wrapper class.

public class langdemo{

public static void Main (string[] args) {

String str = "120";

If the conversion fails, a NumberFormatException exception is thrown

Byte objbyte = byte.valueof (str);

Short objshort = short.valueof (str);

Integer objint = integer.valueof (str);

Long Objlong = long.valueof (str);

System.out.println (Objbyte);

System.out.println (Objshort);

System.out.println (Objint);

System.out.println (Objlong);

}

}

10. What is the Parsexxx method of the packaging class? What are the precautions? Examples Show

A: In addition to the Boolean class and the character class, other wrapper classes have a static Parsexxx method (XXX refers to a specific data type), which is used to convert the string to the corresponding base data type value.

public class parsetest{

public static void Main (string[] args) {

String str = "116";

The Pasexxx method of each wrapper class is called separately to convert the string, and if the conversion fails, the exception is reported

int i = Integer.paeseint (str);

Short s = short.parseshort (str);

byte B = byte.parseshort (str);

Long L = long.parselong (str);

float f = float.parsefloat (str);

Double d = double.parsedouble (str);

System.out.println (i);

System.out.println (s);

System.out.println (b);

System.out.println (l);

System.out.println (f);

System.out.println (d);

}

}

What are the common methods in the 11.Character class? Examples Show

For:

Method prototypes

Description

Boolean isletter (char ch)

Determine if the character ch is an English letter

Boolean isdigit (char ch)

Determines whether the character Ch is a number between 0---9

Boolean isuppercase (char ch)

Determines if the character ch is in uppercase form

Boolean islowercase (char ch)

Determines if the character ch is in lowercase form

Boolean iswhitespace (char ch)

Determine if the character ch is a space or line break

Note: The above methods are static methods, can be called directly through the class name, the return value is a Boolean type, if True, return false otherwise.

Cases:

public class characterdemo{

public static void Main (string[] args) {

Char[] Chararray = {' * ', ' 7 ', ' B ', ', ' A '};

for (int I = 0;i < chararray.length;i++) {

if (Character.isdigit (Chararray[i])) {

System.out.println (Chararray[i]) + "is a number. ”);

}

if (Character.isletter (Chararray[i])) {

System.out.println (Chararray[i]) + "is a letter. ”);

}

if (Character.iswhitespace (Chararray[i])) {

System.out.println (Chararray[i]) + "is a space. ”);

}

if (Character.islowercase (Chararray[i])) {

System.out.println (Chararray[i]) + "is a lowercase form. ”);

}

if (Character.isuppercase (Chararray[i])) {

System.out.println (Chararray[i]) + "is in uppercase form. ”);

}

}

}

}

12. What is packing class unboxing? Examples Show

Answer: JDK5.0 provides automatic sealing box operation

public static void Main (string[] args) {

int x = 1;

Integer y = x;

y++; Packing

System.out.println ("y=" +y+ "z=" +z);

}

StringBuffer, StringBuilder, and wrapper types in Java

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.