Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is BitSet.toString() in Java?

abhilash

toString() is an instance method of the BitSet class that returns the string representation of the BitSet object.

The string representation includes the decimal representation of every index for which the BitSet object has a bit in the set state.

These indices are given in ascending order, separated by ", " (a comma and a space), and enclosed by braces, similar to the standard mathematical notation for a collection of numbers.

The toString method is defined in the BitSet class. The BitSet class is defined in the java.util package. To import the BitSet class, use the following import statement.

import java.util.BitSet;

Syntax


bitSet_Object.toString()

Parameters

This method has no parameters.

Return value

This method returns the string representation of the object.

Example

import java.util.BitSet;

public class Main{

    public static void main(String[] args) {
        BitSet bitSet = new BitSet();
        bitSet.set(2);
        bitSet.set(3);
        bitSet.set(9);
        System.out.println("String representation = " + bitSet.toString());
    }
}
Implementation of the toString() method

Code explanation

  • In line 1, we import the BitSet class.
  • In line 6, we create an empty BitSet object.
  • In lines 7, 8, and 9, we use the set method to set the indexes 2, 3, and 9.
  • In line 10, we print the string representation of the BitSet object.

RELATED TAGS

java
communitycreator
RELATED COURSES

View all Courses

Keep Exploring