Open In App

ShortBuffer duplicate() method in Java With Examples

Improve
Improve
Like Article
Like
Save
Share
Report

The duplicate() method of java.nio.ShortBuffer Class creates a new short buffer that shares this 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. 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 ShortBuffer duplicate()

Return Value: This method returns the new ShortBuffer

Below program illustrates the duplicate() method: 

Program 1

Java




// Java program to demonstrate
// compareTo() method
 
import java.nio.*;
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
    {
        // Declaring the capacity of the sb
        int capacity1 = 10;
        // Creating the ShortBuffer
 
        // creating object of shortbuffer sb
        // and allocating size capacity
        ShortBuffer sb1 = ShortBuffer.allocate(capacity1);
 
        // putting the value in sb
        sb1.put((short)100);
        sb1.put((short)400);
        sb1.put((short)210);
 
        // revind the short buffer
        sb1.rewind();
 
        // print the Original ShortBuffer
        System.out.println("Original ShortBuffer:  "
                           + Arrays.toString(sb1.array()));
 
        // creating duplicate
        ShortBuffer sb2 = sb1.duplicate();
 
        // print the duplicate copy of ShortBuffer
        System.out.println("Duplicate ShortBuffer: "
                           + Arrays.toString(sb2.array()));
 
        // checking if the duplicate is correct or not
        System.out.println("are sb1 and sb2 equal: "
                           + sb1.equals(sb2));
    }
}


Output:

Original ShortBuffer:  [100, 400, 210, 0, 0, 0, 0, 0, 0, 0]
Duplicate ShortBuffer: [100, 400, 210, 0, 0, 0, 0, 0, 0, 0]
are sb1 and sb2 equal: true

Program 2

Java




// Java program to demonstrate
// compareTo() method
 
import java.nio.*;
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Declaring the capacity of the sb
        int capacity1 = 10;
 
        // Creating the ShortBuffer
 
        // creating object of shortbuffer sb
        // and allocating size capacity
        ShortBuffer sb1 = ShortBuffer.allocate(capacity1);
        ShortBuffer sb3 = ShortBuffer.allocate(capacity1);
 
        // putting the value in sb
        sb1.put((short)100);
        sb1.put((short)400);
        sb1.put((short)210);
 
        // revind the short buffer
        sb1.rewind();
 
        // print the Original ShortBuffer
        System.out.println("Original ShortBuffer:  "
                           + Arrays.toString(sb1.array()));
 
        // creating duplicate
        ShortBuffer sb2 = sb1.duplicate();
 
        // print the duplicate copy of ShortBuffer
        System.out.println("Duplicate ShortBuffer: "
                           + Arrays.toString(sb2.array()));
 
        // checking if the duplicate is correct or not
        System.out.println("are sb1 and sb2 equal: "
                           + sb1.equals(sb2));
 
        // checking if the duplicate is correct or not
        System.out.println("are sb2 and sb3 equal: "
                           + sb2.equals(sb3));
    }
}


Output:

Original ShortBuffer:  [100, 400, 210, 0, 0, 0, 0, 0, 0, 0]
Duplicate ShortBuffer: [100, 400, 210, 0, 0, 0, 0, 0, 0, 0]
are sb1 and sb2 equal: true
are sb2 and sb3 equal: false


Last Updated : 26 Jul, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads