ShortBuffer duplicate() method in Java With Examples
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
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity1 = 10 ;
ShortBuffer sb1 = ShortBuffer.allocate(capacity1);
sb1.put(( short ) 100 );
sb1.put(( short ) 400 );
sb1.put(( short ) 210 );
sb1.rewind();
System.out.println("Original ShortBuffer: "
+ Arrays.toString(sb1.array()));
ShortBuffer sb2 = sb1.duplicate();
System.out.println("Duplicate ShortBuffer: "
+ Arrays.toString(sb2.array()));
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
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity1 = 10 ;
ShortBuffer sb1 = ShortBuffer.allocate(capacity1);
ShortBuffer sb3 = ShortBuffer.allocate(capacity1);
sb1.put(( short ) 100 );
sb1.put(( short ) 400 );
sb1.put(( short ) 210 );
sb1.rewind();
System.out.println("Original ShortBuffer: "
+ Arrays.toString(sb1.array()));
ShortBuffer sb2 = sb1.duplicate();
System.out.println("Duplicate ShortBuffer: "
+ Arrays.toString(sb2.array()));
System.out.println("are sb1 and sb2 equal: "
+ sb1.equals(sb2));
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
Share your thoughts in the comments
Please Login to comment...