Java.lang Package Classes

Java Short - reverseBytes() Method



The java.lang.Short.reverseBytes() method returns the value obtained by reversing the order of the bytes in the two's complement representation of the specified short value.

Syntax

public static short reverseBytes(short i)

Parameters

i Specify the value whose bytes are to be reversed.

Return Value

Returns the value obtained by reversing (or, equivalently, swapping) the bytes in the specified short value.

Exception

NA.

Example:

In the example below, the java.lang.Short.reverseBytes() method is used to reverse the order of the bytes in the given short value.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    
    //creating short values
    short x = 5;
    short y = 25;
    short z = -25;

    //creating reverseBytes of the short values
    short p = Short.reverseBytes(x);
    short q = Short.reverseBytes(y);
    short r = Short.reverseBytes(z);

    //printing reverseBytes of the short values
    System.out.println("reverseBytes of x is: " + p); 
    System.out.println("reverseBytes of y is: " + q);   
    System.out.println("reverseBytes of z is: " + r); 
  }
}

The output of the above code will be:

reverseBytes of x is: 1280
reverseBytes of y is: 6400
reverseBytes of z is: -6145

❮ Java.lang - Short