Java Short Keyword
Java supports eight different primitive datatypes. The language has predefined primitive datatypes that are given keyword names. Let's take a closer look at each of the eight primitive data types. 'short' in Java is a primitive data type. To declare variables, use this. It can also be applied to techniques. It has a 16-bit signed; two's complement integer storage capacity.
The short keyword in Java is used to define a numeric variable's type. A short variable can hold any 16-bit integer number in the range of -32,768 to 32767.The java programming language has a keyword called "the short" that it defines. In the Java programming language, keywords are essentially reserved words with specific meaning significant to a compiler. The short keyword also denotes the following.
Important Points to Remember about short Keyword:
- The short has a value of 32,767 as its maximum and a minimum (inclusive).
- The default setting is 0.
- Its 2-byte default size.
- Large arrays use it to conserve memory.
- To declare a variable as a numeric type, use the short keyword.
- A signed 16-bit type, short.
- Its range is between -32,768 and 32,767.
- The short primitive type is wrapped in the short class. The range of values for this type is represented by the variables MIN VALUE and MAX VALUE.
These constants represent the range of values for this type that are compatible with the short class, as determined by compiler compatibility.
Examples of Java Short Keyword
Example 1:
A brief data type example containing positive and negative values.
public class ShortEx1
{
public static void main (String...k)
{
short n1=1;
short n2=-1;
System.out.println("n1: "+n1);
System.out.println("n2: "+n2);
}
}
public class ShortEx1
{
public static void main (String...k)
{
short n1=1;
short n2=-1;
System.out.println("n1: "+n1);
System.out.println("n2: "+n2);
}
}
Output:
n 1: 1
n2: -1
Example 2:
A brief data type example containing positive and negative values.
public class ShortEx2
{
public static void main (String... s)
{
shorthi=11.5;
System.out.println("hi: "+hi);
}
}
Output:
error: incompatible types: possible lossy conversion from double to int.
Example 3:
Program to check whether the int keyword stores the float values or not.
public class ShortEx3
{
public static void main (String...k)
{
short n=20f;
System.out.println("n: "+n);
}
}
Output:
error: incompatible types: possible lossy conversion from float to int
Example 4:
Let's look at an example to see if the int data type can store char values. In this situation, the compiler implicitly converts the character to an int type and outputs the corresponding ASCII value.
public class ShortEx4
{
public static void main (String... s)
{
Short n11='c';
System.out.println("n11: "+n11);
}
}
Output:
n11: 99
Example 5:
The minimum and maximum values in this example are stored in the int data type.
public class ShortEx5
{
public static void main (String... s)
{
int low value=-2147483648;
int high value=2147483647;
System.out.println("low value: "+low value);
System.out.println("high value: "+high value);
}
}
Output:
low value: -2147483648
high value: 2147483647
Example 6:
Program to create the method which returns the value of integer.
public class ShortEx6 {
public shortoutput (short n)
{
return n;
}
public static void main (String [] s) {
ShortEx6 bj=new shortEx6();
System.out.println(bj. Output (30));
}
}
Output:
30
Example 7:
Program using methods and local variables.
public class LocalVariableEx 7 {
public shortadd (int a) {
shortadd = 0;
for (int i = 0; i <a; i++) {
add = add + i;
}
return add;
}
public static void main (String [] s) {
LocalVariableEx 7 localVariableEx 7 = new LocalVariableEx 7();
short sum = localVariableEx 7.add (10);
System.out.println("addition of initial 10 numbers -> " + add);
}
}
Output:
addotion of initial 10 numbers -> 45
Example 8:
public class shortEx 8{
public static void main (String s []) {
boy age (18);
}
static shortboy age (short a) {
System.out.println("Boy age is:"+a);
return0;
}
}
Given code is executed to produces the following result:
Boy age is: 18