- Java Tutorial
- Your first Java Program
- Java and JVM Internals
- Java Keywords and Best Practices
- Java Variables
- Data Types and Literals
- Operators in Java
- Operators in Java Continued
- Operator Precedence and Operators Example
- Java Strings and Command Line Arguments
- Control Statements in Java
- Java looping statements
- Java arrays
- Java Classes and Objects
- Java garbage collection and access modifiers
- Java class methods
- Java class inheritance
- Java abstract classes and abstract methods
- Java final classes, final methods and final data members
- Access superclass constructor and superclass members
- Method Overriding and Run Time Polymorphism
- Multilevel Inheritance and Referencing
- Java interfaces
- Java Interface Variables and Default Methods
- Java Interface References and Extending Interfaces
- Java Exception Handling
- Multiple catch blocks and nested try blocks
- Methods in Throwable, Java’s Built-in Exceptions, and Custom Exception
- Java throw, finally and throws keywords
- Java I/O – built in I/O classes

An operator is a special symbol which performs a specific task. There are different types of operators Java provides, they are

- Arithmetic Operators
- Relational Operators
- Bitwise Operators
- Logical Operators
- Assignment and Shorthand Operators
- Special Operators

From childhood we are practice algebra with different mathematical operations such as addition, subtraction, multiplication and division etc. Java also provides operators to perform these operations, they are given in below table. The below table also shows result of these operations on two variables X and Y. Where X is 5 and Y is 45

Operator Name | Symbol | Description | Example |
---|---|---|---|

Addition | + | Adds two values | X + Y gives 50 |

Subtraction | – | Subtracts second value from first value | X – Y gives -40 |

Multiplication | * | Multiplies two values | X * Y gives 225 |

Division | / | Divides first value by second value give quotient | Y / X gives 9 |

Modulus | % | Divides first value by second value give remainder | Y % X gives 0 |

Increment | ++ | Increases the value by 1 | X++ gives 46 |

Decrement | — | Decreases the value by 1 | X– gives 44 |

Increment and decrement usage can be in two ways. First one is postfix, operator exists after variable. For example is, X++. And second is prefix, operator exists before variable. For example ++X.

There is a difference between these two operations when used with other operations. Consider below example for postfix.

int x = 10; int y = 0; y = x++;

In above example at postfix statement, two operations are happening. One is assignment from x to y and other is increment of x. In this case we used postfix so assignment occurs first and then increment will happen. It means y will contain 10 and x will contain 11.

Consider below example for prefix.

int x = 10; int y = 0; y = ++x;

In above example at prefix statement, two operations are happening. One is assignment from x to y and other is increment of x. In this case we used prefix so increment will happen first and then assignment occurs. It means x contains 11 and y contains 11.

Below program gives all arithmetic operations and their result.

/* This is a simple Java program about Arithmetic Operators. Call this file KH_ArithmeticOperators.java. */ public class KH_ArithmeticOperators { // A Java program begins with a call to main(). public static void main(String args[]) { int x = 45; int y = 5; System.out.println("Addition x + y = " + (x+y)); System.out.println("Subtraction x - y = " + (x-y)); System.out.println("Multiplication x * y = " + (x*y)); System.out.println("Division x / y = " + (x/y)); System.out.println("Modulus x % y = " + (x%y)); //Postfix int z = 0; z = y++; System.out.println("Postfix"); System.out.println("Value of y = " + y); System.out.println("Value of z = " + z); //Prefix z = 0; y = 5; z = ++y; System.out.println("Prefix"); System.out.println("Value of y = " + y); System.out.println("Value of z = " + z); } }

Download the code Run the code

Output:

Addition x + y = 50 Subtraction x - y = 40 Multiplication x * y = 225 Division x / y = 9 Modulus x % y = 0 Postfix Value of y = 6 Value of z = 5 Prefix Value of y = 6 Value of z = 6