ShortBuffer duplicate() method in Java With Examples
Last Updated :
26 Jul, 2022
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
Similar Reads
ShortBuffer equals() method in Java with Examples The equals() method of java.nio.ShortBuffer Class is used to check whether or not the given buffer is equal to another object. Two short buffers are equal if, and only if, They have the same element type, They have the same number of remaining elements, and The two sequences of remaining elements, c
3 min read
ShortBuffer asReadOnlyBuffer() method in Java with Examples The asReadOnlyBuffer() method of java.nio.ShortBuffer Class is used to create a new, read-only short buffer with this bufferâs content. The new buffer is a replica of this buffer. Hence changes made to this bufferâs content will be visible in the new buffer. Since the new buffer is read-only, theref
3 min read
Buffer duplicate() method in Java with Examples The duplicate() method of java.nio.Buffer class is used to create a new 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
3 min read
IntBuffer duplicate() method in Java with Examples The duplicate() method of java.nio.IntBuffer Class is used to create a new IntBuffer that shares the given buffer's content, identically in all aspects. Syntax: public abstract IntBuffer duplicate() Return Value: This method returns the new IntBuffer which is carrying the previous IntBuffer's conten
3 min read
ByteBuffer duplicate() method in Java with Examples The duplicate() method of java.nio.ByteBuffer class is used to create a new byte 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,
3 min read
FloatBuffer duplicate() method in Java with Examples 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,
3 min read