Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Java Datatypes → Short in Java

Java Datatypes

Short in Java

What is Short datatype?

The short datatype is a signed 16-bit integer type. This means that it can store values from -32,768 to 32,767. The short datatype is particularly useful when dealing with integer values that fall within a moderate range and conserving memory is still important. it can represent both positive and negative numbers, including zero. Short variables are declared by using the short keyword. For example,
Java short Declarartion
short var1, var2; short a = 10; short b = 10, c = 23; short d = 10 + 23; short e = d + b; short de = 4/2; short f = 234343453; //compilation error short g = 4534.435; //compilation error short h = fd; //compilation error
Short variables are often used when working with data that is stored in a stream, such as a network stream or a file. They are also useful when working with raw binary data that may not be directly compatible with Java's other built-in types. Here is an example of how to use the short datatype to read a short from a network stream:
Java short datatype example
public class Main{ public static void main(String[] args) { // Declare a short variable. short myShort; // Assign a value to the short variable. myShort = 100; // Print the value of the short variable. System.out.println(myShort); // Try to assign a value that is out of range to the short variable. // myShort = 32768; // This will cause a compiler error. } }


In this example, the inputStream variable is an object of the InputStream class. The read() method of the InputStream class reads a single short from the stream and returns the value as an integer. The System.out.println() method prints the value of the short to the console. Here are some key points to understand about the short datatype: Range: The short datatype has a relatively moderate range compared to other integer datatypes like int and long. It can hold values from -32,768 to 32,767. Memory: Since it uses 16 bits (2 bytes) of memory, a short variable occupies more memory compared to a byte but less memory compared to an int or long. Operations: You can perform various arithmetic and logical operations on short values, just like other numeric datatypes. Java automatically promotes short values to int when performing arithmetic operations to prevent potential overflow. Type Casting: When performing operations involving short and other numeric types (such as int), you might need to explicitly cast the values to avoid potential data loss. Use Cases: The short datatype is often used in scenarios where memory conservation is important, but a larger range of values is required compared to byte. It can be suitable for applications involving smaller datasets or where storage efficiency is a concern. Considerations: While using short can help conserve memory, it's essential to consider that modern systems and architectures often handle larger datatypes efficiently. Therefore, using short solely for memory conservation might not always be necessary.