-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperators.java
More file actions
84 lines (70 loc) · 2.3 KB
/
Operators.java
File metadata and controls
84 lines (70 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
class Operators{
public static void main(String[] args){
int a=5, b=5;
//Arithmetic operators
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a/b);
System.out.println(a*b);
System.out.println(a%b);
//To convert String to integers
String strnum1 = "45";
String strnum2 = "45";
int num1 = Integer.parseInt(strnum1);
int num2 = Integer.parseInt(strnum2);
System.out.println(num1+num2);
//Unary operators
System.out.println(+1);
System.out.println(-1);
System.out.println(!false);
int x=10, y=10;
System.out.println("post increment = " + x++);
System.out.println("pre increment = " + ++y);
//Assignment operator
int s = 10;
//Relational operators
System.out.println(a==b);
System.out.println(a!=b);
System.out.println(a<b);
System.out.println(a<=b);
System.out.println(a>b);
System.out.println(a>=b);
//Logical operators
System.out.println(a>b && a!=b);
System.out.println(a<b || a==b);
//System.out.println(!a);
//Ternary operator (max of 3 nums)
int p=20, q=10, r=30;
int result = (p>q) ? ( p>r ? p : r) : (q>r ? q : r);
System.out.println(r);
//Bitwise operators
int e = 0b1010;
int f = 0b1100;
System.out.println(e&f);
System.out.println(e|f);
System.out.println(e^f);
System.out.println(~e);
//To binary string
String andResult = Integer.toBinaryString(e&f);
System.out.println(andResult);
//Shift operators
a = 10;
System.out.println(a<<1); //multiplies by 2
System.out.println(a>>1); //divides by 2
System.out.println(a>>>1);
//instanceof operator
Example obj = new Example();
System.out.println(obj instanceof Example);
//Operator precedence
a=20;
b=10;
int c=0;
int d=20;
e=40;
f=30;
System.out.println(a+b/10);
System.out.println(a+b*d-e/f);
System.out.println(a+++b);
}
}
class Example{}