**Operators And Assignments**

**Operators And Assignments**

**Increment/Decrement**

Arithmetic Operators

Concatenation

Relational Operators

Equality Operators

Bitwise Operators

Short-Circuit

instanceOf

Typecast Operator

Assignment Operator

Conditional Operator

new Operator

[ ] Operator

Operator Precedence

Evaluation Order of Java Operands

Arithmetic Operators

Concatenation

Relational Operators

Equality Operators

Bitwise Operators

Short-Circuit

instanceOf

Typecast Operator

Assignment Operator

Conditional Operator

new Operator

[ ] Operator

Operator Precedence

Evaluation Order of Java Operands

**Increment And Decrement Operator**

We can apply increment and decrement only for variables but not for constant values.

**Example:**

1) int x=4;

2) int y = ++4; // Compile Error: unexpected type

// required: variable

// found: value

Nesting of increment and decrement operators is not allowed otherwise we will get compile time error.

**Example:**

We can't apply increment and decrement operators for the final variables.

**Example:**

We can apply increment and decrement operators for every primitive data type except Boolean.

**Example:**

**Difference between b++ and b=b+1:**

**Example:**

**Example:**

**Example:**

**Example:**

Whenever we are performing any arithmetic operations (+, -, *, %, /) between two variables a and b, the result type is always infinity.

**Arithmetic Operators**

The arithmetic operators are (+, -,*, /, %)If we are applying any arithmetic operators between two variables a and b, the result type is always,

In case of the integral arithmetic (int, short, long, byte) there is no way to represent infinity. Hence, if the infinity is the result we will always get ArithmeticException(AE: / by zero)

**Example:**

But in case of floating point arithmetic (float and double) there is always a way to represent infinity. For this float and double classes contain the following two constants.

Positive -Infinity = Infinity

Negative -Infinity = -Infinity

Hence, in the case of floating point arithmetic we won't get any ArithmeticException.

**Example:**

**Not as Number(NaN):**

In integral arithmetic (int, short, long, byte) there is no way to represent undefined results. Hence, if the result is undefined we will get ArithmeticException in case of integral Arithmetic.

**Example:**

**Conclusion about ArithmeticException:**

It is a RuntimeException but not compile time error.

Possible only in Integral Arithmetic(int, byte, short, char) but not floating point Arithmetic (float, double).

The only operators which cause ArithmeticException are '/ and %'.

**String Concatenation Operator (+)**

The only overloaded operator in java is '+' operator.Sometimes it acts as arithmetic addition operator and sometimes acts as string arithmetic operator or string concatenation operator.

**Example:**

Here System.out.println() is evaluated from left to right.

**Example:**

### **Relational Operator**

The relational operators are (>, < ,>=, <=)We can apply relational operator for every primitive data type except boolean.

**Example:**

1) 10>20; //false

2) 'a'< 'b'; // true

3) 10>=10.0; //true

4) 'a'< 125; //true

5) true<=true; C.E. operator<=can't be applied to

6) true<false; boolean, boolean.

**Example:**

1) "gaurav"< "gaurav"; C.E. operator<=can't be

2) "snehal" < "Snehal123"; applied to String, string.

Nesting of relational operators are not allowed to apply.

**Example:**

**Example:**

** Equality Operators (= =, ! =): **

These are = =, ! =We can apply equality operators for every primitive type including boolean type.

**Example:**

1) 10 == 10.0 //true

2) 'a'== 97 // true

3) true== false //false

4) 10.5==12.3 // false

2) 'a'== 97 // true

3) true== false //false

4) 10.5==12.3 // false

We can apply equality operators even for object reference also.

For the two object references and r1 and r2 and r1= = r2 returns true iff both r1 and r2 are pointing to the same object. i.e. equality operators (= = ) is always meant for reference / address comparison.

**Example:**

To apply equality operators between the object references compulsory these should be some relationship between argument types.

Either parent to child or child to parent or same type] otherwise we will get Compile Error: Incompatible type.

**Example:**

For any object reference r, if r is pointing to any object

