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
2) Inside the main() method, let’s create a variable and declare it with char data type as shown below:
3) Assing the integer value say 115, to the char data type variable as shown below:
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:
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:
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.
- > 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.