HomeJava

Java for Testers – Passing character values to the int data type variable

Java for Testers – Passing character values to the int data type variable

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

In the previous articles, I have explained about passing decimal values to the int data type variable.

In this article, I am going to explain and show you what happens when we try to assign the character values to the int declared variable.

Java for Testers – Passing character values to the int data type declared variables

Java will convert the character value to the integer value and then assign it to the int data type declared variable.

Every character value will have an associated ASCII integer value. Java will simply covert the character value to the ASCII integer value and then assign to the int declared variable.

Follow the below steps to find out how the character value, when assigned to the int data type, declared variable will be converted to the ASCII integer value before getting assigned:

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

decimal to int - declaring int

3)  Assing the character value say ‘s’ to the above int data type declare variable as shown below:

char to int - char

Observe that on assign the character value say ‘s’ to the int data type declared variable ‘a’, compiler error won’t be displayed.

But, what will happen on assigning the character value ‘s’ to the int data type declared variable ‘a’ as shown above.

The character value will be converted to its ASCII integer value 115 and then get assigned to the int data type declared varaible ‘a’.

Navigate to the below web page which has the ASCII integer value representation for all the characters:

 

4) Now print the ASCII inger converted and stored value in the variable by printing the variable using the print statement as shown below:

char to int - char print

5) Save the Project and click on the ‘Run’ button.

Observe that the Program got executed and the integer ASCII value 115 of character ‘s’ which was converted and stored into the variable a  got printed as shown below:

char to int - char printed

Copy the above-explained code from the below section

public class Demo {

	public static void main(String[] args) {
		
		int a = 's';
		
		System.out.println(a);
	
	}

}

Here concludes this article.

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>>

Comments (0)

Leave a Reply

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

For FREE Testing Tutorials & Videos

X
Open chat
Contact Us on Whatsapp