Otherwise r contains null value. So,

**Note:**

In general , == operator meant for reference comparison where as

.equal() method means for content comparision.

** instanceOf Operators**

By using this operator we can check whether the given object is of a particular type or not.

**Syntax:**

r

**instanceOf**x

any reference type class/interface

**Example:**

**Example**:

To use instanceOf operator, compulsory there should be some relationship between argument type, otherwise we will get compile time error saying Inconvertable type.

**Example:**

Whenever we are checking parent object is of child type then, we will get false as output.

**Example:**

For any class or interface x, null instanceof x always return "false".

**Example**:

**Bitwise Operator**

**& - AND**

If both operands are true then result is true.

**| - AND**

If at least one operands true then result is true.

**^- AND**

If both operands are different then result is true.

**Example**:

**Summary:**

**&, |, ^ :**We can apply for both integral and Boolean types.

**~ :**We can apply only for integral types but not for Boolean types.

**! :**We can apply only for Boolean types but not for integral types.

**Short Circuit Operator(&&,||)**

We can apply these operators just to improve performance of the system.These are exactly same as normal bitwise operators &,| except the following difference.

**X&&y**: y will be evaluated iff x is true.

**X||y**: y will be evaluated iff x is false.

**Example:**

**Type Casting Operator**

1) Implicit type casting2) Explicit type casting

**1) Implicit type casting:**

Compiler is responsible to perform this type casting.

This Typecasting is required whenever we are assigning smaller datatype value to the bigger data type variable.

It is also known as

**widening (or) upcasting**.

No loss of information in this type casting.

The following are various possible implicit type casting.

1) byte

2) short

3) int

4) long

5) float

6) double

7) char

**Example:**

**1) Explicit type casting:**

Programmer is responsible to perform this typecasting.

It is required whenever we are assigning bigger datatype value to the smaller datatype variable.

It is also known as "Narrowing or down casting".

There may be a chance of loss of information in this type casting.

The following are various possible conversion where explicite typecasting is required.

The following are various possible implicit type casting.

byte

short

int

long

float

double

char

**Example:**

**Example:**

**Assignment Operator**

There are two types of primitive type casting:1) Simple Assignment operator

2) Chained assignment operator

3) Compound assignment operator

1)

**Simple assignment operator:**

**Example:**int x=10;

2)

**Chained assignment operator:**

**Example:**int a,b,c,d;

a=b=c=d=20;

We can't perform chained assignment at the time of declaration

**Example:**

**3) Compound assignment operator:**

Sometimes we can mix assignment operator with some other operator to form compound assignment operator.

**Example:**

In compound assignment operators the required typecasting will be performed automatically by the compiler.

**Example:**

**Conditional Operator**

The only ternary operator available in java is a ternary operator (or) conditional operator.**Example:**

Nesting of conditional operator is possible.

**Example:**

**Example:**

**new operator:**

We can use this operator for creation of object.In java there is no delete operator because distraction of useless object is responsibilt of garbage collector.

**[ ] operator**

We can use this operator for declaring and creating array.**Operator Precedence:**

1) **Unary operators:**

[],x++,x--;

++x;--x,~,!;

New,

2)

**Arithmetic operators:**

*,/,%

+,-

3)

**Shift operators:**

>>>,>>,<<

4)

**Comparision operators:**

<,<=,>,>=,instanceof

5)

**Equality operators:**

=,!=

6)

**Bitwise operators:**

&,^,|

7)

**Short circuit operators:**

&&,||

8)

**Conditional operators:**

?:

9)

**Assignment operators:**

+=,-+,%=,*=,/=,&=,|=,^=,>>=,>>>=,<<=

**Evaluation of order of operators:**

There is no precedence for operands before applying any operator. All operands will be evaluated from left to right.

**Example**:

**Output:**

1

2

3

4

5

6

10

2

3

4

5

6

10

**Explanation:**

1+2*3+$*5/6

1+6+4*5/6

1+6+3

7+3

=10

**Example:**

**Output:**

-126