|
Operator symbol | Meaning | Comment |
---|---|---|
&& | The logical AND operator | Binary operator |
|| | The logical OR operator | Binary operator |
! | The logical NOT operator | Unary operator |
Operation | Result |
---|---|
true && true | true |
true && false | false |
false && true | false |
false && false | false |
(3 > 1) && (3 < 5) = true && true = true |
(3 > 1) && (3 > 5) = true && false = false |
Operation | Result |
---|---|
true || true | true |
true || false | true |
false || true | true |
false || false | false |
(3 < 1) || (3 < 5) = false || true = true |
(3 < 1) || (3 > 5) = false || false = false |
Operator | Description | Level | Associativity |
---|---|---|---|
[] . () ++ -- |
access array element access object member invoke a method post-increment post-decrement |
1 | left to right |
++ -- + - ! ~ |
pre-increment pre-decrement unary plus unary minus logical NOT bitwise NOT |
2 | right to left |
() new |
cast object creation |
3 | right to left |
* / % |
multiplicative | 4 | left to right |
+ - + |
additive string concatenation |
5 | left to right |
<< >> >>> |
shift | 6 | left to right |
< <= > >=instanceof |
relational type comparison |
7 | left to right |
== != |
equality | 8 | left to right |
& | bitwise AND | 9 | left to right |
^ | bitwise XOR | 10 | left to right |
| | bitwise OR | 11 | left to right |
&& | conditional AND | 12 | left to right |
|| | conditional OR | 13 | left to right |
?: | conditional | 14 | right to left |
= += -= *= /= %= &= ^= |= <<= >>= >>>= |
assignment | 15 | right to left |
Reference: click here
Note:
|
boolean a; Statement: a = 3 > 1 && 4 < 5 ; Operators in statement: = > && < Executed as follows: a = 3 > 1 && 4 < 5 ; // > and < have higher priority a = true && true ; a = true; |
boolean a; Statement: a = 3 > 1 && ! 4 < 5 Operators in statement: = > && ! < Executed as follows: a = 3 > 1 && ! 4 < 5 // ! has the higher priority Result: error Logical NOT operator (!) cannot be applied to a number |
boolean a; Statement: a = 3 > 1 && ! (4 < 5) Operators in statement: = > && ! ( < ) Executed as follows: a = 3 > 1 && ! (4 < 5); // ( ... ) has the higher priority a = 3 > 1 && ! true; // ! has the higher priority now a = 3 > 1 && false; // > has the higher priority now a = true && false; // && has the higher priority now a = false; |
|
if ( 10 <= a <= 20 ) System.out.println("yes"); |
Because 10 <= a <= 20 is evaluated as follows:
Expression: 10 <= a <= 20 Operators: <= <= Evaluated as: 10 <= a <= 20 (10 <= a is either true or false) true <= 20 or false <= 20 |
It is illegal to use the comparison operator "<=" on a Boolean value and a number
if ( 10 <= a && a <= 20 ) System.out.println("yes"); |
The condition is evaluated as follows:
Expression: 10 <= a && a <= 20 Operators: <= && <= (<= has higher priority, see above) Evaluated as: 10 <= a && a <= 20 Result: && operates on 2 logical values |
Effect: only numbers that are between 10 and 20 will satisfy the condition 10 <= a && a <= 20
import java.util.Scanner; public class Between01 { public static void main(String[] args) { int a; Scanner in = new Scanner(System.in); // Construct Scanner object a = in.nextInt(); // Read in number into a if ( 10 <= a && a <= 20 ) { System.out.println("Number is between 10 and 20"); } } } |
How to run the program:
|
|
|
Reason: see the ASCII code table
0 NUL 1 SOH 2 STX 3 ETX 4 EOT 5 ENQ 6 ACK 7 BEL 8 BS 9 HT 10 NL 11 VT 12 NP 13 CR 14 SO 15 SI 16 DLE 17 DC1 18 DC2 19 DC3 20 DC4 21 NAK 22 SYN 23 ETB 24 CAN 25 EM 26 SUB 27 ESC 28 FS 29 GS 30 RS 31 US 32 SP 33 ! 34 " 35 # 36 $ 37 % 38 & 39 ' 40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 / 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ? 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w 120 x 121 y 122 z 123 { 124 | 125 } 126 ~ 127 DEL |
If we arbitrarily subtract 32 from any ASCII code, the result is not always "convert the lower case character to the same upper case character"
public class ConvertChar01 { public static void main(String[] args) { char x; x = 'a'; if ( x >= 'a' && x <= 'z' ) x = (char) (x - 'a' + 'A'); // Convert only if character code // is a lower case letter System.out.println("'a': " + x); x = 'H'; if ( x >= 'a' && x <= 'z' ) x = (char) (x - 'a' + 'A'); System.out.println("'H': " + x); x = '%'; if ( x >= 'a' && x <= 'z' ) x = (char) (x - 'a' + 'A'); System.out.println("'%': " + x); } } |
Output:
'a': A 'H': H '%': % |
How to run the program:
|