-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConditionalsAndBooleans.java
More file actions
56 lines (50 loc) · 1.32 KB
/
ConditionalsAndBooleans.java
File metadata and controls
56 lines (50 loc) · 1.32 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
public class ConditionalsAndBooleans{
public static void main(String[] args){
int a = 4;
int b = 5;
boolean result;
// boolean operators:
result = a < b;
System.out.println(result);
result = a > b;
System.out.println(result);
result = a == b;
System.out.println(result);
result = a <= 4 ;
System.out.println(result);
result = b >= 6;
System.out.println(result);
result = a != b; // not equal
System.out.println(result);
result = a > b || a < b; // logical or
System.out.println(result);
result = a == b && b > 3; // logical and
System.out.println(result);
result = !result; // logical not
System.out.println(result);
// if
if (a == b){
System.out.println("a == b");
}
// if - else
if (a==b){
System.out.println("a == b");
} else {
System.out.println("a != b");
}
// corresponding one-liner without {}, but not recommended
if (a==b) System.out.println("a == b"); else System.out.println("a != b");
// even shorter
int resultInteger = a == 5 ? 1 : 8;
System.out.println("result = " + resultInteger);
String one = new String("VfB");
String two = new String("VfB");
String three = one;
boolean oneTwo = one == two;
boolean oneTwoEquals = one.equals(two);
boolean oneThree = one == three;
System.out.println(oneTwo);
System.out.println(oneTwoEquals);
System.out.println(oneThree);
}
}