java.lang.Character reverseBytes(char ch)

Description

The Character.reverseBytes(char ch) java method returns the value obtained by reversing the order of the bytes in the specified char value.

The reverseBytes(char ch) method of Character class is static thus it should be accessed statically which means the we would be calling this method in this format:

Character.reverseBytes(char ch)

Non static method is usually called by just declaring method_name(argument) however in this case since the method is static, it should be called by appending the class name as suffix. We will be encountering a compilation problem if we call the java reverseBytes() method non statically.

Method Syntax

public static char reverseBytes(char ch)

Method Argument

Data Type Parameter Description
char ch The char of which to reverse the byte order.

Method Returns

The reverseBytes(char ch) method of Character class returns the value obtained by reversing (or, equivalently, swapping) the bytes in the specified char value.

Compatibility

Requires Java 1.5 and up

Java Character reverseBytes(char ch) Example

Below is a simple java example on the usage of reverseBytes(char ch) method of Character class.

package com.javatutorialhq.java.examples;

import java.util.Scanner;

/*
 * This example source code demonstrates the use of 
 * reverseBytes(char ch) method of Character class.
 */

public class CharacterReverseBytesExample {

	public static void main(String[] args) {

		// Ask for user input
		System.out.print("Enter an input:");

		// use scanner to get the user input
		Scanner s = new Scanner(System.in);

		// gets the user input
		char[] value = s.nextLine().toCharArray();

		// close the scanner object
		s.close();

		/*
		 * get the codepoint of the result of
		 * reversing the bytes of character input
		 */
		for (char ch : value) {
			char result = Character.reverseBytes(ch);
			// print the result
			System.out.println("character " + ch 
					+ " reversing it's bytes is having codepoint " 
					+ (int)result);
		}

	}

}

Sample Output

Below is the sample output when you run the above example.

java Character reverseBytes(char ch) example output