In this course, we'll learn about Java data types and operators.
Learning Objectives
- Variables
- Data Types
- Type Conversion & Type Casting
- Operators
- Operator Precedence
- Expressions, Statements and Block
Intended Audience
- Anyone looking to get Oracle Java Certification
- Those who want to learn the Java Programming language from scratch
- Java developers who want to increase their knowledge
- Beginners with no previous coding experience in Java programming
- Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams
Prerequisites
- No prior knowledge is required about the Java programming language.
- Basic computer knowledge
Hi there. In this lesson, we'll work a little more on the char data type. This is because although the char data type stores a single character, there is an int number corresponding to each character at stores. You can see these values in the ASCII table. For example, the number corresponding to the uppercase letter A is 65, while the number corresponding to the lowercase letter is 97. And in Java, you can encounter some complex operations using these numerical values. For this reason, we'll learn the char data type in a little more detail in this lesson. If you're ready, let's switch to Eclipse and get some practice. In exercise project, right-click on the data type package and select new class.
Okay, char is represented as an integer between zero and 65,535, and it can be used with integer arithmetic. So just remember, char is 0-65,535. Char is a single char or a single character. It can be any character, from special characters to numbers and letters. It's defined in single quotes. Let's see some examples. I'll create a variable type of char. char firstChar = 'J'; So, the letter J is equal to 74 in decimal. Let's create another variable. char secondChar ='1'; So, the number 1 is equal to 49 in decimal. Yes, if you notice, I defined the first variable as a letter and the second variable as a number. But can they really be distinguished as letters or numbers in a char data type?
Let's test it if you want. First, let's print the first char variable to the console. System.out.println("firstChar = " + firstChar); Now let's see if the value stored by this variable is a letter or a number. System.out.println("is firstChar letter?); To understand whether a variable is a number or a letter, we'll use the isLetter and isDigit methods, which are one of the useful methods of the character class. That's why I'm writing character.isLetter here. I write the firstChar variable in the parentheses. This value will be true if the firstChar variable is a letter, false otherwise. Now, let's see if it's a number. I'm copying and pasting this line here. I'm typing digits here. Also, this method will be in digits. It will return true if the firstChar variable is a digit, false otherwise.
Now, let's copy these three print methods, let's paste it here. I'm writing the secondChar variable instead of the firstChar variable. Now let's run the application and observe. Now, let's follow the results on the console screen. As you can see, the first variable is a letter, so the first condition is true. Since the J is a letter, the second condition is false, it is not a digit. Look at the secondChar variable. Since the second variable is a digit, the first condition is false, because 1 is not a letter. Also, the second condition is true, because 1 is a digit. If you notice, each character has a decimal equivalent here. In other words, when we create a variable of char data type and assign the value 65 to this variable, we actually assign the character A to it. Let's see this right on the example.
I'm typing 74 here instead of J. Here I write 49 instead of 1. Now let's run the application once again. As you can see, the characters J and 1 are printed to the console again, because we store it as a char data type, not an int data type. So, whatever it corresponds to in the ASCII table, the compiler detects it. Okay, now let's do a different example. Again, I'll declare a variable of data type char. char myLetter, and I assign the value 66 to this variable. Now, let's define a second variable of data type int. int number =, and I'll assign two more of my letter variables to this variable. So, myLetter +2;
Now, let's create a third variable of char data type, and assign the variable number to this variable. char myNewChar = number; Notice that we got a compilation error, because the int data type is larger than the char data type. An automatic conversion does not occur. If you remember, we had to cast while converting large data types to smaller data types. So, we have to do a typecasting here. If we write char in parentheses before the number variable, we will fix the error. Now, let's print the variable myNewChar to the console. System.out.println("myNewChar = " + myMewChar); Let's guess the results before running the application. We assigned the value 66 to the variable myLetter. 66 corresponds to a capital letter B in the ASCII table. In the second line, we added 2 to this value. So, the value of the variable number became 68. 68 also corresponds to the capital letter D.
In the last line, we converted the value 68 to char data type and assigned it to a new variable. Currently, there's the letter D in the myNewChar variable. As a result, we should see the letter D on the console screen. Now, let's run the application and see. As you can see, the letter D is printed on the console screen. I think you've learned a bit more about the char data type. Now, let's do one more example and end our lesson. First, let's create a new variable in the char data type. char char1 = 65; If you remember, 65 was the letter A. Now, let's create a second variable of data type char. char char2 =, I will assign the value char1++ to this variable. Now, let's create a new variable in boolean data type. boolean boolean1 = here we'll do a check char2 == 'B'; The boolean variable will be true if the value of char2 is equal to the letter B, otherwise false.
Now, let's create another boolean variable. boolean boolean2 =, here's a check like this, char1 < 'D'; Now, let's print the boolean1 and boolean2 values to the console. System.out.println(boolean1); System.out.println(boolean2); Let's guess the result before running the application. The value of char1 is 65. Here, the unary increment operator is written after the variable, that is, first the value of char1 will be transferred to char2, and then the value of char1 will be increased by one. So, after compiling this line, the value of char1 will be 66, while char2 is 65. So, char1 currently corresponds to the letter B, and char2 to the letter A. Let's continue. In the control here, it's checked whether char2 is B or not.
Currently, the result here will be false, since char2 corresponds to the letter A. Let's look at the next line. The control here also checks whether char1 is less than the letter D. The current value of char1 is 66 which is the letter B. So, the condition here is true and will return true. So, we should see false and true statements on the console screen. Let's run the application and test it. As you can see, the first condition was false while the second condition was true. Now, let's make a slight change to the second condition. Here, I'll use the unary increment operator before the char1 variable. I also set the condition here to ==. And I'll change this letter to C. Let's check now. The value of the char1 variable was 66.
Since the increment operator is used before the variable, the value of the char1 variable will be increased first, that is, it will be 67. Then the condition here will be checked. Since the decimal value corresponding to the letter C is also 67, this condition will be true. Let's run the application and see. As you can see, the second condition is still true. Finally, I'll make one more change. I want to write 67 instead of the letter C here. Do you think this code will work? Let's run it and see. And the result is still the same. Yes friends, in this lesson, I tried to show you how we can use the char data type with decimal numbers. Let's take a short break here. See you in the next lesson.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.