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 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


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


My Personal Notes arrow_drop_up

Budding Web DeveloperKeen learnerAverage CoderDancer&Social Activist

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.