Use of stringbuffer Constructor

Source: Internet
Author: User
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 capacityParameter 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 strIs null

Stringbuffer
public StringBuffer(CharSequence seq)
Public java. Lang. stringbuilder (charsequence SEQ) constructs a string buffer, which contains CharSequenceThe same character. The initial size of the string buffer is 16Add CharSequenceThe length of the parameter.

If the specifiedCharSequenceIf the length is less than or equal to 0, the returned capacity is16.

Parameters:
seq-The sequence to be copied.
Throw:
NullPointerException-If seqIs 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.

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.