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 ?
java_lang_character.htm
Advertisements