Home »
Java programming language
Java BitSet toString() Method with Example
BitSet Class toString() method: Here, we are going to learn about the toString() method of BitSet Class with its syntax and example.
Submitted by Preeti Jain, on January 22, 2020
BitSet Class toString() method
- toString() method is available in java.util package.
- toString() method is used to represent string denotation of this BitSet so the representation would be from the first index to the last index separated by ',' or whitespace "" and surrounded by curly braces {}.
- toString() method is a non-static method, so it is accessible with the class object and if we try to access the method with the class name then we will get an error.
- toString() method does not throw an exception at the time of string representation of this BitSet.
Syntax:
public String toString();
Parameter(s):
- It does not accept any parameter.
Return value:
The return type of this method is String, it returns string representation of this BitSet.
Example:
// Java program to demonstrate the example
// of String toString() method of BitSet.
import java.util.*;
public class ToStringOfBitSet {
public static void main(String[] args) {
// create an object of BitSet
BitSet bs = new BitSet(10);
// By using set() method is to set
// the values in BitSet
bs.set(10);
bs.set(20);
bs.set(30);
bs.set(40);
bs.set(50);
// Display Bitset
System.out.println("bs: " + bs);
// By using toString() method is to
// string representation of this BitSet
String s = bs.toString();
// Display s
System.out.println("bs.toString(): " + s);
}
}
Output
bs: {10, 20, 30, 40, 50}
bs.toString(): {10, 20, 30, 40, 50}