Open In App

CharBuffer order() methods in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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: 

Java




// 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();
 
        // Retrieve 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);
    }
}


Output: 

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

 

Examples 2: 

Java




// 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);
 
        // Retrieve 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);
    }
}


Output: 

CharBuffer is : [,,,  ]
Order: LITTLE_ENDIAN

 

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



Last Updated : 20 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads