- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - Character reverseBytes() Method
The Java Character reverseBytes() method accepts a char value as its argument, converts it into its corresponding byte value and then reverses these bytes.
Byte reversal in Java is performed by simply swapping the order of bytes in which they were inputted to the memory block. When this is done, the values are reordered but not modified.
This method belongs to the Character class in the java.lang package and is a static method. All static methods in Java must always be invoked or called on the class or its object. Otherwise, a compile time error is raised.
Syntax
Following is the syntax for Java Character reverseBytes() method
public static char reverseBytes(char ch)
Parameters
ch − the character to be tested
Return Value
This method returns the value obtained by reversing (or swapping) the bytes in the specified char value.
Example
The following example shows the usage of Java Character reverseBytes() method.
package com.tutorialspoint; import java.lang.*; public class CharacterDemo { public static void main(String[] args) { // create 2 char primitives ch1, ch2 char ch1, ch2; // assign value to ch1 ch1 = '\u4d00'; // assign result of reverseBytes on ch1 to ch2 ch2 = Character.reverseBytes(ch1); String str = "Reversing bytes on ch1 gives " + ch2; // print ch2 value System.out.println( str ); } }
Output
Let us compile and run the above program, this will produce the following result −
Reversing bytes on ch1 gives M
Example
In this example let us pass an alphabet as an argument to the method.
import java.lang.*; public class CharacterDemo { public static void main(String[] args) { char ch1, ch2; ch1 = 'a'; ch2 = Character.reverseBytes(ch1); System.out.println("Reversing bytes on ch1 gives " + ch2); } }
Output
Let us compile and run the program given above, displaying the output as follows −
Reversing bytes on ch1 gives ?
Example
For digit characters, the method reverses their bytes as follows −
import java.lang.*; public class CharacterDemo { public static void main(String[] args) { char ch1, ch2; ch1 = '9'; ch2 = Character.reverseBytes(ch1); System.out.println("Reversing bytes on ch1 = 9 gives " + ch2); ch1 = '5'; ch2 = Character.reverseBytes(ch1); System.out.println("Reversing bytes on ch1 = 5 gives " + ch2); } }
Output
On compiling and executing the program above, the output is produced as given below −
Reversing bytes on ch1 = 9 gives ? Reversing bytes on ch1 = 5 gives ?
Example
In the following example, the method takes symbol characters as arguments and returns the values obtained after reversing their bytes.
import java.lang.*; public class CharacterDemo { public static void main(String[] args) { char ch1, ch2, ch3, ch4; ch1 = '&'; ch3 = Character.reverseBytes(ch1); ch2 = '+'; ch4 = Character.reverseBytes(ch2); System.out.println("Reversing bytes on ch1 = & gives " + ch3); System.out.println("Reversing bytes on ch2 = + gives " + ch4); } }
Output
Compile and run the example program above to obtain the output as following −
Reversing bytes on ch1 = & gives ? Reversing bytes on ch2 = + gives ?