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 vissble 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 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)); } } |
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 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)); } } |
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
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- ShortBuffer order() Method in Java with Examples
- ShortBuffer slice() method in Java with Examples
- ShortBuffer allocate() method in Java With Examples
- ShortBuffer compact() method in Java With Examples
- ShortBuffer arrayOffset() method in Java With Examples
- ShortBuffer asReadOnlyBuffer() method in Java with Examples
- ShortBuffer compareTo method in Java with Examples
- ShortBuffer equals() method in Java with Examples
- ShortBuffer hasArray() method in Java with Examples
- ShortBuffer array() Method in Java with Examples
- ShortBuffer toString() method in Java with Examples
- ShortBuffer isDirect() Method in Java with Examples
- ShortBuffer hashCode() Method in Java with Examples
- ShortBuffer put(int, short) method in Java With Examples
- ShortBuffer limit() method in Java with Examples
- ShortBuffer reset() method in Java with Examples
- ShortBuffer rewind() method in Java with Examples
- ShortBuffer mark() method in Java with Examples
- ShortBuffer clear() methods in Java with Examples
- ShortBuffer flip() methods in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

