Java Operators - Operators in Java
Operators in Java
Java Operators
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
public class AssignmentOperator { public static void main(String args[]) { // VARIABLE DECLARATION int a; System.out.println("Assignment Operator Example"); // ASSIGNMENT a = 10; // PRINTS TEN System.out.println("Value of a After Assignment : " + a); } }
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 Subtraction |
* | for Multiplication |
/ | for Division |
% | 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
public class ArithmeticOperators { public static void main(String args[]) { System.out.println("Java Arithmetic Operators Example"); // Assignments System.out.println("Assignment (20+10) = " + (20+10)); // Subtraction System.out.println("Subtraction (20-10) = " + (20-10)); // Multiplication System.out.println("Multiplication (20X10) = " + (20*10)); // Disivion System.out.println("Division (20/10) = " + (20/10)); // Modulo Division System.out.println("Modulo Division (20%12) = " + (20%12)); } }
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
public class ArithmeticAssignmentOperators { public static void main(String args[]) { // VARIABLE DECLARATION AND ASSIGN A VALUE int a = 1, b = 7; a +=10; System.out.println("Arithmetic Assignment Operators Example"); System.out.println("Addition : " + a); b -= 2; System.out.println("Subtraction : " + b); // YOU CAN ALSO DECLARE A VARIABLE AT ANY PLACE float c; c = 3; c *= 5; System.out.println("Multiplication : " + c); double d = 20; d /= 10; System.out.println("Division : " + d); } }
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
public class LogicalOperators { public static void main(String args[]) { // VARIABLES DECLARATION SAME TYPE WITH COMMA OPERATOR boolean T = true, F = false; System.out.println("Logical Operators Example"); // TRUE AND FALSE = FALSE System.out.println("T AND F : " + (T && F)); // TRUE OR FALSE = TRUE System.out.println("T OR F : " + (T || F)); // NOT TRUE = FALSE System.out.println("NOT T : " + (!T)); } }
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
public class RelationalOperators { public static void main(String args[]) { System.out.println("Java Relational Operators Example"); System.out.println("1 Equal to 1 : " + (1==1)); System.out.println("1 Not Equal to 1 : " + (1!=1)); System.out.println("3 Greater Than 2 : " + (3>2)); System.out.println("1 Less Than 2 : " + (1<2)); System.out.println("3 Greater Than or Equal to 2 : " + (3>=2)); System.out.println("1 Less Than or Equal to 2 : " + (1<=2)); } }
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
public class IncrementDecrementOperators { public static void main(String args[]) { int a = 10, b = 20, c = 30, d = 40; System.out.println("Java Increment and Decrement Operators Example"); // VALUE IS 10 AFTER THIS STATEMENT a VALUE IS 11 System.out.println("Post Increment : " + (a++)); System.out.println("After Post Increment : " + a); // VALUE IS 20 AFTER THIS STATEMENT b VALUE IS 19 System.out.println("Post Decrement : " + (b--)); System.out.println("After Post Decrement : " + b); // VALUE IS 31 System.out.println("Pre Increment : " + (++c)); // VALUE IS 39 System.out.println("Pre Decrement : " + (--d)); } }
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
public class BitwiseOperators { public static void main(String args[]) { int a = 10, b = 20; System.out.println("Java Bitwise Operators Example"); System.out.println("a & b : " + (a & b)); System.out.println("a | b : " + (a | b)); System.out.println("a ^ b : " + (a ^ b)); System.out.println("~a : " + (~a)); System.out.println("a << b : " + (a << b)); System.out.println("a >> b : " + (a >> b)); System.out.println("a >>> b : " + (a >>> b)); // There is no unsigned left shift operator } }
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
public class ConditionalOperator { public static void main(String args[]) { int a = 10, b = 20, c, d; // ASSIGN A VALUE TO c, d VARIABLES AT A TIME c=d=0; System.out.println("Java Conditional (Ternary) Operator Example"); // c VALUE IS 20 c = (a > b ? 100 : b); // d VALUE IS 0 d = (a == 10 ? 0 : 15); System.out.println("Value of c : " + c); System.out.println("Value of d : " + d); } }
Special Operators in Java
Java provides some special operators as given below.
- instanceof Operator
- . Dot Operator
- new Operator
instanceof 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.
new Operator
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.