Open In App
Related Articles

IntBuffer put() methods in Java | Set 1

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

put(int i)

The put(int i) method of java.nio.IntBuffer Class is used to write the given int into the newly created int buffer at the current position, and then increments the position. Syntax :
public abstract IntBuffer put(int i)
Parameters: This method takes the int value i as a parameter which is to be written in int buffer. Return Value: This method returns this buffer, in which the int value is inserted. Exception: This method throws the following exceptions:
  • BufferOverflowException- If this buffer’s current position is not smaller than its limit
  • ReadOnlyBufferException- If this buffer is read-only
Below are the examples to illustrate the put(int i) method: Example 1:
// 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 IntBuffer
        int capacity = 3;
  
        // Creating the IntBuffer
        try {
  
            // creating object of Intbuffer
            // and allocating size capacity
            IntBuffer ib = IntBuffer.allocate(capacity);
  
            // putting the value in Intbuffer using put() method
            ib.put(8);
            ib.put(9);
            ib.put(7);
            ib.rewind();
  
            // print the IntBuffer
            System.out.println("Original IntBuffer: "
                               + Arrays.toString(ib.array()));
        }
  
        catch (BufferOverflowException e) {
            System.out.println("Exception throws : " + e);
        }
  
        catch (ReadOnlyBufferException e) {
            System.out.println("Exception throws : " + e);
        }
    }
}

                    
Output:
Original IntBuffer: [8, 9, 7]
Example 2: To demonstrate BufferOverflowException.
// 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 IntBuffer
        int capacity = 3;
  
        // Creating the IntBuffer
        try {
  
            // creating object of Intbuffer
            // and allocating size capacity
            IntBuffer ib = IntBuffer.allocate(capacity);
  
            // putting the value in Intbuffer using put() method
            ib.put(8);
            ib.put(9);
            ib.put(7);
  
            System.out.println("Trying to put the Int at the "
                               + "position more than its limit");
            ib.put(7);
  
            // rewind the Intbuffer
            ib.rewind();
  
            // print the IntBuffer
            System.out.println("Original IntBuffer:  "
                               + Arrays.toString(ib.array()));
        }
  
        catch (BufferOverflowException e) {
            System.out.println("Exception throws : " + e);
        }
  
        catch (ReadOnlyBufferException e) {
            System.out.println("Exception throws : " + e);
        }
    }
}

                    
Output:
Trying to put the Int at the position more than its limit
Exception throws : java.nio.BufferOverflowException
Examples 3: To demonstrate ReadOnlyBufferException.
// 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 IntBuffer
        int capacity = 3;
  
        // Creating the IntBuffer
        try {
  
            // creating object of Intbuffer
            // and allocating size capacity using allocate() method
            IntBuffer ib = IntBuffer.allocate(capacity);
  
            // Creating a read-only copy of IntBuffer
            // using asReadOnlyBuffer() method
            IntBuffer ib1 = ib.asReadOnlyBuffer();
  
            System.out.println("Trying to put the Int value"
                               + " in read only buffer");
  
            // putting the value in readonly Intbuffer
            // using put() method
            ib1.put(8);
        }
  
        catch (BufferOverflowException e) {
            System.out.println("Exception throws : " + e);
        }
  
        catch (ReadOnlyBufferException e) {
            System.out.println("Exception throws : " + e);
        }
    }
}

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

put(int index, int i)

The put(int index, int i) method of java.nio.IntBuffer Class is used to write the given int into the buffer at the given index. Syntax:
public abstract IntBuffer put(int index, int i)
Parameters: This method takes the following arguments as a parameter:
  • index: The index at which the int will be written
  • i: The int value to be written
Return Value: This method returns this buffer. Exception: This method throws the following exception:
  • IndexOutOfBoundsException- If index is negative or not smaller than the buffer’s limit
  • ReadOnlyBufferException- If this buffer is read-only
Below are the examples to illustrate the put(int index, int i) method: Example 1:
// 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 IntBuffer
        int capacity = 3;
  
        // Creating the IntBuffer
        try {
  
            // creating object of Intbuffer
            // and allocating size capacity
            IntBuffer ib = IntBuffer.allocate(capacity);
  
            // putting the value in Intbuffer using put() at  index 0
            ib.put(0, 8);
  
            // putting the value in Intbuffer using put() at  index 2
            ib.put(2, 9);
  
            // putting the value in Intbuffer using put() at  index 1
            ib.put(1, 7);
  
            // rewinding the Intbuffer
            ib.rewind();
  
            // print the IntBuffer
            System.out.println("Original IntBuffer:  "
                               + Arrays.toString(ib.array()));
        }
  
        catch (IndexOutOfBoundsException e) {
            System.out.println("Exception throws : " + e);
        }
  
        catch (ReadOnlyBufferException e) {
            System.out.println("Exception throws : " + e);
        }
    }
}

                    
Output:
Original IntBuffer:  [8, 7, 9]
Example 2: To demonstrate IndexOutOfBoundsException.
// 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 IntBuffer
        int capacity = 3;
  
        // Creating the IntBuffer
        try {
  
            // creating object of Intbuffer
            // and allocating size capacity
            IntBuffer ib = IntBuffer.allocate(capacity);
  
            // putting the value in Intbuffer
            // using put() at  index 0
            ib.put(0, 8);
  
            // putting the value in Intbuffer
            // using put() at  index 2
            ib.put(2, 9);
  
            // putting the value in Intbuffer
            // using put() at  index -1
            System.out.println("Trying to put the value"
                               + " at the negative index");
            ib.put(-1, 7);
        }
  
        catch (IndexOutOfBoundsException e) {
            System.out.println("Exception throws : " + e);
        }
  
        catch (ReadOnlyBufferException e) {
            System.out.println("Exception throws : " + e);
        }
    }
}

                    
Output:
Trying to put the value at the negative index
Exception throws : java.lang.IndexOutOfBoundsException
Example 3: To demonstrate ReadOnlyBufferException.
// 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 IntBuffer
        int capacity = 3;
  
        // Creating the IntBuffer
        try {
  
            // creating object of Intbuffer
            // and allocating size capacity using allocate() method
            IntBuffer ib = IntBuffer.allocate(capacity);
  
            // Creating a read-only copy of IntBuffer
            // using asReadOnlyBuffer() method
            IntBuffer ib1 = ib.asReadOnlyBuffer();
  
            System.out.println("Trying to put the Int value"
                               + " in read only buffer");
  
            // putting the value in readonly Intbuffer
            // using put() method
            ib1.put(0, 8);
        }
  
        catch (BufferOverflowException e) {
            System.out.println("Exception throws : " + e);
        }
  
        catch (ReadOnlyBufferException e) {
            System.out.println("Exception throws : " + e);
        }
    }
}

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


Last Updated : 20 Sep, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads