Short class in Java

In Java, there is a concept called Wrapper Classes. Short is from wrapper class. There are some methods with the help of which we can operate with tasks. If we talk about an object then the object of the short class can hold only one short value.

The syntax for assigning short value:
short short_variable_name = value;

Algorithm:

  1. Create a class.
  2. create a main method inside the same class.
  3. Initialize the short variable and assign some short value to it.
  4. Create an object for that short variable.
  5. Apply methods that are there in a short class.

Methods in short class:

  • toString() – This method returns String representation of short value.
  • compare() – This method compares two short values.
  • compareUnsigned() – This method compares two unsigned short values.
  • equals() – This method compares that given short objects are similar or dissimilar.
  • floatValue() – This method returns float representation of short wrapper class.
  • intValue() – This method will represent int value for given short value.
  • shortValue() – As we know Short is a wrapper class so this method will return a short value which is a primitive value for a given Short object.

create a class and main method.

public class ShortClass{
    public static void main(String[] args){
    }
}

assign short value and create its object.

public static void main(String[] args){
    short num=90;
    Short p = new Short(num);
}

Now, apply the method.

public static void main(String[] args){
    short num=90;
    String val = "45";
    Short p = new Short(num);
    Short q = new Short(val);
    System.out.println("String value for num:"+Short.toString(num)); // toString() method
}

That’s how we can apply as many methods as we want.

That’s all for this tutorial. Thank You.

Leave a Reply

Your email address will not be published. Required fields are marked *