Home » Java programming language

Java BitSet size() Method with Example

BitSet Class size() method: Here, we are going to learn about the size() method of BitSet Class with its syntax and example.
Submitted by Preeti Jain, on January 22, 2020

BitSet Class size() method

  • size() method is available in java.util package.
  • size() method is used to return the size (i.e. the memory allocated to the actual number of bits by using this BitSet to denote bits.
  • size() 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.
  • size() method does not throw an exception at the time of returning the size of this BitSet.

Syntax:

    public int size();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is int, it returns the size (i.e. the number of bits sets in this BitSet).

Example:

// Java program to demonstrate the example 
// of int size() method of BitSet.

import java.util.*;

public class SizeOfBitSet {
    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 size() method is to return 
        // the size of this BitSet
        int size = bs.size();

        // Display size
        System.out.println("bs.size(): " + size);
    }
}

Output

bs: {10, 20, 30, 40, 50}
bs.size(): 64


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.