You are on page 1of 6

Operators - are special symbols that perform mathematical and logical operations on operands

Categories of Java Operators

Arithmetic Operators

Relational Operators

Arithmetic Operators in Java

Arithmetic Operators - are used in mathematical expressions

Addition Operator

-an operator to add 2 or more values

-using +

-we can store the sum in a variable, and then display it

Ex.

int x = 1 + 3; //we are initializing a variable x to be equal to 1 + 3, so 1 will be added

to 3 and the result will be stored inside the x

System.out.println(“The sum is : “ + x ); //you are printing x over here

-we can display the sum directly.

Ex.

System.out.println(“The sum is : “ + (1 + 3) ); //we are printing string concatenated to

1 + 3 inside parentheses

//The sum is : 4

Note: Parentheses is important, if we don’t use parentheses, the plus sign will be considered as a
concatenation operator; without the parentheses the output will be The sum is : 13. So to display the
expression correctly we use ().

Subtraction Operator

-an operator to subtract 2 or more values

-using -

Ex.

int x = 3 - 1;

System.out.println(“The diff is : “ + x ); //The diff is : 2


-display difference directly

Ex.

System.out.println(“The diff is : “ + (3 - 1) ); //The diff is : 2


System.out.println(“The diff is : “ + 3 - 1 ); //ERROR

Note: The + sign is considered as a concatenation operator, so the three will be concatenated to the string
[“The diff is : “], and after that you are subtracting one from the string that we will get, so this is why we will
get an error. To avoid error, you must use the parentheses.

Multiplication Operator

-an operator to multiply 2 or more values

-using *

Ex.

int x = 3 * 2;

System.out.println(“The product is : “ + x ); //The product is : 6

-display difference directly

Ex.

System.out.println(“The product is : “ + (3 * 2) ); //The product is : 6


System.out.println(“The product is : “ + 3 * 2 ); //The product is : 6 (NO ERROR)

Note: The * sign work with or without parentheses

Division Operator

-an operator to divide 2 or more values

-using /

Ex.

Ex.

int x = 6 / 2;

System.out.println(“The result is : “ + x ); //The result is : 3

-display difference directly

Ex.

System.out.println(“The result is : “ + (6 / 2) ); //The result is : 3


System.out.println(“The result is : “ + 6 / 2 ); //The result is : 3 (NO ERROR)
Note: The / sign work with or without parentheses

-The result of the division of two integers is an integer

-We can use casting to get a double result

-The result of the division of two doubles is a double

-The result of the division of an integer and a double is a double (implicit casting)

Modulo Operator

-the remainder of a division

-using %

-this is a very important operator as you will see later on

Ex.

4 % 2 = 0 //4 is even

5 % 2 = 1 //5 is odd

Note: We are using modulo operator to find the remainder of the division by 2 and using the result we can
conclude is the number is even or odd

Ex.

int x = 6 % 4;

System.out.println(“The result is : ” + x ); //The result is : 2

-display difference directly

Ex.

System.out.println(“The result is : “ + (6 % 4) ); //The result is : 2

System.out.println(“The result is : “ + 6 % 4 ); //The result is : 2 (NO ERROR)

Note: The % sign work with or without parentheses

OPERATOR PRECEDENCE

-this specifies what to do first, just like mathematics in school

-all operations are done from left to right in the following order:

The operations between the parentheses

The multiplication and division


The addition and subtraction

Ex.

2 * (1 + 5) - 12 / (4 + 2)

2 * (6) - 12 / (6)

12 - 2

10

Relational Operators in Java

What are Relational Operators?

-they are operators that used to make comparisons in our program.

So suppose that we have two values for example two numbers or two strings, anything, and we want to make
some comparisons

This is used to check if the two operand are equal, not equal, greater than, lesser than, greater than or equal,
lesser than or equal.

- Equality Operator: ==

- Inequality Operator: !=

- Greater than Operator: >

- Greater than or Equal Operators: >=

- Less than: <

- Less than or Equal Operators: <=

Note: All these operators produce a Boolean value when used.

Equal Operator: ==

-Tests if two expressions are equal

-checks if the values of two operands are equal or not, if yes then condition becomes true

Ex.

boolean b1 = (1 == 1); //true

boolean b2 = (2 == 3); //false

Boolean b3 = (2 + 3) == (6 - 1); //true

Note: The parentheses are optional. Just used them to make the code more clear and readable
Not Equal Operators !=

-Tests if two expressions are not equal

-checks if the values of two operands are equal or not, if values are not equal then the condition becomes
true.

Greater than >

Checks of the values of the left operand is greater than the value of right operand, if yes then the condition
becomes true

Greater than or Equal Operators >=

Checks of the values of the left operand is greater than or equal to the value of right operand, if yes then the
condition becomes true

Less than <

Checks of the values of the left operand is less than the value of right operand, if yes then the condition
becomes true

Less than or Equal Operators <=

Checks of the values of the left operand is less than or equal to the value of right operand, if yes then the
condition becomes true.

Kung gusto pa nating magdagdag or magcheck pa ng another condition using and, or, not, pwede nating
gamitin yung Logical Operators.

Logical Operators

- is a symbol or word used to connect two or more expressions such that the value of the compound
expression produced depends only on that of the original expressions and on the meaning of the operator.
Common logical operators include AND, OR, and NOT.

Logical AND Operator &&

If both operands are non-zero, then the condition becomes true.

Logical OR Operator ||

If any of the two operands are non-zero, then the condition becomes true.

Logical NOT Operator !

Use to reserves the logical state of its operand. If a condition is true then Logical NOT operator will make
false.

You might also like