Java - Short valueOf(short s) method
Description
The Java Short valueOf(short s) method returns a Short instance representing the specified short value i.
Declaration
Following is the declaration for java.lang.Short.valueOf() method
public static Short valueOf(short s)
Parameters
i − This is an short value.
Return Value
This method returns a Short instance representing i.
Exception
NA
Getting Short Object from a Positive short Value Example
The following example shows the usage of Short valueOf(short s) method to get the Short object using the specified short value. We've created a int variable and assigned it a positive short value. Then using valueOf() method, we're getting the object and printing it.
package com.tutorialspoint;
public class ShortDemo {
public static void main(String[] args) {
short s = 170;
System.out.println("Number = " + s);
/* returns the Short object of the given number */
System.out.println("valueOf = " + Short.valueOf(s));
}
}
Output
Let us compile and run the above program, this will produce the following result −
Number = 170 valueOf = 170
Getting Short Object from a Negative short Value Example
The following example shows the usage of Short valueOf(short s) method to get the Short object using the specified short value. We've created a int variable and assigned it a negative short value. Then using valueOf() method, we're getting the object and printing it.
package com.tutorialspoint;
public class ShortDemo {
public static void main(String[] args) {
short s = -170;
System.out.println("Number = " + s);
/* returns the Short object of the given number */
System.out.println("valueOf = " + Short.valueOf(s));
}
}
Output
Let us compile and run the above program, this will produce the following result −
Number = -170 valueOf = -170
Getting Short Object from a Zero short Value Example
The following example shows the usage of Short valueOf(short s) method to get the Short object using the specified short value. We've created a int variable and assigned it a zero value. Then using valueOf() method, we're getting the object and printing it.
package com.tutorialspoint;
public class ShortDemo {
public static void main(String[] args) {
short s = 0;
System.out.println("Number = " + s);
/* returns the Short object of the given number */
System.out.println("valueOf = " + Short.valueOf(s));
}
}
Output
Let us compile and run the above program, this will produce the following result −
Number = 0 valueOf = 0