Operator Precedence
Start course
Difficulty
Beginner
Duration
2h 12m
Students
97
Ratings
4/5
starstarstarstarstar-border
Description

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
Transcript

Hi there. In this video, we'll talk about Operator Precedence. It's important to know how Java interprets the symbols you use to perform specific operations and in what order it interprets them. Otherwise, we could write a program with one result in mind and receive an entirely different result. As you see in the table, the priority column is most important because it defines the strict order in which Java interprets the symbols displayed in the operator column. An operator higher in the table always takes precedence over an operator that's lower in the table. There's an important point in this part. If you want to change the order in which an expression is evaluated, you have to use parentheses. Any part of an expression enclosed in parentheses is evaluated first. Let's try to understand operator precedence with some examples.

In Exercise project, right click on the 'operator' package and select 'New', 'Class'. Specify the class name as OperatorPrecedence, and select the checkbox for the main method. Let's declare a variable result with double type and assign 5 + 2 * 4. Okay, let's print the result value by using the print method. Let's run the code. The result is 13, not 28, because the precedence of the multiplication operator is higher than the additive operator. Here, multiplication is performed before adding and the result value is 13. Let's do another example. result = let's assign in the parentheses (5 + 2) and multiply 4 to the variable result. Let's copy the print method. Okay, let's run the code. The result is 28. Here, the expression enclosed in parentheses is evaluated first, then multiplication is done. So, first 5 + 2 equals 7 is done, and 7 is multiplied by 4, and the result equals 28. 

Let's do another example. result += 90/30%10*2+ -4. Let's copy the print method and paste it here. Okay, what do you think the result will be? Let's run it and see. As you can see, the result is 30 because the precedence of multiplicative operators is higher than additive operators. And finally, the assignment operator here will be calculated. So, it first divides 90 by 30. The result will be 3. It then looks at the remainder operator here. Since 3 is less than 10, it still accepts it as 3. Then it goes to the multiplication operation and multiplies 3 by 2. The result is now 6. Then add 6 to -4, and the result is 2. Finally, it looks at the add and assignment operator and adds the value of the variable result to 2. So, it adds 28 to 2, and the result is 30.

Let's make a new example with unary operator precedence. Let's declare four variables with int type. First, x and assign 8. Second, y and assign 4. Third, z and assign 2. And the last, sum, assign 0. Let's assign this operation to the sum variable. x + --y + --z. Let's copy the print method and paste it here. Okay, let's run the code. The result is 12 because the operator precedence of the unary operator is higher than the additive operator. Okay, you can do many more examples to understand the precedence of the operators. So, let's take a short break here. See you in the next video.

 

About the Author
Students
1889
Courses
64
Learning Paths
4

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.

Covered Topics