-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConditionals.java
More file actions
56 lines (48 loc) · 1.9 KB
/
Conditionals.java
File metadata and controls
56 lines (48 loc) · 1.9 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 Conditionals {
// boolean variables are used to evaluate conditions
public static void main(String[] args) {
boolean a = true;
if (a) {
System.out.println("The condition is true");
}
int b = 3;
if (b == 3) {
System.out.println("The condition is true");
}
// Shorter ways of using if else statement
if (a)
System.out.println("The condition is true");
else
System.out.println("The condition is false");
// Using ternary operator
// variable c has to be declared first before assigning a value
String c;
c = a ? "The condition is true" : "The condition is false";
System.out.println(c);
// == and equals
// The == operator will show if objects are same while equals will show if objects are logically equal
String d = new String("Text");
String e = new String("Text");
String f = d;
boolean g = d == e; // This is false since d and e are not the same object
System.out.println(g);
boolean g1 = d.equals(e); // This is true since d and e are logically equal
System.out.println(g1);
boolean g2 = f == d; // This is true since d and f are same objects
System.out.println(g2);
}
public static void booleanOperators(String[] args) {
int a = 4;
int b = 5;
boolean result;
result = a < b; // true
result = a > b; // false
result = a <= 4; // a smaller or equal to 4 - true
result = b >= 6; // b bigger or equal to 6 - false
result = a == b; // a equal to b - false
result = a != b; // a is not equal to b - true
result = a > b || a < b; // Logical or - true
result = 3 < a && a < 6; // Logical and - true
result = !result; // Logical not - false
}
}