Skip to content

Operators

In Java, operators are special symbols that perform operations on variables and values. They are an essential part of most programming languages and allow us to perform both basic and complex operations. In this lesson, we'll cover the three main types of operators: arithmetic, relational, and logical.

Arithmetic Operators

Arithmetic operators are used to perform common mathematical operations. In Java, these are:

  • + : Addition
  • - : Subtraction
  • * : Multiplication
  • / : Division
  • % : Modulus (remainder)

Here's an example of how to use arithmetic operators in Java:

int a = 10;
int b = 20;

// Addition
int sum = a + b; // sum is now 30

// Subtraction
int difference = a - b; // difference is now -10

// Multiplication
int product = a * b; // product is now 200

// Division
int quotient = b / a; // quotient is now 2

// Modulus
int remainder = b % a; // remainder is now 0

Relational Operators

Relational operators are used to compare two values. They return a boolean result: true if the comparison is true, and false otherwise. The relational operators in Java are:

  • == : Equal to
  • != : Not equal to
  • `> : Greater than
  • <   : Less than
  • >= : Greater than or equal to
  • <= : Less than or equal to

Here's an example of using relational operators:

int a = 10;
int b = 20;

boolean isEqual = (a == b); // isEqual is false
boolean isNotEqual = (a != b); // isNotEqual is true
boolean isGreater = (a > b); // isGreater is false
boolean isLesser = (a < b); // isLesser is true
boolean isGreaterOrEqual = (a >= b); // isGreaterOrEqual is false
boolean isLesserOrEqual = (a <= b); // isLesserOrEqual is true

Logical Operators

Logical operators are used to combine two or more conditions. They return a boolean result based on the truthfulness of the conditions. Java includes the following logical operators:

  • && : Logical AND (returns true if both conditions are true)
  • || : Logical OR (returns true if either condition is true)
  • !   : Logical NOT (returns true if the condition is false)

Here's an example of using logical operators:

int a = 10;
int b = 20;

boolean andResult = (a > b) && (a != b); // andResult is false
boolean orResult = (a < b) || (a != b); // orResult is true
boolean notResult = !(a == b); // notResult is true

Tasks

Now, let's practice using the operators we've learned about. In a new Java file named Week2Task2.java, complete the following tasks:

Here are some tasks you can do to practice with operators:

  1. Declare two integer variables a and b, and assign them the values 10 and 20, respectively.
  2. Use each of the arithmetic operators (+, -, *, /, %) to perform operations on a and b. Print out the results of each operation.
  3. Use each of the relational operators (==, !=, >, <, >=, <=) to compare a and b. Print out the results of each comparison.
  4. Use each of the logical operators (&&, ||, !) to combine two boolean conditions. You could, for instance, check if a and b are both greater than 5, or if either a or b is greater than 25, or if it's not the case that a and b are equal. Print out the results of each operation.

Remember, experimentation is a great way to learn. Try changing the values of a and b or the conditions you're checking and see how the output changes!

Answers

Additional Resources

W3 Schools - Operators