Java Operators - Operators in Java
Operators in Java
The special symbols +, -, *, /, % are called operators in Java. Java provides a rich set of operators to manipulate variables. All the Java operators classified into the following groups.
- Assignment Operators
- Arithmetic Operators
- Logical Operators
- Relational Operators
- Bitwise Operators
Java Operators are used to manipulate primitive data types. Java operators can be classified as Unary, Binary and Ternary i.e. taking one, two or three arguments. A Unary operator may appear before its argument or after its argument. A Binary or Ternary operator appears between its arguments.
What is Operator in Java ?
An operator in Java, is a special symbol performing specific operations on one, two or three operands and then returning a result. The operators are classified and listed according to precedence order. Java operators are generally used to manipulate primitive data types. The Java operators are classified into eight different categories: assignment, arithmetic, relational, logical, bitwise, compound assignment, conditional and type comparison operators.
What is Assignment Operator in Java ?
An Assignment operator in Java, Assign the value on its right to the operand on its left. It is denoted by the symbol =. Assignment operator is used the following ways.
- int a = 7, b;
- b = a;
Following example shows Java Assignment Operator . Save the following Java Assignment Operator example program with file name AssignmentOperator.java .
Java Assignment Operator Example
Arithmetic Operators in Java
There are eight arithmetic operators available in Java. They perform addition, subtraction, multiplication, division, modulo (or remainder), increment (or add 1), decrement (or subtract 1), and negation. + Additive operator (also used for String concatenation), - Subtraction operator, * Multiplication operator, / Division operator, % remainder operator.
What are the Arithmetic Operators used in Java ?
Java arithmetic operators are used to perform addition, subtraction, multiplication, and division. They act as basic mathematical operations. They are +, -, *, /, %.
|Following are the List of All Arithmetic Operators in Java|
|+||for Addition (Add two numbers) or Concatenation (Add two strings)|
|%||for modulo Division (Remainder)|
Following example shows Java Arithmetic Operators . Save the following Java Arithmetic Operators example program with file name ArithmeticOperators.java .
Java Arithmetic Operators Example
Java Arithmetic Assignment Operators
- += Add AND assignment operator. It adds right operand to the left operand and assign the result to left operand.
- -= Subtract AND assignment operator. It subtracts right operand from the left operand and assign the result to left operand.
- *= Multiply AND assignment operator. It multiplies right operand with the left operand and assign the result to left operand.
- /= Divide AND assignment operator. It divides left operand with the right operand and assign the result to left operand.
Following example shows Java Arithmetic Assignment Operators . Save the following Java Arithmetic Assignment Operators example program with file name ArithmeticAssignmentOperators.java .
Java Arithmetic Assignment Operators Example
Java Logical Operators
Logical operators return true or false value only i.e. the result is always a boolean data type either true or false.
- && - Logical AND : Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. It returns true when both conditions are true .
- || - Logical OR : Called Logical OR Operator. If any of the two operands are non-zero, then the condition becomes true. It returns true if at least one condition is true .
- ! - Logical NOT : 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.
What does " || " mean in Java ?
|| is a logical OR operator in Java . It means Logical-OR or simply OR like A or B. If A is true, it doesn't evaluate B. A || B in this case it is automatically true.
Explain || operator in Java
|| is a type of Logical Operator and is read as “OR OR” or “Logical OR“. This operator is used to perform “logical OR” operation, i.e. the function similar to OR gate in digital electronics.
It returns true if at least one condition is true . one important point to keep in mind for using || Logical OR is short circuit evaluation.
If A is true, it doesn't evaluate B. A || B in this case it is automatically true. One thing to keep in mind is the second condition is not evaluated if the first one is false, i.e. it has a short-circuiting effect.
Below example shows how to use && Logical AND, || Logical OR operators in Java .
Following example shows Java Logical Operators . Save the following Java Logical Operators example program with file name LogicalOperators.java .
Java Logical Operators Example
Java Relational Operators
Java Relational Operators are used to compare two values or two objects. In other words, the relational operators are used to compare 2 or more objects. There are six relational operators in Java.
- == EQUAL TO
- != NOT EQUAL TO
- > GREATER THAN
- < LESS THAN
- >= GREATER THAN OR EQUAL TO
- <= LESS THAN OR EQUAL TO
Following example shows Java Relational Operators . Save the following Java Relational Operators example program with file name RelationalOperators.java .
Java Relational Operators Example
Java Increment and Decrement Operators
- ++ Increment One
- -- Decrement One
Java Increment and Decrement Operators are two types
- Post Increment or Post Decrement
- Pre Increment or Pre Decrement
Following example shows Java Increment and Decrement Operators . Save the following Java Increment and Decrement Operators example program with file name IncrementDecrementOperators.java .
Java Increment and Decrement Operators Example
Java Bitwise Operators
Java Bitwise Operators are used to manipulate the contents of variables at the bit level. These variables must be of numeric data type (char, short, int, or long). There are seven bitwise operators. They are AND, OR, Exclusive-OR, compliment, Left-shift, Signed Right-Shift, and Unsigned Right-shift.
Following example shows Java Bitwise Operators . Save the following Java Bitwise Operators example program with file name BitwiseOperators.java .
Java Bitwise Operators Example
Java Conditional (Ternary) Operator
- ? - Question Mark
- : - colon
The Conditional operator is ternary i.e. it takes three arguments. The operator evaluates the first argument and, if true then evaluates the second argument. If the first argument evaluates to false, then the third argument is evaluated. The conditional operator is the expression equivalent of the if-else statement. The conditional expression can be nested and the conditional operator associates from right to left.
Following example shows Java Conditional ( Ternary ) Operator . Save the following Java Conditional ( Ternary ) Operator example program with file name ConditionalOperator.java .
Java Conditional (Ternary) Operator Example
Special Operators in Java
Java provides some special operators as given below.
- instanceof Operator
- . Dot Operator
- new Operator
The instanceof operator is used to know if an object is an instance of a particular class or not. This operator returns true if an object given at the left hand side is an instance of the class given at right hand side. Otherwise, it returns false.
. Dot Operator
The . Dot operator is used to access the variables and methods (to select members of a class or object instance) of a class. This dot operator is also used to access classes, packages etc.
The new keyword is a Java operator that creates the object. The new operator is followed by a call to a constructor, which initializes the new object. The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the object constructor. The new operator requires a single, postfix argument: a call to a constructor. The name of the constructor provides the name of the class to instantiate. The new operator returns a reference to the object it created. This reference is usually assigned to a variable of the appropriate type.
What are the type comparison operators in Java ?
The instanceof operator compares an object to a specified type. This operator can be used to test if an object is an instance of a class, subclass or a particular interface.