Short

Primitive Data Types

Short

🙋 Need help? Ask an expert now!

The short primitive datatype is another less commonly used integer-related datatype due to its limited range of -32,768 to 32,767. Let's glance at the example below showing the declaration and use of short.

ShortDataTypeExample.java
package exlcode;

public class ShortDataTypeExample {

  // short can be any integer between -32768 and 32767
  public static short exampleVariableOne = 100;
  public static short exampleVariableTwo = -100;
  public static short exampleVariableThree = 0;

  public static void main(String[] args) {
    System.out.println(exampleVariableOne);
    System.out.println(exampleVariableTwo);
    System.out.println(exampleVariableThree);
  }
}

short functions similarly to int as it can only hold whole, non-decimal numbers. This datatype was commonly used when 16-bit computers were used, and are rarely used now as int has pretty much taken its place. However, short takes up about half the space of an int, so in a specific case when you are dealing with a large list of whole numbers, you may chose to use short to save space. With that being said, we will just use int for most of our programs.

Edit Me on GitHub!

Application Question

Which one of these is a valid short declaration?