Java short

Java short – In Java, the short keyword is used to define a variable that holds a 16‐bit signed integer value. This data type is particularly useful when you want to save memory in large arrays where the memory savings are critical.

The short data type is one of Java’s eight primitive data types. It is used to store integer values in a smaller range compared to int, which makes it suitable for situations where you are working with a limited range of numbers.

This tutorial covers various aspects of the Java short data type, including:

  1. The type of values a short can store and its range.
  2. How to declare, initialize, and update a short variable.
  3. Maximum and minimum values a short data type can hold.
  4. Printing a short value to the console output.
  5. Performing arithmetic operations on short values.
  6. Converting between short and other primitive data types.
  7. Using the Short class for additional operations.

Each section includes detailed descriptions and examples to help you master operations with the short data type in Java.

1 Number of Bytes for a Short

A short in Java occupies 2 bytes (16 bits) of memory.

2 Range of a Short in Java

Being a 16-bit signed integer, a short can store values from -32,768 to 32,767.

3 Declare Variable of Type Short

To declare a short variable, use the following syntax:

</>
Copy
short variable_name;

For example, to declare a short variable named s:

</>
Copy
short s;

4 Initialize Variable with Short Value

You can initialize a short variable by assigning it a value. For example, to declare and initialize s with the value 100:

</>
Copy
short s = 100;

5 Update a Short Variable

To update an existing short variable, assign it a new value. For example, if s is initially 150 and you want to update it to 200:

</>
Copy
short s = 150;
s = 200;

6 Default Value of a Static Short Variable

Static short variables are automatically initialized to 0 if no explicit value is provided. For example:

</>
Copy
public class Example {
    static short s;
    public static void main(String[] args) {
        System.out.println(s);
    }
}

Output:

0

7 Print Short to Console

To print a short value to the console, use the System.out.println() method. For example:

</>
Copy
public class Example {
    public static void main(String[] args) {
        short s = 12345;
        System.out.println(s);
    }
}

Output:

12345

8 Always Initialize Local Short Variables

Local variables must be explicitly initialized before use, otherwise the compiler will throw an error. Consider this example:

</>
Copy
public class Example {
    public static void main(String[] args) {
        short s;
        System.out.println(s);
    }
}

Output (Compilation Error):

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The local variable s may not have been initialized
    at Example.main(Example.java:4)

9 Short Maximum Value

You can retrieve the maximum value a short can hold by using the Short.MAX_VALUE constant. For example:

</>
Copy
public class Example {
    public static void main(String[] args) {
        short s = Short.MAX_VALUE;
        System.out.println(s);
    }
}

Output:

32767

10 Short Minimum Value

Similarly, use the Short.MIN_VALUE constant to retrieve the minimum value a short can hold:

</>
Copy
public class Example {
    public static void main(String[] args) {
        short s = Short.MIN_VALUE;
        System.out.println(s);
    }
}

Output:

-32768

11 Java short – Arithmetic Operations

You can perform various arithmetic operations on short values. For example, consider the following operations on two short variables:

</>
Copy
public class Example {
    public static void main(String[] args) {
        short a = 10;
        short b = 3;
        System.out.println("a + b = " + (a + b));
        System.out.println("a - b = " + (a - b));
        System.out.println("a * b = " + (a * b));
        System.out.println("a / b = " + (a / b));
        System.out.println("a % b = " + (a % b));
    }
}

Output:

a + b = 13
a - b = 7
a * b = 30
a / b = 3
a % b = 1

12 Converting Short to Other Primitive Datatypes

You can convert a short value to other primitive data types using typecasting. For example:

</>
Copy
public class Example {
    public static void main(String[] args) {
        short s = 25;
        int i = (int) s;
        long l = (long) s;
        float f = (float) s;
        double d = (double) s;
        char c = (char) s;
    }
}

13 Converting Other Primitive Datatypes to Java short

You can also convert other primitive types to a short using typecasting. Keep in mind that converting from a larger data type may result in data loss:

</>
Copy
public class Example {
    public static void main(String[] args) {
        int i = 300;
        short s = (short) i;
        
        long l = 1000L;
        s = (short) l;
        
        float f = 12.34f;
        s = (short) f;
        
        double d = 56.78;
        s = (short) d;
    }
}

14 Java Short Class

The Short class, part of the java.lang package, wraps a primitive short in an object. It provides useful constants and methods for manipulating short values, such as parsing strings and comparing short values.

Conclusion

In this Java Tutorial, we explored the short data type. We learned how to declare, initialize, and update a short variable; accessed its maximum and minimum values; printed it to the console; performed arithmetic operations; converted between different primitive data types; and reviewed the use of the Short class. These fundamental concepts are essential for effectively working with smaller integer values in Java.