HomeJava

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

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

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

In the previous article, I have explained assigning different types of literals to the String non-primitive data type variable.

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

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

We will not get any compiler error on assigning character and integer literals to the character data type variable.

But on assigning the other than character and integer literals will result in a compiler error.

Let me demonstrate assigning different literals one after the other.

Assigning integer literals to the character data type variable

character data type variable can be assigned with any integer values and it won’t result in an error.

The assigned integer values will  be converted to the associated ASSCI character value before getting assigned to the variable.

For example, on assigning the integer value 115 to the character data type variable a, then the integer value 115 will be converted to the character value ‘s’ (As mentioned in the http://www.asciitable.com/)

Follow the below steps for implementing the above example:

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

char assign different - char

3) Assing the integer value say 115, to the char data type variable as shown below:

char assign different - char integer

4) Now print the value stored in the char declared variable using the print statement and observe that the integer value (i.e. 123) assigned to the variable will be converted to related ASCII character say ‘s’, and then assigned &  printed as shown below:

char assign different - char integer printed

But on trying to assign the literals other than char and integer type, Java will give you a compiler error.

The below are the screenshots of the compiler errors displayed on assigning the literals other than char and integer type:

char assign different - char double

char assign different - char float

char assign different - char long

char assign different - char boolean

char assign different - char string

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