java.lang.StringBuffer append(boolean b)
Description :
This java tutorial shows how to use the append(boolean b) method of StringBuffer class under java.lang package. The append method of StringBuffer class is overloaded which takes different data type as input however on this case the input is boolean data type thus a character sequence “true” or false is appended on the base string.
Method Syntax :
public StringBuffer append(boolean b)
Parameter Input :
DataType | Parameter | Description |
---|---|---|
boolean | b | This is the value to be converted to character sequence which will be appended on the base string. |
Method Returns :
The append(boolean) method simply returns the reference object which is the combined String representation of the base string the the boolean method input.
Compatibility Version :
Requires Java 1.0 and up
Discussion :
The append method is overloaded thus this method can generally takes different data types however on this case it is boolean. Technically speaking the boolean b method argument is converted using the static method String,valueOf(boolean b).
There are two possible value of the method input, its either true of false thus we would be expecting the string equivalent of either one of these boolean values will be appended on the current String on the buffer.
Java Code Example :
This java example source code demonstrates the use of append(boolean b) method of StringBuffer class. Basically this code just takes a user input and then the program will translate it into boolean value and eventually append the translated user input to the original String on the buffer. This source code is a good example in learning on how to initialize StringBuffer object, modify the contents of the StringBuffer object and printing of the current String on the buffer.
package com.javatutorialhq.java.examples; import static java.lang.System.*; import java.util.Scanner; /* * This example source code demonstrates the use of append(boolean b) * of StringBuffer class */ public class StringBufferAppendBoolean { public static void main(String[] args) { String name = "Andie"; String isMale = ""; boolean flag; // initialize the StringBuffer object StringBuffer sb = new StringBuffer(name+"|"); // ask for user input System.out.print("Are you male (y/n)?"); Scanner s = new Scanner(System.in); isMale = s.nextLine(); if(isMale.equals("y")){ flag = true; } else{ flag=false; } sb.append(flag); System.out.println("Result String:"+sb); } }
Sample Output :
Running the append(boolean b) method example source code of StringBuffer class will give you the following output
Are you male (y/n)?y Andie|true
Exception Scenario :
N/A
Similar Method :
- N/A