Java Short – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Mon, 04 Jul 2022 14:27:54 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg Java Short – Programmerbay https://programmerbay.com 32 32 Java Short Primitive Data Type With Program Example https://programmerbay.com/java-short-primitive-data-type-with-program-example/ https://programmerbay.com/java-short-primitive-data-type-with-program-example/#respond Thu, 30 Jun 2022 05:26:34 +0000 https://programmerbay.com/?p=8699 In Java, short is a primitive data type that can hold 16-bit data size. It ranges from –32,768 to 32,767. It is a useful data type for saving memory in large arrays. Its default value is 0.

Syntax:

short variableName = 2;

Printing short data type

Short is signed type that can support positive and negative values, ranging from -215 to 215-1. Lets see a code to simply print short type.

public class ShortExamples {
    public static void main(String[] args) {
        short posNum =  23;
        short negNum =  -23;
        System.out.println("negative short num :: "+ negNum);
        System.out.println("Positive short num :: "+ posNum);


    }
}

Output: 

negative short num :: -23
Positive short num :: 23

Java short to Int

Short is a primitive data type that can store lesser range of values as compared to int type. There are multiple ways to  convert short to int.

1. By using implicit type casting

A short can be automatically converted int value by the java compiler. When a compiler implicitly converts a type to another data type ( to higher type), this is termed as implicit type casting.

Java program for converting short to int using type casting

public class ShortExamples {
    public static void main(String[] args) {
        short shortNum =  23;
        int num =  shortNum;
        System.out.println("short value type :: "+ num);
    }
}

Output: 

short value type :: 23

2. By using intValue()

Short is a wrapper class that supports a method named intValue(). The method outputs int literal by accepting short value.

Java program for converting short to int using intValue method

public class ShortExamples {
    public static void main(String[] args) {
        short shortNum =  23;
        int num =  new Short(shortNum).intValue();
        System.out.println("short value type :: "+ num);
    }
}

Output:

short value type :: 23

3. By using unsignedInt() method

The method performs unsigned conversion. It takes short value as input parameter and returns its integer representation. Below is the example program.

Java program for converting short to int using intValue method

public class ShortExamples {
    public static void main(String[] args) {
        short shortNum =  23;
        int num =  Short.toUnsignedInt(shortNum);
        System.out.println("unsignedInt type:: "+ num);
    }
}

Output:

unsignedInt type:: 23

Frequently Asked Question

1. How to declare or create short in Java?

Short can be declared in two ways, using short keyword and Short wrapper class.

With the help of short class’s constructor, one can declare and initialize short object. The constructor accepts either string literal or short primitive value and returns initialized  object.

Using short keyword: 

public class ShortExamples {
    public static void main(String[] args) {
        short shortNum =  23;
        System.out.println("value "+ shortNum);
    }
}

Using Short object:

Java doesn’t allow to pass int literal to the short constructor as parameter.

public class ShortExamples {
    public static void main(String[] args) {
        Short shortObj= new Short("23");
        System.out.println("value "+ shortObj);
    }
}

 

]]>
https://programmerbay.com/java-short-primitive-data-type-with-program-example/feed/ 0