
- 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 - Short reverseBytes() Method
The Java Short reverseBytes() method is used to retrieve the primitive short value that is obtained by reversing the order of bytes in binary 2's complement form of the given short value.
The reverseBytes() method is static, which means that the class name should be added as a suffix when calling this method. If we will invoke this method non-statically, we will encounter a compilation error. Non-static methods is generally invoked by simply declaring method_name(argument).
Syntax
Following is the syntax for Java Short parseShort() method −
public static short reverseBytes(short i)
Parameters
i − This is the short value.
Return Value
This method returns the value obtained by reversing the bytes in the specified short value.
Example
We can pass a positive value using reverseBytes() method. The result will be the reverse byte value of the given short value.
The following example shows the usage of Java Short reverseBytes() method. Here, we are assigning a positive value to the short variable ‘shortNum’. Then displaying the short value after reversing the order of the bytes.import java.lang.*; public class ShortDemo { public static void main(String[] args) { // assign value to short short shortNum = 50; /* returns the value after reversing the order of the bytes in the binary representation of the specified short value. */ short shortValue = Short.reverseBytes(shortNum); // displaying the reversed value System.out.println("The reversed value is = " + shortValue); } }
Output
Let us compile and run the above program, this will produce the following result −
The reversed value is = 12800
Example
We can also pass a negative value using reverseBytes() method. The result will be the reverse byte value of the given short value.
import java.lang.*; public class ShortDemo { public static void main(String[] args) { // assign value to short short shortNum = -87; /* * returns the value after reversing the order of the bytes * in the binary representation of the specified short value. */ short shortValue = Short.reverseBytes(shortNum); // displaying the reversed value System.out.println("The reversed value is = " + shortValue); } }
Output
Following is an output of the above code −
The reversed value is = -22017
Example
We can also pass MAX_VALUE as an argument using this method, it returns the value -129.
In the following example, a short variable ‘s’ is created. MAX_VALUE is assigned as the value to this variable. Then the reverseBytes of the given short value is returned −
import java.lang.*; public class ShortDemo { public static void main(String[] args) { // create short value short s = Short.MAX_VALUE; System.out.println("The short MAX_VALUE is: " + s); // create reverseBytes of the given short value short x = Short.reverseBytes(s); // print the reverseBytes of the given short value System.out.println("reverseBytes of s is: " + x); } }
Output
Output of the above code is as follows −
The short MAX_VALUE is: 32767 reverseBytes of s is: -129
Example
If we pass MIN_VALUE as an argument using this method, it returns the value 128.
In the example given below, a short variable ‘s’ is created. MIN_VALUE is assigned as the value to this variable. Then the reverseBytes of the given short value is returned −
import java.lang.*; public class ShortDemo { public static void main(String[] args) { // create short value short s = Short.MIN_VALUE; System.out.println("The short MIN_VALUE is: " + s); // create reverseBytes of the given short value short x = Short.reverseBytes(s); // print the reverseBytes of the given short value System.out.println("reverseBytes of s is: " + x); } }
Output
While executing the above code, we get the following output −
The short MIN_VALUE is: -32768 reverseBytes of s is: 128