X

X

## precedence :

It refers to the condition of being considered more important than someone or something else.

# Operators :-

The operations or specific task or calculations are represented by operators.

Operands :

The objects of operations are called operands.

```Example :
int a = 5;
int b = 10;
int c = a + b;
Here, 'a' and 'b' are operands on which operation is done and '+' is operator.

```

##### Basic types of operator :-

• Unary operator :
• Operator that act on one operand is called unary operator.

• Binary operator :
• Operator that act upon two operands is called binary operator.

• Ternary operator :
• Operator that act upon three operands is called ternary operator.

#### Arithmetic operator :

They are binary operators. It requires two operands. It perform arithmetic operations.

```Example :
addition (+), subtraction (-), multiplication (*), division (/), remainder (%).
```

If we are applying any arithmetic operator between two variables a & b then the result type will be max(int, type of a, type of b).

```byte + byte result as 'int'
byte + short result as 'int'
int + long result as 'long'
long + float result as 'float'
long + double result as 'double'
double + char result as 'double'
char + char result as 'int'
```
```Example :
System.out.println(5 + 5.0); // Output : 10.5
System.out.println('a' + 'b'); // Output : 195
System.out.println(10 + 'a'); // Output : 107
```

Infinity :

In case of integral arithmetic (byte, short, int, long), there is no way to represent infinity. Hence, if infinity is the result then we will get Arithmetic Exception.

```System.out.println(10 / 0); // ArithmeticException: / by zero
```
```System.out.println(10 % 0); // ArithmeticException: / by zero
```

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

• Positive_Infinity = Infinity
• Negative_Infinity = -Infinity

Hence, in the case of floating point arithmetic we won't get any Arithmetic Exception if the result is infinity.

```System.out.println(10 / 0.0); // Output : Infinity
```
```System.out.println(-10 / 0.0); // Output : -Infinity
```

NaN (not a number) :

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

```System.out.println(0 / 0); // ArithmeticException: / by zero
```
```System.out.println(0 % 0); // ArithmeticException: / by zero
```

But in the case of floating point arithmetic (float, double), there is always a way to represent undefined results. For this Float and Double classes contains NaN constants

Hence, in the case of floating point arithmetic we won't get any Arithmetic Exception if the result is undefined.

```System.out.println(0 / 0.0); // Output : NaN
```
```System.out.println(0 % 0.0); // Output : NaN
```
```System.out.println(0.0 / 0); // Output : NaN
```
```System.out.println(0.0 % 0); // Output : NaN
```
```System.out.println(0.0 / 0.0); // Output : NaN
```
```System.out.println(0.0 % 0.0); // Output : NaN
```
```System.out.println(-0.0 / 0.0); // Output : NaN
```
```System.out.println(-0.0 % 0.0); // Output : NaN
```
```System.out.println(Math.sqrt(-4)); // Output : NaN
```
```System.out.println(10 % 0.0); // Output : NaN
```
```System.out.println(-10 % 0.0); // Output : NaN
```

For any value including NaN the following expression returns false except when we are using != (not equals to) operator then it will return true.

```System.out.println(10 > Float.NaN); // Output : false
System.out.println(10 < Float.NaN); // Output : false
System.out.println(10 == Float.NaN); // Output : false
System.out.println(10 != Float.NaN); // Output : true
System.out.println(10.0 > Float.NaN); // Output : false
System.out.println(10.0 < Float.NaN); // Output : false
System.out.println(10.0 == Float.NaN); // Output : false
System.out.println(10.0 != Float.NaN); // Output : true
System.out.println(Float.NaN == Float.NaN); // Output : false
System.out.println(Float.NaN != Float.NaN); // Output : true
```

Increment / decrement operator :

They are used to increase or decrease the existing value of operand by one only but it requires less space and also its precedence is higher than + operator.

##### Types of increment / decrement operator :-

Prefix increment or decrement operator (++a or --a)

