Post

Created by @adamvaughn
 at November 6th 2023, 12:56:00 am.

Post 3: Operators and Expressions in Java

In Java programming, operators are symbols that perform operations on operands to produce a result. Expressions, on the other hand, are combinations of variables, operators, and values that evaluate to a single value. Understanding operators and expressions is crucial in writing efficient and effective Java code.

Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations on numerical values. The following table summarizes the arithmetic operators available in Java:

Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (remainder)

Here are some examples of arithmetic expressions in Java:

int a = 10;
int b = 5;

int sum = a + b;        // sum = 15
int difference = a - b; // difference = 5
int product = a * b;    // product = 50
int quotient = a / b;   // quotient = 2
int remainder = a % b;  // remainder = 0

Assignment Operators

Assignment operators are used to assign values to variables. The most commonly used assignment operator is =. Other assignment operators include +=, -=, *=, /=, and %=. These operators provide a shorthand way to perform an operation and store the result back into the same variable.

Let's consider an example:

int num = 10;

num += 5;  // Equivalent to: num = num + 5;
num -= 3;  // Equivalent to: num = num - 3;
num *= 2;  // Equivalent to: num = num * 2;
num /= 4;  // Equivalent to: num = num / 4;
num %= 3;  // Equivalent to: num = num % 3;

System.out.println(num);  // Output: 0

Relational Operators

Relational operators are used to compare values and determine the relationship between them. The result of a relational expression is a boolean value (true or false). The following table shows the relational operators available in Java:

Operator Description
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Here is an example that demonstrates the usage of relational operators:

int x = 10;
int y = 5;

boolean isEqual = x == y;     // isEqual = false
boolean isNotEqual = x != y;  // isNotEqual = true
boolean isGreater = x > y;    // isGreater = true
boolean isLess = x < y;       // isLess = false
boolean isGreaterOrEqual = x >= y;  // isGreaterOrEqual = true
boolean isLessOrEqual = x <= y;     // isLessOrEqual = false

Logical Operators

Logical operators are used to combine multiple conditions and evaluate them as a single boolean result. The following table shows the logical operators available in Java:

Operator Description
&& Logical AND
|| Logical OR
! Logical NOT

Here is an example that illustrates the usage of logical operators:

int age = 20;
boolean isStudent = true;

boolean isTeenager = age >= 13 && age <= 19;   // isTeenager = false
boolean isEligible = age >= 18 || isStudent;   // isEligible = true
boolean isNotStudent = !isStudent;             // isNotStudent = false

Precedence of Operators

The precedence of operators determines the order in which they are evaluated in an expression. It is important to understand the precedence rules to ensure the desired behavior of your code. Here is a table that shows the precedence of operators, from highest to lowest:

  1. Logical NOT (!)
  2. Multiplication (*), Division (/), Modulus (%)
  3. Addition (+), Subtraction (-)
  4. Relational (>, <, >=, <=)
  5. Equality (==, !=)
  6. Logical AND (&&)
  7. Logical OR (||)

Parentheses () can be used to alter the default precedence and explicitly define the order of evaluation.

int result = 10 + 5 * 2;  // result = 20 (multiplication is performed first)
int newResult = (10 + 5) * 2;  // newResult = 30 (addition is performed first)

Understanding the different operators and expressions in Java is crucial when writing programs. Practice using different operators in various scenarios to become comfortable with their usage. In the next post, we will explore control flow statements, which allow us to control the execution of our Java programs.