WEB DEVELOPER SITE
PHP7
 

Java Operators



Java - Operators

Operators are used to perform operations on variables and values/multiple conditions.

JAVA divides the operators in the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • The Bitwise Operators
  • Logical operators

Arithmetic Operators

JAVA arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc.

Operator Name Example Result
+ Addition x + y Sum of x and y
- Subtraction x - y Difference of x and y
* Multiplication x * y Product of x and y
/ Division x / y Quotient of x and y
% Modulus x % y Remainder of $x divided by $y
** Exponentiation x ** y Raising x to the y'th power  

Assignment Operators

The Java assignment operators are used with numeric values to write a value to a variable.

The basic assignment operator in java is "=". It means that the left operand gets set to the value of the assignment expression on the right.

Assignment Same as... Description
x = y x = y The left operand the expression is equal to right
x += y x = x + y Addition
x -= y x = x - y Subtraction
x *= y x = x * y Multiplication
x /= y x = x / y Division
x %= y x = x % y Modulus


Comparison Operators

The java comparison operators are used to compare two values (number or string):

Operator Name Example Result
== Equal x == y Returns true if x is equal to y
=== Identical x === y Returns true if x is equal to y, and they are of the same type
!= Not equal x != y Returns true if x is not equal to y
<> Not equal x <> y Returns true if $x is not equal to y
!== Not identical x !== y Returns true if x is not equal to y, or they are not of the same type
> Greater than x > y Returns true if $x is greater than $y
< Less than x < y Returns true if $x is less than $y
>= Greater than or equal to x >= y Returns true if x is greater than or equal to y
<= Less than or equal to x <= y Returns true if x is less than or equal to y

The Bitwise Operators

ThE Java have many bitwise operators such as integer types, long, int, short, char, and byte.

Operator Name Description Example
& bitwise and Binary AND Operator copies a bit to the result if it exists in both operands. 0000 1100
| bitwise or Binary OR Operator copies a bit if it exists in either operand. 0011 1101
^ bitwise XOR Binary XOR Operator copies the bit if it is set in one operand but not both. 0011 0001
~ bitwise compliment Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. (~A ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number.
<< left shift The left operands value is moved left by the number of bits specified by the right operand.
>> right shift The left operands value is moved right by the number of bits specified by the right operand.

Logical Operators

The JAVA logical operators are used to combine conditional statements.

Operator Name Example Result
and And x and y True if both x and y are true
or Or x or y True if either x or y is true
xor Xor x xor y True if either x or y is true, but not both
&& And x && y True if both x and y are true
|| Or x || y True if either x or y is true
! Not !x True if x is not true

Conditional Operator ( ? : )

Conditional operator is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions.


Syntex

variable x = (expression) ? value if true : value if false

Example
public class Test {

   public static void main(String args[]) {
      int a, b;
      a = 30;
      b = (a == 1) ? 40: 50;
      System.out.println( "Value of b is : " +  b );

      b = (a == 30) ? 40: 50;
      System.out.println( "Value of b is : " + b );
   }
}


Run example »
output
Value of b is : 50
Value of b is : 40


instanceof Operator

operator checks whether the object is of class type or interface type.


Syntex

( Object reference variable ) instanceof  (class/interface type)

Example
public class Test {

   public static void main(String args[]) {

      String name = "Jhon";

      // following will return true since name is type of String
      boolean result = name instanceof String;
      System.out.println( result );
   }
}

Run example »
Output
true