```Example :
int a = 5;
int b = ++a;
System.out.println(b); // printing statement
Here, the value of 'a' is increased by one and then assigned to 'b'.
(// is used to write single line comments in program )

```

Postfix increment or decrement operator (a++ or a--)

```Example :
int a = 5;
int b = a++;
System.out.println(b);
Here, the value of 'a' is assigned to 'b' then it get increased by one in memory.

```

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

```int a = 1;
int b = ++1;
System.out.println(b); /* Compile time error : unexpected type
required : variable
found : value */
```

Nesting of increment / decrement operator is not allowed otherwise we will get compile time error.

```int a = 1;
int b = ++(++a);
System.out.println(b); /* Compile time error : unexpected type
required : variable
found : value */
```

We can't apply increment / decrement operator for final variables.

```final int a = 1;
a++;
System.out.println(a); // Compile time error : cannot assign a value to final variable a
```

We can apply increment / decrement operator for every primitive data types except boolean.

```double d = 10.5;
d++;
System.out.println(d); // Output : 11.5
```
```char c = 'A';
c++;
System.out.println(c); // Output : B
```
```boolean b = true;
b++;
System.out.println(b); // Compile time error : bad operand type boolean for unary operator '++'
```
```byte b = 10;
b++;
System.out.println(b); // Output : 11
```
```byte b = 10;
b = b + 1;
System.out.println(b); /* Compile time error : possible loss of precision
required : byte
found : int */
```
```byte b = 10;
byte c = 20;
byte a = b + c;
System.out.println(a); /* Compile time error : possible loss of precision
required : byte
found : int */
```

Whenever we are performing any arithmetic operation between two variables a & b then the result type will be max(int, type of a, type of b).

```byte b = 10;
b = (byte) (b + 1);
System.out.println(b); // Output : 11
```

#### Relational operator :-

They are use to compare two operands. Java provides the following relational operators :

• < (less than)

• > (greater than)

• <= (less than equals to)

• >= (greater than equals to)

We can apply relational operators for primitive data types except boolean type.

```System.out.println(10 > 20); // Output : false
System.out.println('a' < 'b'); // Output : true
System.out.println(10 > 10.0); // Output : false
System.out.println(true > false); /* Compile time error : bad operand types for binary operator '>'
first type:  boolean
second type: boolean */
System.out.println(false <= true); /* Compile time error : bad operand types for binary operator '<='
first type:  boolean
second type: boolean */
```

We can't apply relational operators for the object types.

```System.out.println("Algo" >= "Valley");
/* Compile time error : bad operand types for binary operator '>='
first type:  String
second type: String */
```

Nesting of relational operators are not allowed to apply.

```System.out.println(1 < 2 < 3);
/* Compile time error : bad operand types for binary operator '<'
first type:  boolean
second type: int */
// Here, System.out.println() will be evaluated from left to right.
```

#### Equality operator :-

They are use to compare two operands based on their equality. Java provides the following equality operators :

• == (equals to)

• != (not equals to)

We can apply relational operators for primitive data types including boolean type.

```System.out.println(10 == 10.0); // Output : true
System.out.println('a' == 65); // Output : false
System.out.println(true == true); // Output : true
System.out.println(false != false); // Output : false
System.out.println(true != false); // Output : true
```

We can apply equality operators for the object types.

```System.out.println("Algo" != "Valley"); // Output : true

System.out.println(t1 == t2); // Output : false
System.out.println(t1 == t3); // Output : true
```

To apply equality operator between two object references it is compulsory that there should be some relationship between argument types (either parent to child, child to parent, child to child or of same type) otherwise we will get compile time error.

```Object o = new Object();
String s = new String("Algo");
System.out.println(t == o); // Output : false
System.out.println(s == o); // Output : false
System.out.println(t == s); // Compile time error : incomparable types : Thread and String
```

If any object reference is compared to null then it returns false otherwise if object reference is null then it returns true.

```Object o = new Object();
String s = new String("Algo");
System.out.println(o == null); // Output : false
System.out.println(t == null); // Output : false
System.out.println(s == null); // Output : false
System.out.println("Algo" == null); // Output : false
System.out.println(null == null); // Output : true
```

In general, == (equals to) operator is meant for reference comparison whereas equals() method is meant for content comparison.

```String s1 = new String("Algo");
String s2 = new String("Algo");
System.out.println(s1 == s2); // Output : false
System.out.println(s1.equals(s2)); // Output : true
```

#### Bitwise Operator :

Bitwise operator acts upon the individual bit of their operands. Java defines several bitwise operators that can be applied to char, byte, short, int, long. The bitwise operators are :

• & (Bitwise AND) - if both operands are true then result is true.

• | (Bitwise OR) - if at least one operand is true then result is true.

• ^ (Bitwise exclusive OR) - if both operands are different then result is true.

• ~ (Bitwise complement)

• >> (Shift right)

• >>> (Shift right zero fill)

• << (Shift left)

• &= (Bitwise AND assignment)

• |= (Bitwise OR assignment)

• ^= (Bitwise exclusive OR assignment)

• >>= (Shift right assignment)

• >>>= (Shift right zero fill assignment)

• <<= (Shift left assignment)

We can apply & (Bitwise AND), | (Bitwise OR), ^ (Bitwise exclusive OR) for boolean type and integral types.

```System.out.println(true & true); // Output : true
System.out.println(true | false); // Output : true
System.out.println(false ^ true); // Output : true

System.out.println(4 & 5); // Output : 4
/* 4 => 100 (binary form)
5 => 101 (binary form)
100 & 101 => 100 (4 in decimal form) */

System.out.println(4 | 5); // Output : 5
/* 4 => 100 (binary form)
5 => 101 (binary form)
100 | 101 => 101 (5 in decimal form) */

System.out.println(4 ^ 5); // Output : 1
/* 4 => 100 (binary form)
5 => 101 (binary form)
100 ^ 101 => 001 (1 in decimal form) */
```

We can apply ~ (bitwise complement) operator only for integral types but not for boolean type.

```System.out.println(~4); // Output : -5
/* 4 => 000000.....000000100 (binary form having 32 bits i.e 4 bytes)
~4 => 111111.....111111011 (binary form having 32 bits i.e 4 bytes)
Most significant bit represent sign bit (1 => negative (-) and 0 => positive (+)).

Positive number will be represented directly in memory whereas
negative number will be represented in 2's complement :

111111.....111111011 (binary form having 32 bits i.e 4 bytes)
000000.....000000100 (1's complement)
+1 (Adding 1 to 1's complement => 2's complement)
000000.....000000101 (2's complement : 5 in decimal form represented as -5 for negative value) */

System.out.println(~true); // Compile time error :  bad operand type boolean for unary operator '~'
```

#### Short-Circuit operator (!) :

We can use short-circuit operators to improve the performance of system. It is applicable only for boolean type but not for integral types. The short-circuit operators are :

• && (and operator) - x && y => y will be evaluated if and only if x is true.

• || (or operator) - x || y => y will be evaluated if and only if x is false.

```Example 1 :
class Example
{
public static void main(String [ ] args)
{
int a = 10;
int b = 20;
if(++a > 10 && ++b > 20)
{
System.out.println("Value of a , b : "+ a + " " + b);
}
else
{
System.out.println("Value of a , b : "+ a + " " + b);
}
}
}

Output :
Value of a , b : 11 21
```
```Example 2 :
class Example
{
public static void main(String [ ] args)
{
int a = 10;
int b = 20;
if(++a > 10 || ++b > 20)
{
System.out.println("Value of a , b : "+ a + " " + b);
}
else
{
System.out.println("Value of a , b : "+ a + " " + b);
}
}
}

Output :
Value of a , b : 11 20
```

#### Boolean complement operator (!) :

