Categories: Java

Short class in Java

The Short class is a wrapper class for the primitive type short. It contains several methods for dealing with short values effectively, such as converting them to string representations and vice versa.

Constructors:

To initialize a Short object, there are primarily two constructors-

public Short(short b)
public Short(String s)throws NumberFormatException
Methods of Short wrapper class:
MethodsDescription
int compareTo(Long b)-If the invoked Short object has the same value as b, it returns zero.
-Returns the negative difference if the Short object invoked has a value less than b.
-If the invoked Short object has a value greater than b, this method returns the positive difference.
boolean equals(Object ob)Returns true if invoked Short object has the same value as referred by ob, else false.
static Short parseShort(String s)If String, s can be converted to a valid short value, it returns a primitive short value.
static Short valueOf(short b)Returns a Short object after converting it from a primitive short value, b.
static Short valueOf(String s)After converting a String, s, to a Short object, this method returns it.
short shortValue()Returns a primitive short value after converting a Short object to it.
byte byteValue()Returns a primitive byte value after converting a Short object to it.
int intValue()Returns a primitive int value after converting a Short object to it.
long longValue()After converting a Short object to it, this function returns a primitive int value.
float floatValue()Returns a primitive float value after converting a Short object to it.
double doubleValue()Returns a primitive double value after converting a Short object to it.
Example: program to illustrate various methods of Short class
public class Coderz {  
      
    public static void main(String[] args) {  
        Short s1 = 234 ;  
        Short s2 = 32 ;  
        Short s3=12;  
       // It compares two Short objects numerically  
        int val = s1.compareTo(s2);  
        if (val>0) {  
            System.out.println(s1 + " is greater than " + s2);  
        }  
        else{  
            System.out.println(s2 + " is greater than " + s1);  
        }  
        //It is used to check whether both short values are equal or not.  
        Boolean b1 = s3.equals(s2);  
        if (b1) {  
            System.out.println(s2 + " and " + s3 +" are equal.");  
        }  
        else{  
            System.out.println(s1 + " and " + s2 +" are not equal.");  
        }  
        //returns the value of this Short as a long  
        Long f3 = s2.longValue();  
        System.out.println("Long value of "+s2+ " is : "+f3);  
        //Returns a string representation of the Short object  
        String f4 = s2.toString();  
        System.out.println("String value of "+s2+ " is : "+f4);  
       //It returns a double value for this Short object  
        Double f5 = s1.doubleValue();  
        System.out.println("Double value of "+s1+ " is : "+f5);  
    }  
}  
Output:
234 is greater than 32
234 and 32 are not equal.
Long value of 32 is : 32
String value of 32 is : 32
Double value of 234 is : 234.0
Exception when converting a String to Short:

class Coderz
{
   public static void main(String []args)
   {
     Short b1 = new Short("12g"); //Passing a String value that cannot be                 converted to a short value.

     System.out.println("Primitive short value of '12g' : "+ b1);
   }
}
Output:
Exception in thread "main" java.lang.NumberFormatException: For input string: "12g"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
 at java.base/java.lang.Integer.parseInt(Integer.java:652)
 at java.base/java.lang.Short.parseShort(Short.java:120)
at java.base/java.lang.Short.<init>(Short.java:336)
 at Coderz.main(Coderz.java:6)

A NumberFormatException is thrown when we pass a String value to the constructor of the Short class or its method parseByte(), which cannot be converted to a Byte object.

Note: also read about the Wrapper Class in Java

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago