Operators in java - Passionate Geekz

Breaking

Where you can unleash your inner geek.

Saturday, 25 January 2020

Operators in java

Java Operators

Operators are used to perform operations on variables and values.

The value is called an operand, while the operation (to be performed between the two operands) is defined by an 

public class MyClass

{
public static void main(String[] args)

{
int x = 100 + 50;
System.out.println(x);
}
}

Although the + operator is often used to add together two values, like in the example above, it can also be used to add together a variable and a value, or a variable and another variable:

public class MyClass

{
public static void main(String[] args)

{
int sum1 = 100 + 50;
int sum2 = sum1 + 250;
int sum3 = sum2 + sum2;
System.out.println(sum1);
System.out.println(sum2);
System.out.println(sum3);
}
}

Java divides the operators into the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators

1).Arithmetic Operators

Arithmetic operators are used to perform common mathematical operations.

OperatorNameDescriptionExample 
+AdditionAdds together two valuesx + y 
SubtractionSubtracts one value from anotherx – y 
*MultiplicationMultiplies two valuesx * y 
/DivisionDivides one value from anotherx / y 
%ModulusReturns the division remainderx % y 
++IncrementIncreases the value of a variable by ++x 
DecrementDecreases the value of a variable by 1–x

2).Java Assignment Operators

Assignment operators are used to assign values to variables.

In the example below, we use the assignment operator (=) to assign the value 10 to a variable called x:

public class MyClass

{
public static void main(String[] args)

{
int x = 10;
System.out.println(x);
}
}

The addition assignment operator (+=) adds a value to a variable:

public class MyClass

{
public static void main(String[] args)

{
int x = 10;
x += 5;
System.out.println(x);
}
}

A list of all assignment operators:

OperatorDescriptionExample
=Simple assignment operator. Assigns values from right side operands to left side operand.C = A + B will assign value of A + B into C
+=Add AND assignment operator. It adds right operand to the left operand and assign the result to left operand.C += A is equivalent to C = C + A
-=Subtract AND assignment operator. It subtracts right operand from the left operand and assign the result to left operand.C -= A is equivalent to C = C – A
*=Multiply AND assignment operator. It multiplies right operand with the left operand and assign the result to left operand.C *= A is equivalent to C = C * A
/=Divide AND assignment operator. It divides left operand with the right operand and assign the result to left operand.C /= A is equivalent to C = C / A
%=Modulus AND assignment operator. It takes modulus using two operands and assign the result to left operand.C %= A is equivalent to C = C % A
<<=Left shift AND assignment operator.C <<= 2 is same as C = C << 2
>>=Right shift AND assignment operator.C >>= 2 is same as C = C >> 2
&=Bitwise AND assignment operator.C &= 2 is same as C = C & 2
^=bitwise exclusive OR and assignment operator.C ^= 2 is same as C = C ^ 2
|=bitwise inclusive OR and assignment operator.C |= 2 is same as C = C | 2

3).Java Comparison Operators

Comparison operators are used to compare two values:

OperatorNameExample 
==Equal tox == y 
!=Not equalx != y 
>Greater thanx > y 
<Less thanx < y 
>=Greater than or equal tox >= y 
<=Less than or equal tox <= y

4).Java Logical Operators

Logical operators are used to determine the logic between variables or values:

OperatorNameDescriptionExample 
&& Logical andReturns true if both statements are truex < 5 &&  x < 10 
|| Logical orReturns true if one of the statements is truex < 5 || x < 4 
!Logical notReverse the result, returns false if the result is true!(x < 5 && x < 10)

No comments:

Post a Comment