Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Definition
3.
Declaration
4.
Field
5.
Constructor
6.
Methods
6.1.
byteValue()
6.1.1.
Syntax
6.1.2.
Return value
6.1.3.
Example
6.1.4.
Output
6.2.
Compare()
6.2.1.
Syntax
6.2.2.
Return Value
6.2.3.
Example
6.2.4.
Output
6.3.
compareTo()
6.3.1.
Syntax
6.3.2.
Return Value
6.3.3.
Example
6.3.4.
Output
6.4.
compareUnsigned()
6.4.1.
Syntax
6.4.2.
Return Value
6.4.3.
Example
6.4.4.
Output
6.5.
decode()
6.5.1.
Syntax
6.5.2.
Return Value
6.5.3.
Example
6.5.4.
Output
6.6.
parseShort()
6.6.1.
Syntax
6.6.2.
Return Value
6.6.3.
Example
6.6.4.
Output
6.7.
valueOf()
6.7.1.
Syntax
6.7.2.
Return Value
6.7.3.
Example
6.7.4.
Output
6.8.
equals()
6.8.1.
Syntax
6.8.2.
Return Value
6.8.3.
Example
6.8.4.
Output
7.
FAQs
8.
Key Takeaways
Last Updated: Mar 27, 2024

Java Short Class

Author Aniket Majhi
0 upvote
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

Welcome readers!

If you have a basic knowledge about wrapper classes in Java, then this blog will be useful for you to learn one of the wrapper classes available in Java, namely the Short Class. You will learn about short class and their different properties with proper examples and code implementations through this blog.

So without further ado, let’s start the blog.

Definition

The Java short class is a wrapper class used to wrap the value of the primitive short data type to an object data type. The object only contains a single field of type short. It contains several methods to deal with the short values.

Also see,  Swap Function in Java

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Declaration

The declaration of the short class is as follows:

public final class Short extends Number implements Comparable<Short>

Field

The fields for the java short class are defined below:

  1. Short MAX_VALUE:  This is a constant that is holding the maximum value of the short can have, 215 - 1
  2. Short MIN_VALUE: This is a constant that is holding the minimum value of the short can have, -215
  3. class Type: This is the class instance representing the primitive type short.
  4. int SIZE: This is the number of bits used to represent the short value in the 2’s complement binary form.
  5. int BYTES: This is the number of bits used to represent the short value in the 2’s complement binary form.

Constructor

The Java short class provides mainly two types of constructors, these are as follows:

  • Short(short param): It accepts a parameter of type short and constructs a newly allocated Short object that represents the specified short value.
short a = 45;
Short x = new Short(a);
  • Short(String s): It accepts a parameter of type String and converts that String into Short class object.
String b = "45";
Short y = new Short(b);

Methods

Some of the most useful Java short class methods are as follows:

byteValue()

The method is described below:

Syntax

byte byteValue()

Return value

It returns the value of the short as a byte.

Example

public class ShortByteValue {
    public static void main(String[] args) {
        Short a = 10;
        byte b = a.byteValue();
        System.out.println("Byte b = " + b);
    }
}

Output

Try it on java online compiler and Hashcode Method in Java

Compare()

The method is described below:

It basically compares two shorts values numerically.

Syntax

public static int compare(short x , short y)

Return Value

If both the short values are equal then it returns 0.

If the first short value is greater than the second one, it returns a positive integer.

If the first short value is less than the second short value, it returns a negative integer.

Example

public class ShortCompare{
    public static void main(String[] args) {
   
        short a = 10;
        short b = 10;   // a == b
        short c = 5;    // a > c
        short d = 15;  // a < d

        Short aa = new Short(a);
        Short bb = new Short(b);
        Short cc = new Short(c);
        Short dd = new Short(d);

        int result1 = Short.compare(aa , bb);
        int result2 = Short.compare(aa , cc);
        int result3 = Short.compare(aa , dd);

        System.out.println("Compared result for a and b is: " + result1);
        System.out.println("Compared result for a and c is: " + result2);
        System.out.println("Compared result for a and d is: " + result3);
    }
}

Output

compareTo()

The method is described below:
It compares a Short object to another Short object numerically.

Syntax

public int compareTo(Short x)  

Return Value

If both the short values are equal then it returns 0.

If the short value is numerically greater than to the argument short, it returns a positive integer.

If the short value is numerically less than the argument short value, it returns a negative integer.

Example

public class ShortCompareTo {
    public static void main(String[] args) {
        Short s1 = 5;
        Short s2 = 5;   // s1 == s2
        Short s3 = 10; // s1 < s3
        Short s4 = 3; // s1 > s4

        int res1 = s1.compareTo(s2);
        int res2 = s1.compareTo(s3);
        int res3 = s1.compareTo(s4);

        System.out.println("CompareTo result for s1 and s2: " + res1);
        System.out.println("CompareTo result for s1 and s3: " + res2);
        System.out.println("CompareTo result for s1 and s4: " + res3);
    }
}

Output

compareUnsigned()

The method is discussed below:

It is used to compare the two short values numerically where it treats every short value as unsigned.

Syntax

public static int compareUnsigned(short x, short y)  

Return Value

If both the unsigned short values are equal then it returns 1.

If the first unsigned short value is greater than the second one, it returns a positive integer.

If the first unsigned short value is less than the second short value, it returns a negative integer.

Example

public class ShortCompareUnsigned {
    public static void main(String[] args) {
        Short a = 10;
        Short b = 10; //a == b
        Short c = 20;  // a < c
        Short d = 5;  // a > d

        int res1 = Short.compareUnsigned(a , b);
        int res2 = Short.compareUnsigned(a , c);
        int res3 = Short.compareUnsigned(a , d);

        System.out.println("CompareUnsigend Result for a and b is: " + res1);
        System.out.println("CompareUnsigned Result for a and c is: " + res2);
        System.out.println("CompareUnsigned Result for a and d is: " + res3);
    }
}

Output

decode()

The method is discussed below.

It decodes a String into Short.

Syntax

public static Short decode(String a) throws NumberFormatException  

Return Value

It returns a short object that holds short values represented by the string argument.

Example

public class ShortDecode {
    public static void main(String[] args) {
        String val = "10";
        Short result = Short.decode(val);
        System.out.println("The decoded string value is: " + result);
    }
}

Output

parseShort()

The method is discussed below.

It returns a primitive byte value contained in the string argument str.

Syntax

static short parseShort(String x)throws NumberFormatException  

Return Value

This method returns a short value representing the string x.

Example

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

        String x = "10";
        Short res = Short.parseShort(x);
        System.out.println("The Short value representing the string x is : " + res);
    }
}

Output

valueOf()

The method is discussed below.

It basically converts the string s(containing short value) into Short class object and returns that Short object. 

Syntax

public static Short valueOf(short s) throws NumberFormatException
public static Short valueOf(String s) throws NumberFormatException
public static Short valueOf(String s, int radix) throws NumberFormatException

Return Value

It returns a short object that holds the value represented by the string, or short values.

Example

public class ValueOf {
    public static void main(String[] args) {
        Short  val1 = 10;
        String val2 = "1000";

        Short res1 = Short.valueOf(val1);
        Short res2 = Short.valueOf(val2);
        Short res3 = Short.valueOf(val2 , 2);
        System.out.println("The short object representing the short value is: " + res1);
        System.out.println("The short object representing the : " + res2);
        System.out.println("The value of the short object is: " + res3);
    }
}

Output

equals()

The method is discussed below.

It compares the object with the specified object.

Syntax

boolean equals(Object obj)

Return Value

It returns true if both the objects are the same otherwise, it returns false.

Example

public class Equals {
    public static void main(String[] args) {
        Short a = 5;
        Short b = 5; // a == b
        Short c = 10; // a != c
        boolean res1 = a.equals(b);
        boolean res2 = a.equals(c);

        System.out.println("Equals result for a and b: " + res1);
        System.out.println("Equals result for a and c: " + res2);
    }
}

Output

Also see, Duck Number in Java

FAQs

  1. What is a short class in Java?
    The java short class is a wrapper class used to wrap the value of the primitive short data type to an object data type.
     
  2. What is a wrapper class in Java?
    A wrapper class in Java is a class that is used to convert a primitive data type to an object data type.
     
  3. Is String a wrapper class in Java?
    No String is not a wrapper class in Java.
     
  4. Is boolean a wrapper class in Java?
    Yes, a wrapper class in Java named Boolean wraps the primitive data type boolean.

Key Takeaways

In this article, we have extensively discussed the Java Short class and their implementation.

We started with the basic concept of the wrapper classes, then we moved to the short class, its basic definition, different fieldstypes of constructors and different methods using examples.

We hope that this blog has helped you enhance your knowledge regarding Java Short and if you would like to learn more, check out our articles on Java Float ClassJava Double Class. Do upvote our blog to help other ninjas grow.

Recommended Readings:

Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!

Happy Reading!

Live masterclass