Java Data Types and Operators
The course is part of this learning path
In this course, we'll learn the fundamentals of Java.
- Data Types
- Type Conversion & Type Casting
- Operator Precedence
- Expressions, Statements and Block
- 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
- No prior knowledge is required about the Java programming language.
- Basic computer knowledge
Hi there. Now, let's look at the questions you may encounter about variables and operators and their solutions. This question is one of the simple questions. It's asking for the data type used to create a variable that stores the texts. If you remember, we could store texts with string data type. Therefore, the answer is option D. Let's move on to the next question. In this question, it wants us to determine which of the following about variable naming conventions is correct. Here, you can pause the video and answer the question. I'll be giving the answer on the next slide.
Yes, friends A, B and E options are in accordance with the variable naming convention. Variable names can contain dollar signs, variable names can start with an underscore. Again, it can be capitalized public variable name. If the first letter of this was lower case, then it would be a Java keyword and not a variable name. True is the Java keyword. java.lang is not valid because it contains dots. Option E cannot be a variable name because it starts with a number. Variable names cannot start with a digit. Let's move on to the next question. Yes, this question is about operators. It asks which of the following operators can be used with the boolean data type. Pause the video here and try to answer it yourself.
I'll be giving the answer on the next slide. Yes, you have given the correct answer if you have selected options, B, F, G, and H. The operator in option A is the increment operator. You cannot apply it to boolean data types. The operator in option B is the equal to operator. This operator compares two different types; returns true if same, false otherwise. So, it can be used with boolean type. The operator in option C is the add and assignment operator, not used with the boolean data type. The operator in option D is the subtraction operator. This is also not used with the boolean data type. The operator in option E is greater than or equal to operator. This is also not used with the boolean data type.
The operator in option F is the not equal to operator. This operator compares two different types; returns false if the same, not true. It can be used with the boolean data type. The operator in the G option is the conditional AND operator. It compares two boolean types. If both are true, the result is true, otherwise it is false. It is used with a boolean data type. The operator in option H is the logical complement operator. It makes false the value of true and it makes true the value of false. It is used with the boolean data type. As a result, the answer is options B, F, G, and H. Let's move on to the next question. Yes, this question asked about the bitwise exclusive OR operator.
Three variables named A, B and C are defined in int data type and the values of these variables are compared and the result is written to the console. The question is, what will be the result? Pause the video here and try to answer it yourself. I'll be giving the answer on the next slide. Yes, if you marked it as false, you gave the correct answer. Let's examine the question now. The value of variable a is 10, the value of variable b is 7. So, a is greater than b, and the result of the first comparison in the println method will be true. In the other comparison, since c has a value of 15 and b has a value of 7, b is less than c and the result is still true. Here, the result is false even though both results are true, because the bitwise exclusive OR operator is used. Remember this operator returns true if both cases are different.
If both cases are the same, then it returns false as well. So, since both cases being compared here are the same, i.e. true, the result is false. Do not forget this. The bitwise exclusive OR operator checks whether the two conditions are the same or different, rather than whether the conditions are true or false, and returns a result accordingly. Therefore, the result is false. Now, let's move on to the next question. Yes, this type of question is a type of question that can confuse you. In fact, it's a simple question type that requires some attention. If you want stop the video here and try to figure it out yourself. We'll solve the question together on the next slide. Yes, your answer is correct if you selected option D.
Now, let's look at the solution to the problem. In line 4 the division is done using two int values. If both treated values are ints, the result is also int. So, even though the result of 11 divided by two is 5.5, this result is cast to int data type and the result is 5. So, the first print method prints 5 to the console. Now, let's look at the second print method. Notice that although the first number is an int, the second number is double. Therefore, the result of the operation will be double. So, this time the result that is 5.5 will not be cast to int data type, and the result will be printed to the console as 5.5. In the third print method, two int data types are used again. Since the remainder operator is used here, the results will be one.
So, the third print method prints 1 to the console. In the last print method, since the second number is double, the result is printed to the console as a double. So, this print method also prints 1.0 to the console. And since all methods are the print methods, the results will be printed on the same line. So, the result will be 55.511.0. The answer is option D. Let's move on to the next question. In this question, a variable of type byte was created and the value 130 was transferred to this variable. Casting was also done. As information, the range of the byte data type is given. The question is, what will be the output of this piece of code? Pause the video and try to solve the question. I'll be giving the answer on the next slide.
Yes, if you calculated the output as -126, you calculated correctly. Now let's check the answer together. The byte takes a value between -128 and +127. Think of it as a number line stretching from -128 to +127, minimum value -128, maximum value +127. If you convert 127 to byte type you will still get 127. If you try to convert 128 to byte type this time you will get the value -128 at the beginning of the number line. If you convert 129 bytes, the value you will get this time is -127. So, for each value after 127, you move from the negative side of the number line to the positive side. Therefore, the value 130 also corresponds to -126 in byte type. So, the answer is option F. Yes, in this question, both char data type and boolean data type are used, and some operations are done using operators.
Pause the video here and try to figure it out yourself. We'll tackle the question together on the next slide. Yes, if you ticked the E option, you gave the correct answer. Let's solve it together now. In line the value 65 is assigned to the variable char1, so char1 currently represents character A. In line 5, char1++ is assigned to the variable char2. Since the increment operator is used after the variable char1 here, the value of the variable char1 will be transferred to the variable char2 first and then the value of the variable char1 will be increased by one. Therefore, the value of the variable char2 become 65 and stores the character A. When the compiler pass to line 6, the value of the char1 variable increases by one, and the value of the char1 variable is now 66, and stores the B character. Now let's look at the control on line 6.
The statement, char 2 = B, in the first condition is false, because the value of char 2 is 65 and stores the character A. The first condition is false. The statement, char 1 < B, in the second condition is false, because the new value of char 1 is 66 and stores the character B. So, the second condition is false. Both conditions are compared with the OR operator and the value of the variable result is false, because both conditions are false. As a result, char 1 stores character B, and char 2 stores character A, and the result is false. If char 1 and char 2 were transferred to the int variable before printing to the console, and the int variable was printed to the console. This time int values willl be printed instead of characters. Pay attention to this. Since there is no transfer to any int variable here, the characters will be printed to the console as they are.
So, the output is option E, not option B. Let's move on to the next question. Yes. This question is specifically for the use of increment operators. Variables of int, short and long data types are defined and a value is assigned to these variables. Afterwards, some operators are applied to these variables and a mathematical operation is performed, and the result is written to the console. We'll try to find out what the output of this code will be. Pause the video here and try to figure it out yourself. I'll be giving the answer on the next slide. Yes. If you found the answer as 49, you gave the correct answer. Now, let's solve the question together. Variable A has a value of 5, variable B has a value of 7, and variable C has a value of 9.
Looking at the print method, there are increment, decrement and arithmetic operators. Since increment and decrement operators take precedence over arithmetic operators, let's examine them first. We see that the increment operator is applied to variable A. But notice, that the increment operator is used after variable A. Therefore the value of variable A will not change for the operation here. So A is still 5. Since the increment operator is applied to variable B and it's applied before variable B, the value of variable B increases by 1. So, the new value of variable B becomes 8.
The decrement operator is also used in the C variable. But since it's used right after the C variable, the value of the C variable will not change in the operation here. That is, the C variable will also remain as 9. So, our transaction became, 5 * 8 + 9. So, we have operators to consider, multiplication and addition. Since the multiplication operator takes precedence, we must multiply 5 and 8 first. So, 5 * 8 = 40. Then we need to add 40 to the value of C. That is nine. So, the results will be 49. The answer is option C. Let's move on to the next question. Yes. This question is also about the use of the ternary operator, but if you notice, it's a bit more complex. Pause the video and try to solve it. I'll be giving the answer on the next slide.
Yes, it's correct if you found the answer as 7. Now let's examine the question together and solve it. In line four, the variables A and B of type int are defined. Variable A is assigned a value of 7 and variable B is assigned a value of 4. In addition, the boolean type variable C is assigned, and the value A < B is assigned to the variable C. But A is not less than B, so the initial value of variable C will be false. Now let's look at line 5. In this line, a variable named result of type int is defined. Notice, that this variable is assigned a value using the ternary operator.
Let's find this now. If you notice the condition, there is a long expression consisting of AND and OR operators. Since the AND operator has higher precedence than the OR operator, let's look at the AND operator first. The statement, A>10, is incorrect, because the value of A is 7 and 7 is less than 10. In this case, the first expression is false. Since the first statement is false, there's no need to look at the second condition, because in the AND operator, if the first expression is false the result is false. So, the operand before the OR operator will be false.
Now let's look at the operator after the OR operator. Here is the statement, C is equal to false. Since the initial value of C is false, this expression is true and the result here is true. Therefore, since the first operand of the OR operator is false, but the second operand is true, the condition of the ternary operator is true. Because the condition is true, the statement just after the question mark will be executed. The expression after the question mark is A++. So, an increment operator is used here, but notice that the increment operator is used after variable A. That is, the value of the variable A will be transferred to the result variable first, and the value of A will be increased later. Therefore, the value 7 will be assigned to the result variable. That is, the value 7 is printed on the console screen. The answer is option E.
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.