Java Basic Operators

Published by Admin on

Java Basic Operators - intechnologies.in

Java provides a set of operators to manipulate variables.

  1. Arithmetic Operators
  2. Relational Operators
  3. Bitwise Operators
  4. Logical Operators
  5. Assignment Operators
  6. Miscellaneous Operators

1. Arithmetic Operators

In mathematical expression we are used arithmetic operator in the same way as they are used in algebra.

Arithmetic operators list :−

Operator SymbolOperator NameDescriptionExample
+AdditionAdds(+) values on either side of the operator.A + B
SubtractionSubtracts(-) right-hand operand from left-hand operand.A – B
*MultiplicationMultiplies(*) values on either side of the operator.A * B
/DivisionDivides left-hand operand by right-hand operand.B / A
%ModulusDivides left-hand operand by right-hand operand and get remainder.B % A
++IncrementIncreases the value of the operand by 1.B++
DecrementDecreases the value of the operand by 1.B–

2. Relational Operators

Relational operators determine the relationship that one operand has to another operand. Relational operators evaluates the relation between the two operations and returns true if the relation exists else false.

Relational Operators list :-

Operator SymbolOperator NameDescriptionExample
==equal toChecks if the values of two operands are equal, if yes then condition becomes true otherwise returns false.(A == B)
!=not equal toChecks if the values of two operands are equal or not, if values are not equal then condition becomes true otherwise returns false.(A != B)
>greater thanChecks if the value of left operand is greater than(>) the value of right operand, if yes then condition becomes true.(A > B)
<less thanChecks if the value of left operand is less than(<) the value of right operand, if yes then condition becomes true.(A < B)
>=greater than or equal toChecks if the value of left operand is greater than or equal to(>=) the value of right operand, if yes then condition becomes true.(A >= B)
<=less than or equal toChecks if the value of left operand is less than or equal to(<=) the value of right operand, if yes then condition becomes true.(A <= B)

3. Bitwise Operators

Java defines many bitwise operators, which can be applied to the integer types, long, int, short, char, and byte.

Bitwise operators list :-

Operator SymbolOperator NameDescriptionExample
&bitwise andBinary AND(&) Operator copies a bit to the result if it exists in both operands.(A & B)
|bitwise orBinary OR(|) Operator copies a bit if it exists in either operand.(A | B)
^bitwise XORBinary XOR(^) Operator copies the bit if it is set in one operand but not both.(A ^ B)
~bitwise complimentBinary Ones Complement Operator is unary and has the effect of ‘flipping’ bits.(~A )
<<left shiftBinary Left Shift(<<) Operator. The left operands value is moved left by the number of bits specified by the right operand.A << 2
>>right shiftBinary Right Shift(>>) Operator. The left operands value is moved right by the number of bits specified by the right operand.A >> 2
>>>zero fill right shiftShift right zero fill(>>>) operator. The value of the left operands is moved to the right by the number of bits specified by the right operand and shifted values are filled up with zeros.A >>>2

4. Logical Operators

Logical Operators list :-

Operator SymbolOperator NameDescriptionExample
&&logical andCalled Logical AND(&&) operator.The condition become true only if both the operands are non-zero.(A && B)
||logical orCalled Logical OR(||) Operator. If any of the two operands are non-zero, the condition becomes true.(A || B)
!logical notCalled Logical NOT(!) Operator. Use to reverses the logical position of its operand. If a condition is true then Logical NOT(!) operator will make false.!(A && B)

5. Assignment Operators

Assignment Operators list :-

Operator SymbolDescriptionExample
=Simple assignment(=) operator. Specifies the values from right side operands to left side operand.C = A + B
+=Add and assignment(+=) operator. This will add right operand to the left operand and assigns the result to left operand.C += A
-=Subtract and assignment(-=) operator. This will subtract right operand from the left operand and assign the result to left operand.C -= A
*=Multiply and assignment(*=) operator. This will multiplies right operand with the left operand and assigns the result to left operand.C *= A
/=Divide and assignment(/=) operator. This will divides left operand with the right operand and assigns the result to left operand.C /= A
%=Modulus and assignment(%=) operator. It takes modulus using two operands and assigns the result to the left operand.C %= A
<<=Left shift and assignment (<<=) operator.C <<= 2
>>=Right shift and assignment (>>=) operator.C >>= 2
&=Bitwise and assignment(&=) operator.C &= 2
^=bitwise exclusive OR and assignment(^=) operator.C ^= 2
|=bitwise inclusive OR and assignment(|=) operator.C |= 2

6. Miscellaneous Operators

There are a few other operators supported by the Java Language.

a. Conditional/Ternary Operator ( ? : )

Conditional operator is also known as the (?:) ternary operator. This operator has three operands and is used to evaluate Boolean expressions.

Example

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

b. instanceof Operator

This operator is only used for object reference variables. The operator checks whether the object is of a particular type (interface type or class type).

Example

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

c. Precedence of Java Operators

The operator’s precedence determines the grouping of the terms in the expression. This affects how an expression is evaluated. Some operators are higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator.

Precedence of Java Operators list :-

CategoryOperatorAssociativity
Postfixexpression++ expression–Left to right
Unary++expression –-expression +expression –expression ~ !Right to left
Multiplicative* / %Left to right
Additive+ –Left to right
Shift<< >> >>>Left to right
Relational< > <= >= instanceofLeft to right
Equality== !=Left to right
Bitwise AND&Left to right
Bitwise XOR^Left to right
Bitwise OR|Left to right
Logical AND&&Left to right
Logical OR||Left to right
Conditional?:Right to left
Assignment= += -= *= /= %= ^= |= <<= >>= >>>=Right to left

Question :-

  • What is Arithmetic Operators in Java?
  • What is Logical Operators in Java?
  • What are Compound Assignment Operators?
  • What is the difference between ++x and x++ under increment operators?
  • What are Bitwise Logical Operators?


0 Comments

Leave a Reply

Facebook
Twitter
Pinterest
LinkedIn
Instagram
Follow by Email
YouTube
Telegram
RSS
WhatsApp