JAVA Operators
A Operator in Java is a Symbol that manipulates one or more primitive data types (arguments) to produce a result. Operators are very important part of Java programming. Below is the list of Operators available in Java.
OPERATORS | |||||||
Assignment Operators | = | ||||||
Arithmetic Operators | - | + | * | / | % | ++ | -- |
Relational Operators | > | < | >= | <= | == | != | |
Logical Operators | && | || | ! | ||||
Bit wise Operator | & | | | ^ | >> | >>> | ||
Compound Assignment Operators | += | -= | *= | /= | %= | ||
Conditional Operator | ?: |
Assignment Operator: It is the most common and simple operator that is used to assign value on its right to the operand on its left.
Arithmetic Operators: As the name suggest these are the operators that are used for performing arithmetic operations. Java provide seven arithmetic operators add, subtract, multiply, divide, mode (returns remainder), unary increment and unary decrement.
Bit Wise Operators: Bit operator in Java are use to manipulate variables at bit level. It is not recommended to use bit wise operatically in normal application programming because in makes code hard to understand. So, we are not going to discusses bit wise operators in details. In code below we shown how to use these operators.
Compound Operators:- Compound operators provide a short way or syntax to write Arithmetic and Bitwise operators. There are eleven compound operators in Java.
Below is the Java Code that shows how to use Assignment, Arithmetic, Bit wise and Compound Operator in a program. This Class doesn't actually do anything in terms of producing result, but it shows you the syntax to use these operators.
public class Operators {
//Bit Wise Operators |
Relational Operator in Java are binary operators i.e. they are used to compare two operands. There are six relational operators as shown above. Relational operator in java compare to numaric objects and results in a boolean value.
public class Relational{ results in : "+(a >= b)); System.out.println("a <= b results in : "+(a <= b)); System.out.println("a == b results in : "+(a == b)); System.out.println("a != b results in : "+(a != b)); |
Logical Operator:
Logical operator evaluates two Java boolean values and gives results in boolean value. There are three logical operator in Java. Below is the code that shows the basics of logical operators.
public class Logical{ results in : " + (a && b)); System.out.println("a || b results in : " + (a || b)); System.out.println("!a results in : " + (!a)); } |
Truth table for and or and not operations: Here P and Q are Boolean expressions and T stands for true and F stands for false.
Conditional Operator:
Conditional Operator is a ternary operator in Java which evaluates a expression and returns option1 if result is true else returns option2. Take a Look at the figure below.
public class Conditional { |