Signup/Sign In

Java Character reverseBytes() Method

Java reverseBytes() method is a part of Character class. This method returns the values obtained by reversing the order of bytes for the specified character.

Syntax:

public static char reverseBytes(char ch)  

Parameters:

The parameter passed is the char value whose reverse bytes value is to be returned.

Returns:

The value obtained by reversing the order of bytes of the specified character.

Example 1:

Here, the value obtained by reversing the order of bytes of the character is returned.

public class StudyTonight
{  
	public static void main(String[] args)
	{  
		char ch1 = '\u5f02';  
		char ch2 = 'q';  

		char ch11 = Character.reverseBytes(ch1);  
		char ch12 = Character.reverseBytes(ch2);  

		System.out.println("The reverse byte value is: " + ch11 );  
		System.out.println("The reverse byte value is: " + ch12 );  
	}
}


The reverse byte value is: ?
The reverse byte value is: ?

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.

import java.util.Scanner; 
public class StudyTonight
{  
	public static void main(String[] args)
	{  
		try
		{
			System.out.print("Enter the character: ");  
			Scanner sc = new Scanner(System.in);         
			char ch = sc.next().charAt(0);  
			char cc = Character.reverseBytes(ch);
			System.out.println("The value after reversing is "+cc);
		}
		catch(Exception e)
		{
			System.out.println("Invalid Input!!");
		}
	}  
}


Enter the character: w
The value after reversing is ?
**************************************
Enter the character: 1
The value after reversing is ?

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.