We can apply boolean complement operator only for boolean type but not for integral type.

```System.out.println(!true); // Output : false
System.out.println(!false); // Output : true

System.out.println(!4); // Compile time error : bad operand type int for unary operator '!'
```

#### Assignment operator :

We can use assignment operator to assign value to variable.

There are three types of assignment operator :

• Simple assignment operator :

```int a = 10;
int b = 20, c = 30;
```

• Chained assignment operator :

```int a, b, c;
a = b = c = 10;
```

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

```int a = b = c = 10; // Compile time error :  cannot find symbol : (b & c)
```

• Compound assignment operator :

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

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

#### Conditional operator (? :) :

The only ternary operator in Java is conditional operator.

```Example :
int a = 10 , b = 20;
int c = (a > b) ? 10 : 20;
System.out.println(c); // Output : 20

int d = (a < b) ? 10 : 20;
System.out.println(d); // Output : 10

```

Nesting of conditional operator is possible.

```int a = 10 , b = 20;
int c = (a > 20) ? 10 : ((b > 10) ? 20 : 30);
System.out.println(c); // Output : 20
```

#### String Concatenation operator (+) :

The only overloaded operator in Java is '+' operator.

Sometime it acts as arithmetic addition operator and sometime it acts as String concatenation operator depending on the situation.

```Example 1 :
int a = 1, b = 2, c = 3;
String d = "Algo";
System.out.println(a + b + c + d); // Output : 6Algo
System.out.println(d + a + b + c); // Output : Algo123
System.out.println(c + d + a + b); // Output : 3Algo12
System.out.println(b + c + d + a); // Output : 5Algo1
// Here, System.out.println() will be evaluated from left to right.
// If we encounter String type then '+' operator acts as concatenation
operator otherwise it will acts as arithmetic addition operator.

```
```Example 2 :
int a = 1, b = 2;
String c = "Algo";
a = b + c;
System.out.println(a); /* Compile time error : incompatible types
required : int
found : String */

c = a + b;
System.out.println(c); /* Compile time error : incompatible types
required : String
found : int */

b = c + a;
System.out.println(b); /* Compile time error : incompatible types
required : int
found : String */

c = a + c;
System.out.println(c); // Output : 1Algo

b = a + b;
System.out.println(b); // Output : 3

// Here, expression will be evaluated from right to left.

```

#### instanceof operator :

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

```Syntax :

reference_type instanceof class_name / interface_name```
```Example :

System.out.println(t instanceof Thread); // Output : true
System.out.println(t instanceof Object); // Output : true
System.out.println(t instanceof Runnable); // Output : true

```

To apply instanceof operator, it is compulsory that there should be some relationship between argument types (either parent to child, child to parent, child to child or of same type) otherwise we will get compile time error.

```Thread t = new Thread();
System.out.println(t instanceof String); /* Compile time error :  inconvertible types
required : String
```

Whenever we are checking parent object is of child type or not then it returns false.

```Object o = new Object();
System.out.println(o instanceof String); // Output : false
```

For any class or interface, null instanceof class_name / interface_name will return false.

```System.out.println(null instanceof String); // Output : false
```

#### new operator :

We can use new operator for creating objects. In Java, there is no delete operator because destruction of useless object is responsibility of garbage collector.

#### [ ] operator :

We can use [ ] operator for declaring & creating Arrays.

### Operator precedence :-

The operator precedence decide the order in which expressions are evaluated. Order is as follows :

Unary operators :

`[ ] , a++ , a--`

`++a , --a , ~ , !`

`new`

Arithmetic operators :

`* , / , %`

`+ , -`

Shift operators :

`>>> , >> , <<`

Comparison operators :

`< , <= , > , >= , instanceof`

Equality operators :

`== , !=`

Bitwise operators :

`& `

`^ `

`| `

Short-circuit operators :

`&& `

`|| `

Conditional operator :

`? :`

Assignment operators :

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

##### Note :

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

X

## MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic. 