43 lines
1.3 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// operators/Bool.java
// (c)2021 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2016-01-25 18:05:55 -08:00
// Relational and logical operators
2015-06-15 17:47:35 -07:00
import java.util.*;
public class Bool {
public static void main(String[] args) {
Random rand = new Random(47);
int i = rand.nextInt(100);
int j = rand.nextInt(100);
2015-11-03 12:00:44 -08:00
System.out.println("i = " + i);
System.out.println("j = " + j);
System.out.println("i > j is " + (i > j));
System.out.println("i < j is " + (i < j));
System.out.println("i >= j is " + (i >= j));
System.out.println("i <= j is " + (i <= j));
System.out.println("i == j is " + (i == j));
System.out.println("i != j is " + (i != j));
2015-06-15 17:47:35 -07:00
// Treating an int as a boolean is not legal Java:
2015-12-18 11:28:19 -08:00
//- System.out.println("i && j is " + (i && j));
//- System.out.println("i || j is " + (i || j));
//- System.out.println("!i is " + !i);
2015-11-03 12:00:44 -08:00
System.out.println("(i < 10) && (j < 10) is "
2015-06-15 17:47:35 -07:00
+ ((i < 10) && (j < 10)) );
2015-11-03 12:00:44 -08:00
System.out.println("(i < 10) || (j < 10) is "
2015-06-15 17:47:35 -07:00
+ ((i < 10) || (j < 10)) );
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
i = 58
j = 55
i > j is true
i < j is false
i >= j is true
i <= j is false
i == j is false
i != j is true
(i < 10) && (j < 10) is false
(i < 10) || (j < 10) is false
2015-09-07 11:44:36 -06:00
*/