get()
The get() method of java.nio.IntBuffer Class is used to reads the int at the given buffer’s current position, and then increments the position.
Syntax :
public abstract int get()
Return Value: This method returns the int value at the buffer’s current position.
Throws: 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:
Examples 1:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 5 ;
try {
IntBuffer ib = IntBuffer.allocate(capacity);
ib.put( 8 );
ib.put( 9 );
ib.put( 1 );
ib.rewind();
System.out.println( "Original IntBuffer: "
+ Arrays.toString(ib.array()));
int value = ib.get();
System.out.println( "Int Value: " + value);
int value1 = ib.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 IntBuffer: [8, 9, 1, 0, 0]
Int Value: 8
Next Int Value: 9
Examples 2: To demonstrate java.nio.BufferUnderflowException
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
IntBuffer ib = IntBuffer.allocate(capacity);
ib.put( 8 );
ib.put( 9 );
System.out.println( "Original IntBuffer: "
+ Arrays.toString(ib.array()));
int value = ib.get();
System.out.println( "Int Value: " + value);
System.out.print( "Since the buffer current position is incremented" );
System.out.print( " to greater than its limit " );
int value1 = ib.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 IntBuffer: [8, 9, 0]
Int Value: 0
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 IntBuffer is used to read the article at a specified index.
Syntax :
public abstract int get(int index)
Parameters: This method takes index (The index from which the int will be read) as a parameter.
Return Value: This method returns the int 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 {
IntBuffer ib = IntBuffer.allocate(capacity);
ib.put( 8 );
ib.put( 9 );
ib.put( 6 );
System.out.println( "Original IntBuffer: "
+ Arrays.toString(ib.array()));
int value0 = ib.get( 0 );
System.out.println( "Int Value at index 0: " + value0);
int value1 = ib.get( 1 );
System.out.println( "Int Value at index 1: " + value1);
int value2 = ib.get( 2 );
System.out.println( "Int 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 IntBuffer: [8, 9, 6]
Int Value at index 0: 8
Int Value at index 1: 9
Int Value at index 2: 6
Examples 2: To demonstrate IndexOutOfBoundsException
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
IntBuffer ib = IntBuffer.allocate(capacity);
ib.put( 6 );
ib.put( 8 );
ib.put( 12 );
System.out.println( "Original IntBuffer: "
+ Arrays.toString(ib.array()));
int value0 = ib.get( 0 );
System.out.println( "Int Value at index 0: " + value0);
int value1 = ib.get( 1 );
System.out.println( "Int Value at index 1: " + value1);
System.out.println( "Trying to get the Int"
+ " of index greater than its limit " );
int value2 = ib.get( 4 );
System.out.println( "Int 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 IntBuffer: [6, 8, 12]
Int Value at index 0: 6
Int Value at index 1: 8
Trying to get the Int of index greater than its limit
Exception thrown: java.lang.IndexOutOfBoundsException
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
22 Jul, 2019
Like Article
Save Article