# The Arithmetic Operators

Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. The following table lists the arithmetic operators:

Assume integer variable a = 10 and variable b = 20, then:

 Operator Description Example + Addition - Adds values on either side of the operator a + b will give 30 - Subtraction - Subtracts right hand operand from left hand operand a - b will give -10 * Multiplication - Multiplies values on either side of the operator a * b will give 200 / Division - Divides left hand operand by right hand operand b / a will give 2 % Modulus - Divides left hand operand by right hand operand and returns remainder b % a will give 0 ++ Increment - Increases the value of operand by 1 b++, ++b gives 21 equivalent to b = b +1; -- Decrement - Decreases the value of operand by 1 b-- , --b gives 19 Equivalent to b = b-1;

# The Shorthand Assignment Operators:

These are the shorthand assignment operators supported in Java:

Examples

 Operator Description Example += Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand c += a is same asc = c + a -= Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand c -= a is same as c = c - a *= Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand c *= a is same as c = c * a /= Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand c /= a is same as c = c / a %= Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand c %= a is same as c = c % a

#### Points to remember!

• The result of any division operation is a double type
• Use shorthand notations where ever possible
• Java uses the standard PEMDAS (Parenthesis, Exponents, Multiplication and Division, Addition and Subtraction) order when evaluating several operators in the same expression. When there are multiple instances of the same precedence, Java reads from left to right.

# The Relational Operators

Following are the relational operators supported in Java language. The result of a relational operator is a boolean value, true or false. The operands for this operator can be any variable or an expression.

Assume variable a = 10 and variable b = 20, then:

Examples

 Operator Description Example == Checks if the values of two operands are equal or not, if yes then condition becomes true. (a == b) is false. != Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. (a != b) is true. > Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (a > b) is false. < Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (a < b) is true. >= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (a >= b) is false. < = Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (a < = b) is true.

# The Logical Operators

Logical operators are applied to booleans only. Either it is a variable which holds a boolean value or a relational expression which evaluates to a boolean value. The following table lists the logical operators:

Assume Boolean variables a = true and variable b = false, then:

Examples

 Operator Description Example && Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. (a && b) is false. || Called Logical OR Operator. If any of the two operands are non-zero, then the condition becomes true. (a || b) is true. ! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. !(a && b) is true.

#### Using Scanner

Scanner is a very useful class present in java.util package used to obtain input from the user. You have so far been able to pass input to your program as a program argument. When you send input this way, you have to run your program every time you want to change your argument. Using Scanner, you can send input to your program, when the program is running.

To create an object of Scanner class, you generally pass System.in argument, which represents the standard input stream.

Here is an example of using Scanner:

``````import java.util.Scanner; //notice the import statement for scanner!

public class ScannerDemo{

public static void main(String[] args){

Scanner sc = new Scanner(System.in);

String name = sc.nextLine(); // get a String value
System.out.println("Hi " + name);

short age = sc.nextInt(); // get an short value
System.out.println("Glad to know you are " + age + " years old!");