Monday, November 18, 2024
Google search engine
HomeLanguagesJavaCharBuffer put() methods in Java

CharBuffer put() methods in Java

put(char i)

The put(char i) method of java.nio.CharBuffer Class is used to write the given character into the newly created char buffer at the current position and then increments the position.

Syntax :  

public abstract CharBuffer put(char i)

Parameters: This method takes the char value i as a parameter which is to be written in char buffer.

Return Value: This method returns this buffer, in which the char 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(char i) method:

Example 1: 

Java




// Java program to demonstrate
// put(char i) method
 
import java.nio.*;
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Declaring the capacity of the CharBuffer
        int capacity = 3;
 
        // Creating the CharBuffer
        try {
 
            // creating object of Charbuffer
            // and allocating size capacity
            CharBuffer cb = CharBuffer.allocate(capacity);
 
            // putting the value in Charbuffer using put() method
            cb.put('a');
            cb.put('b');
            cb.put('c');
            cb.rewind();
 
            // print the CharBuffer
            System.out.println("Original CharBuffer: "
                               + Arrays.toString(cb.array()));
        }
 
        catch (BufferOverflowException e) {
 
            System.out.println("Exception throws : " + e);
        }
 
        catch (ReadOnlyBufferException e) {
 
            System.out.println("Exception throws : " + e);
        }
    }
}


Output: 

Original CharBuffer: [a, b, c]



 

Example 2: To demonstrate BufferOverflowException.

Java




// 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 CharBuffer
        int capacity = 3;
 
        // Creating the CharBuffer
        try {
 
            // creating object of Charbuffer
            // and allocating size capacity
            CharBuffer cb = CharBuffer.allocate(capacity);
 
            // putting the value in Charbuffer using put() method
            cb.put('a');
            cb.put('b');
            cb.put('c');
 
            System.out.println("Trying to put the Int at the "
                               + "position more than its limit");
            cb.put('d');
 
            // rewind the Charbuffer
            cb.rewind();
 
            // print the CharBuffer
            System.out.println("Original CharBuffer: "
                               + Arrays.toString(cb.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




// 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 CharBuffer
        int capacity = 3;
 
        // Creating the CharBuffer
        try {
 
            // creating object of Charbuffer
            // and allocating size capacity using allocate() method
            CharBuffer cb = CharBuffer.allocate(capacity);
 
            // Creating a read-only copy of CharBuffer
            // using asReadOnlyBuffer() method
            CharBuffer cb1 = cb.asReadOnlyBuffer();
 
            System.out.println("Trying to put the Int value"
                               + " in read only buffer");
 
            // putting the value in readonly Charbuffer
            // using put() method
            cb1.put('a');
        }
 
        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, char i)

The put(int index, char i) method of java.nio.CharBuffer Class is used to write the given int into the buffer at the given index.

Syntax:  

public abstract CharBuffer put(int index, char i)

Parameters: This method takes the following arguments as a parameter: 

  • index: The index at which the char will be written
  • i: The char 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, char i) method:

Example 1: 

Java




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


Output: 

Original CharBuffer: [a, b, c]



 

Example 2: To demonstrate IndexOutOfBoundsException.

Java




// 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 CharBuffer
        int capacity = 3;
 
        // Creating the CharBuffer
        try {
 
            // creating object of Charbuffer
            // and allocating size capacity
            CharBuffer cb = CharBuffer.allocate(capacity);
 
            // putting the value in Charbuffer
            // using put() at index 0
            cb.put(0, 'c');
 
            // putting the value in Charbuffer
            // using put() at index 2
            cb.put(2, 'b');
 
            // putting the value in Charbuffer
            // using put() at index -1
            System.out.println("Trying to put the value"
                               + " at the negative index");
            cb.put(-1, 'a');
        }
 
        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




// 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 CharBuffer
        int capacity = 3;
 
        // Creating the CharBuffer
        try {
 
            // creating object of Charbuffer
            // and allocating size capacity using allocate() method
            CharBuffer cb = CharBuffer.allocate(capacity);
 
            // Creating a read-only copy of CharBuffer
            // using asReadOnlyBuffer() method
            CharBuffer cb1 = cb.asReadOnlyBuffer();
 
            System.out.println("Trying to put the Int value"
                               + " in read only buffer");
 
            // putting the value in readonly Charbuffer
            // using put() method
            cb1.put(0, 'a');
        }
 
        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



 

RELATED ARTICLES

Most Popular

Recent Comments