CharBuffer order() methods in Java with Examples

The order() method of java.nio.CharBuffer class is used to retrieve this buffer’s byte order. The byte order of a char buffer created by allocation or by wrapping an existing char array is the native order of the underlying hardware. The byte order of a char buffer created as a view of a byte buffer is that of the byte buffer at the moment that the view is created.

Syntax:

public abstract ByteOrder order()

Return Value: This method returns this buffer’s byte order.



Below are the examples to illustrate the order() method:

Examples 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// order() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // creating object of CharBuffer
        // and allocating size capacity
        CharBuffer cb
            = CharBuffer.allocate(4);
  
        // append the int value in the charbuffer
        cb.append('a')
            .append('b')
            .append('c')
            .append('d');
  
        // rewind the Bytebuffer
        cb.rewind();
  
        // Retrive the ByteOrder
        // using order() method
        ByteOrder order = cb.order();
  
        // print the char buffer and order
        System.out.println("CharBuffer is : "
                           + Arrays.toString(cb.array())
                           + "\nOrder: " + order);
    }
}

chevron_right


Output:

CharBuffer is : [a, b, c, d]
Order: LITTLE_ENDIAN

Examples 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// order() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // creating object of CharBuffer
        // and allocating size capacity
        CharBuffer cb = CharBuffer.allocate(4);
  
        // Retrive the ByteOrder
        // using order() method
        ByteOrder order = cb.order();
  
        // print the char buffer and order
        System.out.println("CharBuffer is : "
                           + Arrays.toString(cb.array())
                           + "\nOrder: " + order);
    }
}

chevron_right


Output:

CharBuffer is : [,,,  ]
Order: LITTLE_ENDIAN

Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/CharBuffer.html#order–



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.