Document directory
- Stringbuffer
- Stringbuffer
- Stringbuffer
Use of stringbuffer Constructor
When I read the 23rd questions in Java, I didn't really see where the problem was at first. So I posted the code in the book first, so let's take a look at it, because the code in the book has other traps besides the constructor traps, I modified the code in the book and there was only one constructor trap.
Problem code
Java
1 import java. util .*;
2
3 Public class rhymes {
4 Private Static random RND = new random ();
5
6 public static void main (string [] ARGs ){
7 stringbuffer word = NULL;
8 switch (RND. nextint (3 )){
9 Case 1: Word = new stringbuffer ('P ');
10 break;
11 case 2: Word = new stringbuffer ('G ');
12 break;
13 default: Word = new stringbuffer ('M ');
14 break;
15}
16 word. append ('A ');
17 word. append ('I ');
18 word. append ('n ');
19 system. Out. println (Word );
20}
21}
22
Look at this code and what results will it output? Pain? Gain? Or main?
It tells you that everything is wrong. It outputs only ain.
Do you know what the problem is? Let me remind you that the problem is above the stringbuffer constructor.
The Problem Analysis Section is as follows:
I don't know if you have any Java API help documentation at hand. I checked it and described the constructor as follows:
//////////////////////////////////////// //////////////////////////////////////// /////////
Stringbuffer
public StringBuffer()
-
Construct a string buffer with no characters. Its initial capacity is 16 characters.
Stringbuffer
public StringBuffer(int capacity)
-
Construct a string buffer with no characters but a specified initial capacity.
-
Parameters:
-
capacity
-Initial capacity.
-
Throw:
-
NegativeArraySizeException
-If
capacity
Parameter less
0
.
Stringbuffer
public StringBuffer(String str)
-
Constructs a string buffer and initializes its content to the specified string content. The initial capacity of this string is
16
Length of the string parameter.
-
Parameters:
-
str
-Initial content of the buffer.
-
Throw:
-
NullPointerException
-If
str
Is
null
Stringbuffer
public StringBuffer(CharSequence seq)
-
Public java. Lang. stringbuilder (charsequence SEQ) constructs a string buffer, which contains
CharSequence
The same character. The initial size of the string buffer is
16
Add
CharSequence
The length of the parameter.
If the specifiedCharSequence
If the length is less than or equal to 0, the returned capacity is16
.
-
Parameters:
-
seq
-The sequence to be copied.
-
Throw:
-
NullPointerException
-If
seq
Is
null
-
Start with the following versions:
-
1.5
//////////////////////////////////////// //////////////////////////////////////// /////////
There are four constructors in total, but none of them belong to the char type. Therefore, our program can compile and run, which indicates that the parameters of the constructor in the program have been converted. which of the four constructor methods can satisfy is public.Stringbuffer(INT capacity). All characters in the parameter have been converted to the int type. The actual task is to allocate the blank buffer corresponding to the size of the int type. in this case, the final result is "Ain. add a few characters to the blank buffer, which is displayed when the output is printed.
So pay attention to the stringbuffer construction method, including when using other APIs. Do not take it for granted. If you are confused, check the API documentation.