java.lang.Short toUnsignedInt(short x)
Description
The toUnsignedInt(short x) method of Short class converts the argument to an int by an unsigned conversion. In an unsigned conversion to an int, the high-order 16 bits of the int are zero and the low-order 16 bits are equal to the bits of the short argument. Consequently, zero and positive short values are mapped to a numerically equal int value and negative short values are mapped to an int value equal to the input plus 216.
Make a note that the toUnsignedInt(short x) method of Short class is static thus it should be accessed statically. Mean to say the we would be calling this method in this format:
Short.toUnsignedInt(short x)
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 toUnsignedInt(short x) method non statically.
Method Syntax
public static int toUnsignedInt(short x)
Method Argument
Data Type | Parameter | Description |
---|---|---|
short | x | the value to convert to an unsigned int |
Method Returns
The toUnsignedInt(short x) method of Short class returns the value to convert to an unsigned int
Compatibility
Requires Java 1.8 and up
Java Short toUnsignedInt(short x) Example
Below is a simple java example on the usage of toUnsignedInt(short x) method of Short class.
package com.javatutorialhq.java.examples; import java.util.Scanner; /* * This example source code demonstrates the use of * toUnsignedInt(short x) method of Short class */ public class ShortToUnsignedIntExample { public static void main(String[] args) { // Ask for user input System.out.print("Enter a value:"); // declare a scanner object to read the user input Scanner s = new Scanner(System.in); // assign the input to a variable short value = s.nextShort(); // get the result of toUnsignedInt method int result = Short.toUnsignedInt(value); // print the result System.out.println("Result:" + result); // close the scanner object s.close(); } }