Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

FloatBuffer duplicate() method in Java with Examples

  • Last Updated : 17 Oct, 2019

The duplicate() method of java.nio.FloatBuffer Class is used to Create a new float buffer that shares the given buffer’s content.

The content of the new buffer will be that of this buffer. Changes to this buffer’s content will be visible in the new buffer, and vice versa; the two buffers’ position, limit, and mark values will be independent.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

The new buffer’s capacity, limit, position, and mark values will be identical to those of this buffer. The new buffer will be direct if, and only if, this buffer is direct, and it will be read-only if, and only if, this buffer is read-only.

Syntax :



public abstract FloatBuffer duplicate()

Return Value: This method returns the new float buffer which is carrying the previous float buffer content

Below are the examples to illustrate the duplicate() method:

Examples 1: Using direct floatbuffer




// Java program to demonstrate
// duplicate() method
// Using direct floatbuffer
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the FloatBuffer
        int capacity = 10;
  
        // Creating the FloatBuffer
        try {
  
            // creating object of floatbuffer
            // and allocating size capacity
            FloatBuffer fb1 = FloatBuffer.allocate(capacity);
  
            // putting the value in floatbuffer
            fb1.put(8.56F);
            fb1.put(2, 9.61F);
            fb1.rewind();
  
            // print the Original FloatBuffer
            System.out.println("Original FloatBuffer:  "
                               + Arrays.toString(fb1.array()));
  
            // Creating a duplicate copy of FloatBuffer
            // using duplicate() method
            FloatBuffer fb2 = fb1.duplicate();
  
            // print the duplicate copy of FloatBuffer
            System.out.print("\nDuplicate FloatBuffer: "
                             + Arrays.toString(fb2.array()));
        }
  
        catch (IllegalArgumentException e) {
  
            System.out.println("IllegalArgumentException catched");
        }
  
        catch (ReadOnlyBufferException e) {
  
            System.out.println("ReadOnlyBufferException catched");
        }
    }
}
Output:
Original FloatBuffer:  [8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

Duplicate FloatBuffer: [8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

Examples 2: Using read-onlyfloatbuffer




// Java program to demonstrate
// duplicate() method
// using read-onlyfloatbuffer
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the FloatBuffer
        int capacity = 10;
  
        // Creating the FloatBuffer
        try {
  
            // creating object of floatbuffer
            // and allocating size capacity
            FloatBuffer fb1 = FloatBuffer.allocate(capacity);
  
            // putting the value in floatbuffer
            fb1.put(8.56F);
            fb1.put(2, 9.61F);
            fb1.rewind();
  
            // print the Original FloatBuffer
            System.out.println("Original FloatBuffer:  "
                               + Arrays.toString(fb1.array()));
  
            // Creating a read-only copy of FloatBuffer
            // using asReadOnlyBuffer() method
            FloatBuffer readonly = fb1.asReadOnlyBuffer();
  
            // print the read-only copy of FloatBuffer
            System.out.print("\nread-only FloatBuffer:  ");
            while (readonly.hasRemaining())
                System.out.print(readonly.get() + ", ");
            System.out.println("");
  
            // Rewinding the readonly FloatBuffer
            readonly.rewind();
  
            // Creating a duplicate copy of FloatBuffer
            // using duplicate() method
            FloatBuffer fb2 = readonly.duplicate();
  
            // print the duplicate copy of FloatBuffer
            System.out.print("\nduplicate copy of read-only FloatBuffer:  ");
  
            while (fb2.hasRemaining())
                System.out.print(fb2.get() + ", ");
            System.out.println("");
        }
  
        catch (IllegalArgumentException e) {
  
            System.out.println("IllegalArgumentException catched");
        }
  
        catch (ReadOnlyBufferException e) {
  
            System.out.println("ReadOnlyBufferException catched");
        }
    }
}
Output:
Original FloatBuffer:  [8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

read-only FloatBuffer:  8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 

duplicate copy of read-only FloatBuffer:  8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!