beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

StringBuilder.append(boolean b) Method

By Chaitanya Singh | Filed Under: Java.lang.StringBuilder

Description

public StringBuilder append(boolean b): This method appends the string representation of the boolean value(provided as an argument to this method, during call) to the StringBuilder sequence.

Example

This example shows two ways to append a boolean value to the StringBuilder. We can append the value directly by using boolean literal or by using boolean variable. In both the cases, the string representation of boolean values “true” or “false” would be append to the StringBuilder. Here is the complete code:

import java.lang.StringBuilder;
class StringBuilderAppend{ 
  public static void main(String[] args) {
     StringBuilder sb = new StringBuilder("My Initial String");
 
     // Appending boolean value
     sb.append(true);
 
     // Initializing a boolean variable
     boolean bvar = false;
 
     // Appending Boolean variable
     sb.append(bvar);
 
     // Display StringBuilder
     System.out.println(sb);
  }
}

Output:

My Initial Stringtruefalse

In the above example we have appended a boolean literal and a boolean variable. In both the cases we have used the method StringBuilder.append(boolean b). As you can see, the output is having the String representation of boolean values appended to the String initialized by StringBuilder instance.

Enjoyed this post? Try these related posts

  1. StringBuilder.charAt() Method
  2. StringBuilder.capacity() Method
  3. StringBuilder.append(char c) Method

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap