HomeJava

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

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

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

In the previous article, I have explained different types of Literals in Java.

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

Java for Testers – Assigning different types of literals to the byte 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 byte type declared variable.

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

byte data type variable can be assigned with the integer values which are in the range of -128 to 127

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

  1. Within range integer literal Example: 113
  2. Out of range integer literal Example: 927

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 byte data type as shown below:

out of range - byte

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

assigning different literals to byte - integers within

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

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

assigning different literals to byte - integers out of range

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

Assigning Long type literal to the byte data type variable

On assigning the long type literal to the byte 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 byte data type as shown below:

out of range - byte

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

assigning different literals to byte - long

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

Assigning character type literal to the byte data type variable

On assigning the character type literal to the byte 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 byte data type as shown below:

out of range - byte

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

assigning different literals to byte - character

4) Now print the value stored in the byte 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:

assigning different literals to byte - character printed

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

Assigning boolean type literal to the byte data type variable

On assigning the boolean type literal to the byte 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 byte data type as shown below:

out of range - byte

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

assigning different literals to byte - boolean compiler

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

Assigning float type literal to the byte data type variable

On assigning the floating-point type literal to the byte 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 byte data type as shown below:

out of range - byte

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

assigning different literals to byte - float

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

Assigning double type literal to the byte data type variable

On assigning the double type literal to the byte 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 byte data type as shown below:

out of range - byte

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

assigning different literals to byte - double

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

Assigning  String type literal to the byte data type variable

On assigning the String type literal to the byte 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 byte data type as shown below:

out of range - byte

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

assigning different literals to byte - String

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

Here conclude this article on assigning the different types of Literals to the byte 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 *