Skip to content
Related Articles

Related Articles

Improve Article

LongBuffer get() methods in Java

  • Last Updated : 22 Jul, 2019

get()

The get() method of java.nio.LongBuffer Class is used to read the Long at the given buffer’s current position, and then increment the position.

Syntax :

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

public abstract Long get()

Return Value: This method returns the Long 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:

Example 1:




// 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 LongBuffer
        int capacity = 5;
  
        // Creating the LongBuffer
        try {
  
            // creating object of Longbuffer
            // and allocating size capacity
            LongBuffer ib = LongBuffer.allocate(capacity);
  
            // putting the value in Longbuffer
            ib.put(8);
            ib.put(9);
            ib.put(1);
            ib.rewind();
  
            // prLong the LongBuffer
            System.out.println("Original LongBuffer:  "
                               + Arrays.toString(ib.array()));
  
            // Reads the Long at this buffer's current position
            // using get() method
            Long value = ib.get();
  
            // prLong the Long value
            System.out.println("Long Value: " + value);
  
            // Reads the  Long at this buffer's next position
            // using get() method
            Long value1 = ib.get();
  
            // prLong the Long value
            System.out.print("Next Long 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 LongBuffer:  [8, 9, 1, 0, 0]
Long Value: 8
Next Long Value: 9

Example 2:




// 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 LongBuffer
        int capacity = 3;
  
        // Creating the LongBuffer
        try {
  
            // creating object of Longbuffer
            // and allocating size capacity
            LongBuffer ib = LongBuffer.allocate(capacity);
  
            // putting the value in Longbuffer
            ib.put(8);
            ib.put(9);
  
            // prLong the LongBuffer
            System.out.println("Original LongBuffer:  "
                               + Arrays.toString(ib.array()));
  
            // Reads the Long at this buffer's current position
            // using get() method
            Long value = ib.get();
  
            // prLong the Long value
            System.out.println("Long Value: " + value);
  
            // Reads the  Long at this buffer's next position
            // using get() method
            System.out.print("Since the buffer current position is incremented");
            System.out.print(" to greater than its limit ");
  
            Long value1 = ib.get();
  
            // prLong the Long value
            System.out.print("Next Long 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 LongBuffer:  [8, 9, 0]
Long Value: 0
Since the buffer current position is incremented to greater than its limit Exception throws : java.nio.BufferUnderflowException

get(Long index)

The get(Long index) method of LongBuffer is used to read the article at a specified index.

Syntax :

public abstract Long get(Long index)

Parameters: This method takes index (The index from which the Long will be read) as a parameter.



Return Value: This method returns the Long valueat 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(Long index) method:

Example 1:




// Java program to demonstrate
// get(Long index) method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the LongBuffer
        int capacity = 3;
  
        // Creating the LongBuffer
        try {
  
            // creating object of Longbuffer
            // and allocating size capacity
            LongBuffer ib = LongBuffer.allocate(capacity);
  
            // putting the value in Longbuffer
            ib.put(8);
            ib.put(9);
            ib.put(6);
  
            // prLong the LongBuffer
            System.out.println("Original LongBuffer:  "
                               + Arrays.toString(ib.array()));
  
            // Reads the Long at the index 0 of the Longbuffer
            // using get() method
            Long value0 = ib.get(0);
  
            // prLong the Long value
            System.out.println("Long Value at index 0: " + value0);
  
            // Reads the Long at the index 1 of the Longbuffer
            // using get() method
            Long value1 = ib.get(1);
  
            // prLong the Long value
            System.out.println("Long Value at index 1: " + value1);
  
            // Reads the Long at the index 2 of the Longbuffer
            // using get() method
            Long value2 = ib.get(2);
  
            // prLong the Long value
            System.out.println("Long 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 LongBuffer:  [8, 9, 6]
Long Value at index 0: 8
Long Value at index 1: 9
Long Value at index 2: 6

Example 2:




// 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 LongBuffer
        int capacity = 3;
  
        // Creating the LongBuffer
        try {
  
            // creating object of Longbuffer
            // and allocating size capacity
            LongBuffer ib = LongBuffer.allocate(capacity);
  
            // putting the value in Longbuffer
            ib.put(6);
            ib.put(8);
            ib.put(12);
  
            // prLong the LongBuffer
            System.out.println("Original LongBuffer:  "
                               + Arrays.toString(ib.array()));
  
            // Reads the Long at the index 0 of the Longbuffer
            // using get() method
            Long value0 = ib.get(0);
  
            // prLong the Long value
            System.out.println("Long Value at index 0: " + value0);
  
            // Reads the Long at the index 1 of the Longbuffer
            // using get() method
            Long value1 = ib.get(1);
  
            // prLong the Long value
            System.out.println("Long Value at index 1: " + value1);
  
            // Reads the Long at the index 2 of the Longbuffer
            // using get() method
            System.out.println("Trying to get the Long"
                               + " of index greater than its limit ");
            Long value2 = ib.get(4);
  
            // prLong the Long value
            System.out.println("Long 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 LongBuffer:  [6, 8, 12]
Long Value at index 0: 6
Long Value at index 1: 8
Trying to get the Long of index greater than its limit 
Exception thrown: java.lang.IndexOutOfBoundsException



My Personal Notes arrow_drop_up
Recommended Articles
Page :