In java operators are used to perform a specific operations using variables and its values.
Java operators are devided into many types:
Arithmetic operators are used to perform common mathematical operations.
Operator | Name | Description | Example |
---|---|---|---|
+ | Addition | Adds together two values | x + y |
- | Subtraction | Subtracts one value from another | x - y |
* | Multiplication | Multiplies two values | x * y |
/ | Division | Divides one value by another | x / y |
% | Modulus | Returns the division remainder | x % y |
Example Programs
Addition example:
public class Addition {
public static void main(String[] args) {
int x = 5;
int y = 3;
int z = x+y;
System.out.println(z);
}
}
Subtraction example:
public class Subtraction {
public static void main(String[] args) {
int x = 5;
int y = 3;
int z = x-y;
System.out.println(z);
}
}
Multiplication example:
public class Multiplication {
public static void main(String[] args) {
int x = 5;
int y = 3;
int z = x*y;
System.out.println(z);
}
}
Division example:
public class Division {
public static void main(String[] args) {
int x = 5;
int y = 3;
int z = x/y;
System.out.println(z);
}
}
Modulus example:
public class Modulus {
public static void main(String[] args) {
int x = 5;
int y = 3;
int z = x%y;
System.out.println(z);
}
}
The Java unary operators require only one operand. Unary operators are used to perform various operations i.e.:
Operator | Name | Description | Example |
---|---|---|---|
++ | Increment | This operator is use to increment the operand (value) by 1 | x++ |
-- | Decrement | This operator is use to decrement the operand (value) by 1 | --y |
~ | Nagation | This operator is use to display the opposite value | ~x |
Example Programs
Increment example:
public class Increment {
public static void main(String[] args) {
int x = 5;
x++;
System.out.println(x);
}
}
Decrement example:
public class Decrement {
public static void main(String[] args) {
int x = 5;
--x;
System.out.println(x);
}
}
Nagation example:
public class NagationExample{
public static void main(String args[]){
int a=10;
int b=-10;
boolean c=true;
boolean d=false;
System.out.println(~a);//-11 (minus of total positive value which starts from 0)
System.out.println(~b);//9 (positive of total minus, positive starts from 0)
System.out.println(!c);//false (opposite of boolean value)
System.out.println(!d);//true
}}
Comparison operators are used to compare two values:
Operator | Name | Description | Example |
---|---|---|---|
== | Equal To | This operator is use to check the equality of two numbers | x == y |
!= | Not Equal To | This operator is use to check the not equality of two numbers | x != y |
> | Greater Than | This operator is use to check the greater value between two numbers | x > y |
< | Less Than | This operator is use to check the less value between two numbers | x < y |
>= | Greater Than Equal To | This operator is use to check the greater than or equal to value between two numbers | x >= y |
<= | Less Than Equal To | This operator is use to check the less than or equal to value between two numbers | x <= y |
Example Programs
Equal to example:
public class EqualTo {
public static void main(String[] args) {
int x = 5;
int y = 3;
boolean result = (x == y); // returns false because 5 is not equal to 3
System.out.println(result);
}
}
Not Equal example:
public class NotEqual {
public static void main(String[] args) {
int x = 5;
int y = 3;
boolean result = (x != y); // returns true because 5 is not equal to 3
System.out.println(result);
}
}
Greater Than example:
public class GreaterThan {
public static void main(String[] args) {
int x = 5;
int y = 3;
boolean result = (x > y); // returns true because 5 is greater than 3
System.out.println(result);
}
}
Less Than example:
public class LessThan {
public static void main(String[] args) {
int x = 5;
int y = 3;
boolean result = (x < y); // returns false because 5 is less than 3
System.out.println(result);
}
}
Greater Than Or Equal To example:
public class GreaterThanOrEqualTo {
public static void main(String[] args) {
int x = 5;
int y = 3;
boolean result = (x >= y); // returns true because 5 is greater, or equal, to 3
System.out.println(result);
}
}
Less Than Or Equal To example:
public class LessThanOrEqualTo {
public static void main(String[] args) {
int x = 5;
int y = 3;
boolean result = (x <= y); // returns false because 5 is less than, or equal, to 3
System.out.println(result);
}
}
Logical operators are used to determine the logic between variables or values:
The logical operators doesn't check the second condition if the first condition is true. In case of first condition false then only it will check for the second condition.
Operator | Name | Description | Example |
---|---|---|---|
&& | Logical and | Return true if both the statements are true | x < 5 && x < 10 |
|| | Logical or | Return true if one statement is true | x < 5 || x < 4 |
! | Logical not | This will reverse the result, return false if the result is true | !(x < 5 || x < 4) |
Example Programs
Logical and example:
public class LogicalAnd {
public static void main(String[] args) {
int x = 5;
System.out.println(x > 3 && x < 10); // returns true because 5 is greater than 3 AND 5 is less than 10
}
}
Logical or example:
public class LogicalOr {
public static void main(String[] args) {
int x = 5;
System.out.println(x > 3 || x < 4); // returns true because one of the conditions are true (5 is greater than 3, but 5 is not less than 4)
}
}
Logical not example:
public class LogicalNot {
public static void main(String[] args) {
int x = 5;
System.out.println(!(x > 3 && x < 10)); // returns false because ! (not) is used to reverse the result
}
}
Assignment operators are used to assign values to variables.
Check below example 10 values is getting assigned to x variable:
int x = 10;
The addition assignment operator (+=) adds a value to a variable:
int x = 10;
x += 5;
The bitwise operator is also used to perform the logical operations which always check both the condition and which needs to be true to execute the condition.
Bitwise operators are three types
Operator | Name | Description | Example |
---|---|---|---|
& | bitwise AND | Return true if both the statements are true | x < 5 & x < 10 |
^ | bitwise exclusive OR | Return true if one statement is true | x < 5 ^ x < 4 |
| | bitwise inclusive OR | This will reverse the result, return false if the result is true | !(x < 5 | x < 4) |
Java ternary operator is the only conditional operator that takes three operands.
Syntax:
variable = Expression1 ? Expression2: Expression3
Example Program
/ Java program to find largest among two
// numbers using ternary operator
import java.io.*;
class Ternary {
public static void main(String[] args)
{
// variable declaration
int n1 = 5, n2 = 10, max;
System.out.println("First num: " + n1);
System.out.println("Second num: " + n2);
// Largest among n1 and n2
max = (n1 > n2) ? n1 : n2;
// Print the largest number
System.out.println("Maximum is = " + max);
}
}