ShortBuffer put(int, short) method in Java With Examples


The put() method of java.nio.ShortBuffer Class is used to write the given short into this buffer at the given index.

Syntax:

public abstract ShortBuffer put(int index, short s)

Parameters:



  • index:This parameter specifies to the index at which the short will be written.It is optional.
  • s:This parameter specifies to the short value to be written

Return Value:This method returns the this buffer.

Exception:

  • IndexOutOfBoundsException – If index is negative or not smaller than the buffer’s limit.
  • ReadOnlyBufferException – If this buffer is read-only

Below program illustrates the put() method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate put() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the ShortBuffer
        int capacity = 3;
  
        // Creating the ShortBuffer
        try {
  
            // creating object of shortbuffer
            // and allocating size capacity
            ShortBuffer sb
                = ShortBuffer.allocate(capacity);
  
            // putting the value in shortbuffer
            // using put() at index 0
            sb.put(0, (short)400);
  
            // putting the value in shortbuffer
            // using put() at index 2
            sb.put(2, (short)1000);
  
            // putting the value in shortbuffer
            // using put() at index 1
            sb.put(1, (short)30);
  
            // rewinding the shortbuffer
            sb.rewind();
  
            // print the ShortBuffer
            System.out.println("Original ShortBuffer: "
                               + Arrays.toString(sb.array()));
        }
  
        catch (IndexOutOfBoundsException e) {
  
            System.out.println("Exception throws: " + e);
        }
  
        catch (ReadOnlyBufferException e) {
  
            System.out.println("Exception throws: " + e);
        }
    }
}

chevron_right


Output:

Original ShortBuffer: [400, 30, 1000]

Program 2:
To demonstrate IndexOutOfBoundsException.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate put() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the ShortBuffer
        int capacity = 3;
  
        // Creating the ShortBuffer
        try {
  
            // creating object of shortbuffer
            // and allocating size capacity
            ShortBuffer sb
                = ShortBuffer.allocate(capacity);
  
            // putting the value in shortbuffer
            // using put() at index 0
            sb.put(0, (short)31);
  
            // putting the value in shortbuffer
            // using put() at index 2
            sb.put(2, (short)49);
  
            // putting the value in shortbuffer
            // using put() at index -1
            System.out.println("Trying to put the value"
                               + " at the negative index");
            sb.put(-1, (short)27);
        }
  
        catch (IndexOutOfBoundsException e) {
  
            System.out.println("Exception throws: " + e);
        }
  
        catch (ReadOnlyBufferException e) {
  
            System.out.println("Exception throws: " + e);
        }
    }
}

chevron_right


Output:

Trying to put the value at the negative index
Exception throws: java.lang.IndexOutOfBoundsException

Program 3: To demonstrate ReadOnlyBufferException.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate put() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the ShortBuffer
        int capacity = 3;
  
        // Creating the ShortBuffer
        try {
  
            // creating object of shortbuffer
            // and allocating size capacity
            // using allocate() method
            ShortBuffer sb
                = ShortBuffer.allocate(capacity);
  
            // Creating a read-only copy of ShortBuffer
            // using asReadOnlyBuffer() method
            ShortBuffer sb1 = sb.asReadOnlyBuffer();
  
            System.out.println("Trying to put the float value"
                               + " in read only buffer");
  
            // putting the value in readonly shortbuffer
            // using put() method
            sb1.put(0, (short)13);
        }
  
        catch (BufferOverflowException e) {
  
            System.out.println("Exception throws: " + e);
        }
  
        catch (ReadOnlyBufferException e) {
  
            System.out.println("Exception throws: " + e);
        }
    }
}

chevron_right


Output:

Trying to put the float value in read only buffer
Exception throws: java.nio.ReadOnlyBufferException

Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/ShortBuffer.html#put-int-short-



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.