Home » Java programming language

Java BitSet clone() Method with Example

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

BitSet Class clone() method

  • clone() method is available in java.util package.
  • clone() method is used to clone this Bitset or in other words, this method is used to create a Bitset that is similar to this Bitset.
  • clone() 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.
  • clone() method does not throw an exception at the time of returning the cloned object.

Syntax:

    public Object clone();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is Object, it returns cloned object of this bit set.

Example:

// Java program to demonstrate the example 
// of Object clone() method of BitSet.

import java.util.*;

public class CloneOfBitSet {
    public static void main(String[] args) {
        // create an object of two BitSet
        BitSet bs1 = new BitSet(10);
        BitSet bs2 = new BitSet(10);

        // By using set() method is to set
        // the values in BitSet 1 
        bs1.set(10);
        bs1.set(20);
        bs1.set(30);
        bs1.set(40);
        bs1.set(50);

        // Display Bitset1 and BitSet2
        System.out.println("bs1: " + bs1);
        System.out.println("bs2: " + bs2);

        // By using clone() method is to copy the
        //e lements of BitSet1 to another BitSet2
        bs2 = (BitSet) bs1.clone();

        // Display BitSet 2
        System.out.println("bs1.clone()  : " + bs2);
    }
}

Output

bs1: {10, 20, 30, 40, 50}
bs2: {}
bs1.clone()  : {10, 20, 30, 40, 50}


Comments and Discussions!

Load comments ↻





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