The course is part of this learning path
In this course, we'll learn about Java data types and operators.
- 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. In this video, we'll talk about the Unary Operators. The Unary Operators require only one operand. They perform various operations, such as incrementing or decrementing a value by one, negating an expression, or inverting the value of a Boolean. Let's look at these operations. Plus(+).
This is the Unary plus operator. It's used for giving positive values. It's only used when deliberately converting a negative value to a positive. Minus(-). This is the Unary minus operator. It's used for negating the values. Plus Plus(++). This is the increment operator. It increments the value by one. Minus Minus(--). This is the decrement operator. It decrements a value by one. Exclamation Mark(!) . This is the logical complement operator. It inverts the value of a Boolean. Let's make some examples with Unary Operators. In exercise project, right click the 'Operator package' and select 'New class'. Specify the name as UnaryOperator, and select the check box for the main method. Okay, let's start to code.
Let's declare two variables. First, number with double type and assign 7.6. Second, isCheck with Boolean type and assign true. In the first print method, we'll use the Unary plus operator. Let's copy the print method and paste it below four times. In the second print method, we'll use the Unary minus operator. In the third print method, we'll use the increment operator. In the fourth print method, we'll use the decrement operator. In the last print method, we'll use the logical complement operator. That's all. Let's run the code. Remember that the value of the number variable was 7.6.
As you see in the console +number = 7.6; there is no change. -number = -7.6. It inverts the sign of the number variable. ++number = 8.6. It increments the value by one. So, the new value of the number variable is 8.6. --number = 7.6. It decrements the value by one. !isCheck = false. The first value of isCheck was true. It inverts the value of a Boolean. In Java, we can also use ++ and -- operators as both prefix and suffix. However, there is a significant difference while using increment and decrement operators as prefix and suffix. Let's make an example.
Let's declare variable result with the double type and assign 4.7 to it. In the first print method, let's print the own value of the result variable. Now, let's copy and paste this print method below four times. In the second method, I'll use the increment operator as the suffix and write, result++. In the third method, we write out only the result. In the fourth method, we'll use the increment operator as the prefix and write ++result. In the last print method, we write only the result again. That's all. Let's run the code. You see the results in the console. When the first print is executed, the original value is evaluated first.
Then, in the second print method, the value of the result variable was first printed to the console and then, increased by one. So, the second result is 4.7 once again. But the new value of the result variable is 5.7. Therefore, in the third print method, 5.7 was printed to the console. In the fourth print method, the value of the result variable is increased by one and the new value is 6.7, and then, it was printed to the console. So, the 6.7 was printed to the console. And in the last print method, the current value of the result variable is printed on the screen once again. So, it is the 6.7. Okay, let's take a short break here. See you in the next video.
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.