ByteBuffer getChar() method in Java with Examples

getChar()

The getChar() method of java.nio.ByteBuffer class is used to get method for reading a char value

Reads the next two bytes at this buffer’s current position, composing them into a char value according to the current byte order, and then increments the position by two.

Syntax:

public abstract char getChar()

Return Value: This method returns the char 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 getChar() method:

Examples 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// getChar() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the ByteBuffer
        int capacity = 50;
  
        // Creating the ByteBuffer
        try {
  
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity);
  
            // putting the string in the bytebuffer
            bb.asCharBuffer().put("Geeks");
  
            // rewind the Bytebuffer
            bb.rewind();
  
            // Declaring the variable
            char c;
  
            // print the ByteBuffer
            System.out.println("Original ByteBuffer: ");
            while ((c = bb.getChar()) != 0)
                System.out.print(c + " ");
  
            // rewind the Bytebuffer
            bb.rewind();
  
            // Reads the char at this buffer's current position
            // using getChar() method
            char value = bb.getChar();
  
            // print the char value
            System.out.println("\n\nByte Value: " + value);
  
            // Reads the  char at this buffer's next position
            // using getChar() method
            char value1 = bb.getChar();
  
            // print the char value
            System.out.print("\nNext Byte Value: " + value1);
        }
  
        catch (IllegalArgumentException e) {
  
            System.out.println("\nException Thrown: " + e);
        }
  
        catch (ReadOnlyBufferException e) {
  
            System.out.println("\nException Thrown: " + e);
        }
  
        catch (BufferUnderflowException e) {
  
            System.out.println("\nException Thrown: " + e);
        }
    }
}

chevron_right


Output:

Original ByteBuffer: 
G e e k s 

Byte Value: G

Next Byte Value: e

Examples 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// getChar() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the ByteBuffer
        int capacity = 8;
  
        // Creating the ByteBuffer
        try {
  
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity);
  
            // putting the string in the bytebuffer
            bb.asCharBuffer().put("abc");
  
            // rewind the Bytebuffer
            bb.rewind();
  
            // Declaring the variable
            char c;
  
            // print the ByteBuffer
            System.out.print("Original ByteBuffer: ");
            while ((c = bb.getChar()) != 0)
                System.out.print(c + " ");
  
            // rewind the Bytebuffer
            bb.rewind();
  
            // Reads the char at this buffer's current position
            // using getChar() method
            char value = bb.getChar();
  
            // print the char value
            System.out.println("\n\nFirst char Value: " + value);
  
            // Reads the  char at this buffer's next position
            // using getChar() method
            char value1 = bb.getChar();
  
            // print the char value
            System.out.println("\nSecond char Value: " + value1);
  
            // Reads the  char at this buffer's next position
            // using getChar() method
            char value2 = bb.getChar();
  
            // print the char value
            System.out.println("\nThird char Value: " + value2);
  
            // Reads the  char at this buffer's next position
            // using getChar() method
            System.out.print("\nsince the buffer current postion is incremented");
            System.out.print(" to greater than its limit ");
            char value3 = bb.getChar();
            char value4 = bb.getChar();
        }
  
        catch (BufferOverflowException e) {
  
            System.out.println("\nException Thrown: " + e);
        }
  
        catch (ReadOnlyBufferException e) {
  
            System.out.println("\nException Thrown: " + e);
        }
  
        catch (BufferUnderflowException e) {
  
            System.out.println("\nException Thrown: " + e);
        }
    }
}

chevron_right


Output:

Original ByteBuffer: a b c 

First char Value: a

Second char Value: b

Third char Value: c

since the buffer current postion is incremented to greater than its limit 
Exception Thrown: java.nio.BufferUnderflowException
get(int index)

The get(int index) method of ByteBuffer is used to reads two bytes at the given index, composing them into a char value according to the current byte order.

Syntax:

public abstract char getChar(int index)

Parameters: This method takes index (The index from which the Byte 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// getChar() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the ByteBuffer
        int capacity = 50;
  
        // Creating the ByteBuffer
        try {
  
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity);
  
            // putting the string in the bytebuffer
            bb.asCharBuffer().put("abc");
  
            // rewind the Bytebuffer
            bb.rewind();
  
            // Declaring the variable
            char c;
  
            // print the ByteBuffer
            System.out.print("Original ByteBuffer: ");
            while ((c = bb.getChar()) != 0)
                System.out.print(c + " ");
  
            // rewind the Bytebuffer
            bb.rewind();
  
            // Reads the char at this buffer's at index 0
            // using getChar() method
            char value0 = bb.getChar(0);
  
            // print the char value
            System.out.println("\n\nchar Value at index 0: "
                               + value0);
  
            // Reads the  char at this buffer's at index 2
            // using getChar() method
            char value1 = bb.getChar(2);
  
            // print the char value
            System.out.println("\nchar Value at index 2: "
                               + value1);
  
            // Reads the  char at this buffer's at index 4
            // using getChar() method
            char value2 = bb.getChar(4);
  
            // print the char value
            System.out.println("\nchar Value at index 4: "
                               + value2);
        }
  
        catch (IllegalArgumentException e) {
  
            System.out.println("\nException Thrown: " + e);
        }
  
        catch (ReadOnlyBufferException e) {
  
            System.out.println("\nException Thrown: " + e);
        }
  
        catch (BufferUnderflowException e) {
  
            System.out.println("\nException Thrown: " + e);
        }
    }
}

chevron_right


Output:

Original ByteBuffer: a b c 

char Value at index 0: a

char Value at index 2: b

char Value at index 4: c

Examples 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// getChar() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the ByteBuffer
        int capacity = 50;
  
        // Creating the ByteBuffer
        try {
  
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity);
  
            // putting the string in the bytebuffer
            bb.asCharBuffer().put("abc");
  
            // rewind the Bytebuffer
            bb.rewind();
  
            // Declaring the variable
            char c;
  
            // print the ByteBuffer
            System.out.print("Original ByteBuffer: ");
            while ((c = bb.getChar()) != 0)
                System.out.print(c + " ");
  
            // rewind the Bytebuffer
            bb.rewind();
  
            // Reads the char at this buffer's at index 0
            // using getChar() method
            char value0 = bb.getChar(0);
  
            // print the char value
            System.out.println("\n\nchar Value at index 0: "
                               + value0);
  
            // Reads the  char at this buffer's at index 2
            // using getChar() method
            char value1 = bb.getChar(2);
  
            // print the char value
            System.out.println("\nchar Value at index 2: "
                               + value1);
  
            // Reads the  char at this buffer's at index 4
            // using getChar() method
            System.out.println("\nTrying to get the char"
                               + " at negative index ");
            char value2 = bb.getChar(-4);
        }
  
        catch (IndexOutOfBoundsException e) {
  
            System.out.println("\nException Thrown: "
                               + e);
        }
  
        catch (ReadOnlyBufferException e) {
  
            System.out.println("\nException Thrown: "
                               + e);
        }
  
        catch (BufferUnderflowException e) {
  
            System.out.println("\nException Thrown: "
                               + e);
        }
    }
}

chevron_right


Output:

Original ByteBuffer: a b c 

char Value at index 0: a

char Value at index 2: b

Trying to get the char at negative index 

Exception Thrown: java.lang.IndexOutOfBoundsException

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.