CharBuffer get() methods in Java
Last Updated :
22 Jul, 2019
The
get() method of
java.nio.CharBuffer Class is used to reads the char at the given buffer's current position, and then increments the position.
Syntax:
public abstract char get()
Return Value: This method returns the char value at the buffer's current position.
Exceptions: This method throws
BufferUnderflowException - If the buffer's current position is not smaller than its limit, then this exception is thrown.
Below are the examples to illustrate the
get() method:
Example 1:
Java
// Java program to demonstrate
// get() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the CharBuffer
int capacity = 5;
// Creating the CharBuffer
try {
// creating object of Charbuffer
// and allocating size capacity
CharBuffer cb = CharBuffer.allocate(capacity);
// putting the value in Charbuffer
cb.put('a');
cb.put('b');
cb.put('c');
cb.rewind();
// print the CharBuffer
System.out.println("Original CharBuffer: "
+ Arrays.toString(cb.array()));
// Reads the Int at this buffer's current position
// using get() method
char value = cb.get();
// print the Int value
System.out.println("Int Value: " + value);
// Reads the Int at this buffer's next position
// using get() method
char value1 = cb.get();
// print the Int value
System.out.print("Next Int Value: " + value1);
}
catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException catched");
}
catch (ReadOnlyBufferException e) {
System.out.println("ReadOnlyBufferException catched");
}
catch (BufferUnderflowException e) {
System.out.println("Exception throws: " + e);
}
}
}
Output:
Original CharBuffer: [a, b, c, , ]
Int Value: a
Next Int Value: b
Examples 2:
Java
// Java program to demonstrate
// get() 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
cb.put('a');
cb.put('b');
// print the CharBuffer
System.out.println("Original CharBuffer: "
+ Arrays.toString(cb.array()));
// Reads the Char at this buffer's current position
// using get() method
char value = cb.get();
// print the Char value
System.out.println("Char Value: " + value);
// Reads the Char at this buffer's next position
// using get() method
System.out.print("Since the buffer current position is incremented\n");
System.out.print(" to greater than its limit ");
char value1 = cb.get();
// print the Char value
System.out.print("Next Char Value: " + value1);
}
catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException catched");
}
catch (ReadOnlyBufferException e) {
System.out.println("ReadOnlyBufferException catched");
}
catch (BufferUnderflowException e) {
System.out.println("Exception throws : " + e);
}
}
}
Output:
Original CharBuffer: [a, b, ]
Char Value:
Since the buffer current position is incremented
to greater than its limit Exception throws : java.nio.BufferUnderflowException
get(int index)
The
get(int index) method of CharBuffer is used to read the article at a specified index.
Syntax :
public abstract char get(int index)
Parameters: This method takes
index (The index from which the char will be read) as a parameter.
Return Value: This method returns the
char value at the given index.
Exception: This method throws
IndexOutOfBoundsException. If index is negative or not smaller than the buffer's limit this exception is thrown.
Below are the examples to illustrate the get(int index) method:
Examples 1:
Java
// Java program to demonstrate
// get(int index) 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
cb.put('a');
cb.put('b');
cb.put('c');
// print the CharBuffer
System.out.println("Original CharBuffer: "
+ Arrays.toString(cb.array()));
// Reads the Char at the index 0 of the Charbuffer
// using get() method
char value0 = cb.get(0);
// print the Char value
System.out.println("Char Value at index 0: " + value0);
// Reads the Char at the index 1 of the Charbuffer
// using get() method
char value1 = cb.get(1);
// print the Char value
System.out.println("Char Value at index 1: " + value1);
// Reads the Char at the index 2 of the Charbuffer
// using get() method
char value2 = cb.get(2);
// print the Char value
System.out.println("Char Value at index 2: " + value2);
}
catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException catched");
}
catch (IndexOutOfBoundsException e) {
System.out.println("ReadOnlyBufferException catched");
}
catch (BufferUnderflowException e) {
System.out.println("Exception throws : " + e);
}
}
}
Output:
Original CharBuffer: [a, b, c]
Char Value at index 0: a
Char Value at index 1: b
Char Value at index 2: c
Examples 2:
Java
// Java program to demonstrate
// get() 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
cb.put('a');
cb.put('b');
cb.put('c');
// print the CharBuffer
System.out.println("Original CharBuffer: "
+ Arrays.toString(cb.array()));
// Reads the Char at the index 0 of the Charbuffer
// using get() method
char value0 = cb.get(0);
// print the Char value
System.out.println("Char Value at index 0: " + value0);
// Reads the Char at the index 1 of the Charbuffer
// using get() method
char value1 = cb.get(1);
// print the Char value
System.out.println("Char Value at index 1: " + value1);
// Reads the Char at the index 2 of the Charbuffer
// using get() method
System.out.println("Trying to get the Char"
+ " of index greater than its limit ");
char value2 = cb.get(4);
// print the Char value
System.out.println("Char Value at index 2: " + value2);
}
catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException catched");
}
catch (IndexOutOfBoundsException e) {
System.out.println("Exception thrown: " + e);
}
catch (BufferUnderflowException e) {
System.out.println("Exception throws : " + e);
}
}
}
Output:
Original CharBuffer: [a, b, c]
Char Value at index 0: a
Char Value at index 1: b
Trying to get the Char of index greater than its limit
Exception thrown: java.lang.IndexOutOfBoundsException
Similar Reads
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 parame
6 min read
CharBuffer equals() method in Java
The equals() method of java.nio.CharBuffer Class is used to check whether or not the given buffer is equal to another object. Two char buffers are equal if, and only if, They have the same element type, They have the same number of remaining elements, and The two sequences of remaining elements, con
4 min read
CharBuffer compareTo() method in Java
compareTo() method of java.nio.charBuffer class is used to compare one buffer to another. Two char buffers are compared by comparing their sequences of remaining elements lexicographically, without considering the starting position of each sequence within its corresponding buffer. Pairs of char elem
4 min read
CharBuffer compact() method in Java
The compact() method of java.nio.CharBuffer Class is used to compact the given buffer. The values between the buffer's current position and its limit are copied to the beginning of the buffer. The buffer's position is then set to n+1 and its limit is set to its capacity. The buffer's position is set
3 min read
CharBuffer slice() method in Java
slice() method of java.nio.charBuffer Class is used to create a new char buffer whose content is a shared subsequence of the given buffer's content. The content of the new buffer will start from this buffer's current position. The new buffer will show the changes made in the buffer's content, and vi
3 min read
CharBuffer wrap() method in Java
wrap(char[ ] array) The wrap() method of java.nio.CharBuffer Class is used to wrap a character array into a buffer. The new buffer will be backed by the given char array. As a consequence, any modifications to the buffer will cause the array to be modified and vice versa. The new buffer's capacity i
4 min read
CharBuffer duplicate() method in Java
The duplicate() method of java.nio.CharBuffer Class is used to Create a new char buffer that shares the given 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, li
3 min read
CharBuffer array() method in Java
The array() method of java.nio.CharBuffer Class is used to return the char array that backs this buffer. Any modifications done to this buffer's content will cause the returned array's content also to be modified, and vice versa. hasArray() method is invoked before invoking this method in order to e
3 min read
CharBuffer hasArray() method in Java
The hasArray() method of java.nio.CharBuffer class is used to ensure whether or not the given buffer is backed by an accessible char array. It returns true if there is an accessible backing array to this buffer, else it returns false. If this method returns true, then the array() and arrayOffset() m
2 min read
Array get() Method in Java
The java.lang.reflect.Array.get() is an inbuilt method in Java and is used to return the element at a given index from the specified Array. Syntax Array.get(Object []array, int index) Parameters : This method accepts two mandatory parameters: array: The object array whose index is to be returned. in
3 min read