HomeJava

Java for Testers – Assigning different types of literals to the short data type variable

Java for Testers – Assigning different types of literals to the short data type variable

<<Previous Post                    << Complete Tutorial>>                        Next Post>>

In the previous article, I have explained assigning different types of literals to the byte data type variable.

In this article, I am going to explain assigning different types of literals to the short data type variable.

Java for Testers – Assigning different types of literals to the short data type variable

There are different types of literals like integer, long, character, boolean, floating-point, double and string literals.

I will practically demonstrate assigning the literals one after the other to the short type declared variable.

Assigning within and out of range integer literals to the short data type variable

short data type variable can be assigned with the integer values which are in the range of -32,768 to 32,767

Let’s take examples for within and out of range integer literals

  1. Within range integer literal Example: 31456
  2. Out of range integer literal Example: 56789

Follow the below steps for implementing the above two examples:

1) Let’s create a Java program and create a Demo class as shown below:

Note: If you are not aware of creating a Java project and a Class in Java, refer to our previous article – Creating a Java project in Eclipse IDE

Print statements - Java project

2) Inside the main() method, let’s create a variable and declare it with short data type as shown below:

short assigned different - short

 

3) Assing the within range integer value say 31456, to the short data type variable as shown below:

short assigned different - short within

 

In the above example, there is no compiler error and the within range integer value got assigned properly to the short type declared variable.

4) Assign out or range integer value say 56789, to the short data type variable as shown below:

short assigned different - short out of range

 

In the above example, on assigning out of range integer value of a short data type variable, we got a compiler error – ‘Type mismatch: cannot convert from int to short’ as shown below:

short assigned different - short out of range compiler error

Assigning Long type literal to the short data type variable

On assigning the long type literal to the short data type variable, we will get a compiler error.

Long type literals are the integer values having the L letter added at the end (i.e. 913L)

Follow the below steps for implementing this:

1) Let’s create a Java program and create a Demo class as shown below:

Note: If you are not aware of creating a Java project and a Class in Java, refer to our previous article – Creating a Java project in Eclipse IDE

Print statements - Java project

2) Inside the main() method, let’s create a variable and declare it with short data type as shown below:

short assigned different - short

 

3) Assing the Long type value say 913L, to the short data type variable as shown below:

short assigned different - long to short compiler error

 

In the above example, on assigning Long type literal to the short type declared variable, we got a compiler error – ‘Type mismatch: cannot convert from long to short’

Assigning character type literal to the short data type variable

On assigning the character type literal to the short data type variable, we will not get a compiler error. Instead, the ASCII value of character liter ‘p’ i.e. ( 115 as shown at this page) will be assigned.

character type literals are the single letter characters within the single quotes. For example ‘p’ is the character literal.

Follow the below steps for implementing this:

1) Let’s create a Java program and create a Demo class as shown below:

Note: If you are not aware of creating a Java project and a Class in Java, refer to our previous article – Creating a Java project in Eclipse IDE

Print statements - Java project

2) Inside the main() method, let’s create a variable and declare it with short data type as shown below:

short assigned different - short

 

3) Assing the character type value say ‘p’, to the short data type variable as shown below:

short assigned different - short character assign

 

4) Now print the value stored in the short declared variable using the print statement and observe that the ASCII value of the assigned character will be converted, assigned and then printed as shown below:

short assigned different - short character printed

 

In the above example, on assigning character literal to the short data type variable, the character literal got converted to the ASCII value (As listed in this page) and printed

Assigning boolean type literal to the short data type variable

On assigning the boolean type literal to the short data type variable, we will get a compiler error.

boolean type literals are only two (i.e. true and false)

Follow the below steps for implementing this:

1) Let’s create a Java program and create a Demo class as shown below:

Note: If you are not aware of creating a Java project and a Class in Java, refer to our previous article – Creating a Java project in Eclipse IDE

Print statements - Java project

2) Inside the main() method, let’s create a variable and declare it with short data type as shown below:

short assigned different - short

 

3) Assing the boolean type literal say false, to the short data type variable and observe that the compiler error – ‘Type Mismatch: cannot convert from boolean to short’ will be displayed as shown below:

short assigned different - boolean to short compiler error

 

In the above example, on assigning boolean type literal to the short type declared variable, we got a compiler error – ‘Type mismatch: cannot convert from boolean to short’

Assigning float type literal to the short data type variable

On assigning the floating-point type literal to the short data type variable, we will get a compiler error.

floating-point type literals have the decimal value followed by f letter. (i.e. for example 3.69f)

Follow the below steps for implementing this:

1) Let’s create a Java program and create a Demo class as shown below:

Note: If you are not aware of creating a Java project and a Class in Java, refer to our previous article – Creating a Java project in Eclipse IDE

Print statements - Java project

2) Inside the main() method, let’s create a variable and declare it with short data type as shown below:

short assigned different - short

 

3) Assing the floating-point type literal say 3.69f, to the short data type variable and observe that the compiler error – ‘Type Mismatch: cannot convert from float to short’ will be displayed as shown below:

short assigned different - float to short compiler error

 

In the above example, on assigning floating-point type literal to the short type declared variable, we got a compiler error – ‘Type mismatch: cannot convert from float to short’

Assigning double type literal to the short data type variable

On assigning the double type literal to the short data type variable, we will get a compiler error.

double type literals are decimal values. (i.e. for example 3.69)

Follow the below steps for implementing this:

1) Let’s create a Java program and create a Demo class as shown below:

Note: If you are not aware of creating a Java project and a Class in Java, refer to our previous article – Creating a Java project in Eclipse IDE

Print statements - Java project

2) Inside the main() method, let’s create a variable and declare it with short data type as shown below:

short assigned different - short

 

3) Assing the double type literal say 3.69, to the short data type variable and observe that the compiler error – ‘Type Mismatch: cannot convert from double to short’ will be displayed as shown below:

short assigned different - double to short compiler error

 

In the above example, on assigning double type literal to the short type declared variable, we got a compiler error – ‘Type mismatch: cannot convert from double to short’

Assigning  String type literal to the short data type variable

On assigning the String type literal to the short data type variable, we will get a compiler error.

String type literals are sequence of characters within double-quotes. (i.e. for example “QAFox – The Software Testing Tutorials”)

Follow the below steps for implementing this:

1) Let’s create a Java program and create a Demo class as shown below:

Note: If you are not aware of creating a Java project and a Class in Java, refer to our previous article – Creating a Java project in Eclipse IDE

Print statements - Java project

2) Inside the main() method, let’s create a variable and declare it with short data type as shown below:

short assigned different - short

 

3) Assing the String type literal say “QAFox – The Software Testing Tutorials”, to the short data type variable and observe that the compiler error – ‘Type Mismatch: cannot convert from String to short’ will be displayed as shown below:

short assigned different - string to short compiler error

 

In the above example, on assigning String type literal to the short type declared variable, we got a compiler error – ‘Type mismatch: cannot convert from String to short’

Here conclude this article on assigning the different types of Literals to the short data type in Java.

In the next article, I will explain the remaining things about data types.

Next Steps:

  • > To learn more about Java, continue to the next post (Click on Next Post link below)
  • > Check complete Java Tutorial Contents here (Click here)

Please leave your questions/comments/feedback below:

Happy Learning 🙂

Arun Motoori (www.QAFox.com)

On a mission to help the Testing Community in all possible ways.

<<Previous Post                    << Complete Tutorial>>                        Next Post>>

If you like this article, share it on LinkedIn using the below button:

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *