short to int Java

After knowing primitive data types and Java rules of Data Type Casting (Type Conversion), let us cast short to int as an example.

The short takes 2 byte of memory and int takes 4 bytes of memory. Assignment of 2 bytes of memory to 4 byte of memory is done implicitly. This is known as widening conversion.

See the data types order to know the brief rules of casting.

byte –> short –> int –> long –> float –> double

The left-side value can be assigned to any right-side value and is done implicitly. The reverse like int to short requires explicit casting.

Examples of Java implicit casting

short x = 10;
int y = x;

byte x = 10;
short y = x;

Following example on short to int explains the syntax of Java implicit casting.
public class Conversions
  public static void main(String args[])
    short s1 = 10;                     // 2 bytes
    int i1 = s1;                       // 2 bytes assigned to 4 bytes

    System.out.println("short value: " + i1);            // prints 10
    System.out.println("Converted int value: " + i1);    // prints 10

short to int
Output screenshot of short to int Java

int i1 = s1;

The short s1 is assigned to int i1. This is done implicitly by JVM without extra efforts by Programmer.

View all for 65 types of Conversions

Leave a Comment

Your email address will not be published.