Operators in Java
Operators are special symbols that perform operations.
Arithmetic Operator (+, -, *, /,%)
Arithmetic operation from arithmetic expression (a+b, a/b)
The result of arithmetic expression is integer literal or floating point literal.
Example:
package com.javabykiran.basics;
public class Lab3Test {
public static void main(String[] args) {
byte b1 = 10;
byte b2 = b1 + 2; // error type mismatch
System.out.println(b2);
float f1 = 9.9f;
float f2 = f1 + 10.0; // error type mismatch
System.out.println(f1);
System.out.println(f2);
}
}
Modifying
byte b2=b1+2 to byte b2=(byte)(b1+2); // type casting
float f2=f1+10.0 to float f2=(float)(f1+10.0);
package com.javabykiran.basics;
public class Lab3Test {
public static void main(String[ ] args) {
byte b1 = 10;
//byte b2 = b1 + 2;
byte b2=(byte)(b1+2);
System.out.println(b2);
float f1 =9.9f;
//float f2 = f1 + 10.0;
float f2 = (float)(f1 + 10.0); // type casting
System.out.println(f1);
System.out.println(f2);
}
}
Remember the chart drawn below :
| Type byte | Operator + | Type byte | Result Type byte |
|---|---|---|---|
| byte | + | short | Byte |
| byte | + | int | Int |
| byte | + | long | Long |
| byte | + | float | Float |
| byte | + | double | Double |
| short | + | short | Short |
| short | + | int | Int |
| short | + | long | Long |
| short | + | float | Float |
| short | + | double | Double |
| int | + | int | Int |
| int | + | long | long |
Example:
package com.javabykiran.basics;
public class Lab4Test {
public static void main(String[] args) {
int a=0/10;
System.out.println(a); //0
// int b=10/0;
// java.lang.ArithmeticExcepton:/by zero
// system.out.println(b);
double dl = 0.0/10.;
System.out.println(dl); //0.0
double d2= 10.0/00;
System.out.println(d2); //Infinity
int x=10%3;
System.out.println(x); //1
double d3=10.0/10.0;
System.out.println(d3); //1.0
int x1=10;
System.out.println(x1); //10
double d=10.0%1.0;
System.out.println(d); //0.0
}
}
Output:
0
0.0
Infinity 1
1.0
10
0.0
Relational Operator (>, >=, <, <=)
It is a form of relational expression whose result is the ‘boolean’ value a < b.
Logical Operator (&&, ||, \)
A Logical operator forms logical expression, which is a combination of one or more relational expressions
(a>b) && (a>c)
(a>b) || (a>c)
The result of logical expression is based on Boolean values, which in turn is based on the following table:
| A | B | A&&B [and] | A||B [OR] |
|---|---|---|---|
| T | T | T | T |
| T | F | F | T |
| F | T | F | T |
| F | F | F | F |
Assign Operator (=, +=, -=, *=, /=, &=)
-
Syntax:
- destination_var = source_var;
Example:
int x = 10+9.9; // error - Type mismatch: cannot convert from double to int
System.out.println(10+9.9); //ok
byte b = 10; // ok
int x = b; //ok
There are two types of casting:
Implicit casting
Explicit casting
Implicit casting : When the destination is bigger than the source, then conversion happens automatically, which is called implicit casting. Another term for it is widening.
Explicit casting : When the destination is smaller than the sources, then you have to do explicit casting or narrowing
-
Syntax:
- destination_var = (destination type)source_var;
Example:
int x = 10+9.9; // error-Type mismatch: cannot convert from double to int
int x1=(int)10+9.9; // error-Type mismatch: cannot convert from double to int
int x2=(int) (10+9.9); // ok
int x3=10+(int)9.9; // ok
Increment/Decrement Operator (++, --)
It is a type of arithmetic operator. It allows you to add and subtract 1 from variables.
Example:
package com.javabykiran.basics;
class Lab5 {
public static void main(String[] args) {
int x = 10;
System.out.println(x); // o/p 10
int a = ++x; // prefix increment
int b = --x; // prefix decremennt
int c = x++; // postfix increment
int d = x--; // postfix decrement
int e = ++x; // prefix increment
int f = ++x; // prefix increment
int g = x++;
System.out.println(a); // 11 11 11
System.out.println(b); // 10 10 10
System.out.println(c); // 10 11 10
System.out.println(d); // 11 11 11
System.out.println(e); // 11 12 11
System.out.println(f); // 12 12 12
System.out.println(g); // 12 13 12
}
}
Output:
10
11
10
10
11
11
12
12
Ternary Operator (?:)
Ternary operator is used to perform simple conditional checking
- Syntax : (Conditional expression)? s1:s2;
First, the given expression is evaluated. If the expression is true, then s1 will be executed. If the expression is false, then s2 will be executed.
Question : Write a program to read the two numbers from command line and find the minimum and maximum number.
package com.javabykiran.basics;
public class Lab6 {
public static void main(String[] as) {
int a = Integer.parseInt(as[0]);
int b = Integer.parseInt(as[1]);
int min=(a < b) ? a:b;
int max=(a > b) ? a:b;
System.out.println("Min :: "+min);
System.out.println("Max :: "+max);
}
}
Output:
20
1) javaLab6
// ArrayIndexOutOfBoundsException:0
2) java Lab6 10 [when we pass single parameter below error will occur]
// ArrayIndexOutOfBoundException:1
3) java Lab6 10 20 [output would be]
4) Min :: 10
Max :: 20
package com.javabykiran.basics;
public class Lab7 {
public static void main(String[] args) {
int a=10;
int b=20;
//int min=(a+b) ? a:b; // error-Type mismatch: cannot convert
// from int to boolean
int min=(a>b) ? a:b;
System.out.println(min);
}
}
/*error: incompatible types
found: int
required: boolean
int min= (a+b) ? a:b;
*/
Question : Write a program to take three numbers from the command line and find the minimum and maximum.
package com.javabykiran.basics;
public class Lab8 {
public static void main(String[] a5) {
int a =Integer.parseInt(a5[0]);
int b =Integer.parseInt(a5[1]);
int c =Integer.parseInt(a5[2]);
int max=(a > b) ? (a > c) ? a:c: (b > c) ? b:c;
int min=(a < b) ? (a < c) ? a:c: (b < c) ? b:c;
System.out.println("Min:" +min);
System.out.println("Max:" +max);
}
}
Output:
Min: 5
Max 15
Bitwise Operator (&, \, ^)
Bitwise operator acts on individuals bits of given numbers.
| 1. | Left shift operator | << |
| 2. | Right shift operator | >> |
| 3. | Bitwise AND | & |
| 4. | Bitwise OR | | |
| 5. | Bitwise XOR | ^ |
| 6. | Bitwise NOT | ~ |
Example:
package com.javabykiran.basics;
public class Lab10Test {
public static void main(String[] args) {
int a = 2; // 0010
int b = 4; // 0100
System.out.println("value of a before:"+a);
System.out.println("value of b before:"+b);
// bitwise unary complement operator (~)
System.out.println("value of a after negation: " + ~a); //-3
System.out.println("value of a after negation: " + ~b); //-5
// bitwise AND operator &
System.out.println("Result of a&b is:"+(a & b)); //0
// bitwise OR operator |
System.out.println("Result of a|b is:"+(a| b)); // 6
// bitwise XOR operator ^
System.out.println("Result of a^b is:"+(a^ b)); // 6
int no = 8; // 0000 1000
System.out.println("Original number:"+no); // 8
// left shifting bytes with 1 position
no = no << 1; // should be 16 i.e. 0001 0000
// equivalent of multiplication of 2
System.out.println("value after left shift:" + no); //16
no = -8;
//right shifting bytes with sign 1 position
no=no>>1; //should be 16 i.e.00010000
// equivalent of division of 2
System.out.println("value after right shift with sign:"+no); //-4
no = -8;
//right shifting bytes without sign 1 position no=no>>>1;
//should be 16 i.e.00010000
// equivalent of division of 2
System.out.println("value after right shift with sign: " + no);
// 2147483644
}
}
Output:
38
76
9
6
15
9
-16
-501
130
32
-1
New Operator (new)
A new operator is used to create an object of a given class.
-
Example:
Hello h = new Hello();Dot Operator
A dot operator is used to refer members of a class using class name or object.
-
Example:
Hello h = new Hello();
h.a;
h.show();Equality
| bitwise AND | & |
| bitwise exclusive OR | ^ |
| bitwise inclusive OR | | |
| logical AND | && |
| logical OR | || |
| ternary | ? |
| assignment | =++-=*=/=%=&=^=|=<=>= |
Operator Precedence
| Operator | Precedence |
|---|---|
| Post fix | expr ++ expr -- |
| Unary | ~ ! +!expr -- expr+expr-expr |
| Multiplicative | * / % |
| Additive | + - |
| Shift | < < > >>>> |
| Relational | < > <=> = instance of |
Note: no sizeof operator in java
Example:
package com.javabykiran.basics;
public class Lab11 {
public static void main(String[] args) {
double a=19.9;
//System.out.println(a>>1); //error- operator <<
//cannot be applied to double
//int x = 09; //error - The literal Octal 09 (digit 9)
//of type int is out of range
//System.out.println(x); // integer number too large :09
int y = 0101;
System.out.println(y); //65 - octal conversion
}
}
Output:
65
← Back to Java Language Chapter