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:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 5 ;
try {
CharBuffer cb = CharBuffer.allocate(capacity);
cb.put( 'a' );
cb.put( 'b' );
cb.put( 'c' );
cb.rewind();
System.out.println( "Original CharBuffer: "
+ Arrays.toString(cb.array()));
char value = cb.get();
System.out.println( "Int Value: " + value);
char value1 = cb.get();
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:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
CharBuffer cb = CharBuffer.allocate(capacity);
cb.put( 'a' );
cb.put( 'b' );
System.out.println( "Original CharBuffer: "
+ Arrays.toString(cb.array()));
char value = cb.get();
System.out.println( "Char Value: " + value);
System.out.print( "Since the buffer current position is incremented\n" );
System.out.print( " to greater than its limit " );
char value1 = cb.get();
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:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
CharBuffer cb = CharBuffer.allocate(capacity);
cb.put( 'a' );
cb.put( 'b' );
cb.put( 'c' );
System.out.println( "Original CharBuffer: "
+ Arrays.toString(cb.array()));
char value0 = cb.get( 0 );
System.out.println( "Char Value at index 0: " + value0);
char value1 = cb.get( 1 );
System.out.println( "Char Value at index 1: " + value1);
char value2 = cb.get( 2 );
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:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
CharBuffer cb = CharBuffer.allocate(capacity);
cb.put( 'a' );
cb.put( 'b' );
cb.put( 'c' );
System.out.println( "Original CharBuffer: "
+ Arrays.toString(cb.array()));
char value0 = cb.get( 0 );
System.out.println( "Char Value at index 0: " + value0);
char value1 = cb.get( 1 );
System.out.println( "Char Value at index 1: " + value1);
System.out.println( "Trying to get the Char"
+ " of index greater than its limit " );
char value2 = cb.get( 4 );
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...