Description

On this document we will be showing a java example on how to use the append() method of BigInteger Class. The append() method is overloaded. It can accept multiple data type which is the following:

  • append(boolean b)
  • append(char c)
  • append(char[] str)
  • append(char[] str, int offset, int len)
  • append(CharSequence s)
  • append(CharSequence s, int start, int end)
  • append(double d)
  • append(float f)
  • append(int i)
  • append(long lng)
  • append(Object obj)
  • append(String str)
  • append(StringBuffer sb)

A quick background on Method Overloading

Method overloading is one of the feature of java where we can use the same method name but with different method signature.

Method Syntax

  • public StringBuilder append(Object obj)
  • public StringBuilder append(String str)
  • public StringBuilder append(StringBuffer sb)
  • public StringBuilder append(CharSequence s)
  • public StringBuilder append(CharSequence s, int start, int end)
  • public StringBuilder append(char[] str)
  • public StringBuilder append(char[] str, int offset, int len)
  • public StringBuilder append(boolean b)
  • public StringBuilder append(int i)
  • public StringBuilder append(long lng)
  • public StringBuilder append(float f)
  • public StringBuilder append(double d)

Method Returns

The append() method returns this StringBuilder with the method argument appended.

Compatibility

Requires Java 1.5 and up

Java StringBuilder append() Example

Below is a java code demonstrates the use of append() method of BigInteger class. The example presented might be simple however it shows the behavior of the append() method.

package com.javatutorialhq.java.examples;



/*
 * A java example source code to demonstrate
 * the use of append() method of StringBuilder class
 */

public class StringBuilderAppendExample {

	public static void main(String[] args) {	
		
		StringBuilder sb = new StringBuilder("this");
		System.out.println("Value:"+sb);
		Object obj = new String(" is a string");
		sb.append(obj);
		System.out.println("Value after adding object:"+sb);
		sb.append("-");
		System.out.println("Value after adding string:"+sb);
		long longVal = 123;
		sb.append(longVal);
		System.out.println("Value after adding long:"+sb);
		boolean b = false;
		sb.append(b);
		System.out.println("Value after adding boolean:"+sb);		
		
	}

}

This example is a lot simpler than it looks. We just declared several data types and appended one by one to our character sequences to simulate the behavior of StringBuilder and append method.

Sample Output

Below is the sample output when you run the above example.

StringBuilder append() example output