250 likes | 379 Vues
Dive into the world of Java programming as we explore assignment, relational, and logical operators. This guide examines the nuances of variable assignments, operator behaviors, and evaluates expressions in Java. With examples like `a = b = c = 5` and various boolean evaluations, you'll learn not only whether the code compiles but also how to anticipate outputs. Gain insights into left and right associativity, and grasp how logical operators function. Perfect for Java learners looking to strengthen their understanding of basic Java syntax and operator precedence.
E N D
Assignment Operator int a, b, c; a = b = c = 5; System.out.println( a + b + c ); Does it compile?
Assignment Operator int a, b, c; a = b = c = 5; System.out.println( a + b + c ); What is the output?
Assignment Operator int a, b, c; a = b = c = 5; System.out.println( a + b + c ); Output File Edit WindowHelp 15
Assignment Operator int a, b, c; a = b = c = 5; System.out.println( a + b + c ); right associative
Relational Operators관계 연산자 <, <=, >, >= 결과가 true / false evaluates to true / false
Relational Operators int a = 1; boolean b = a < 2; value of b: true
Relational Operators int a = 1; boolean b = a < 0; value of b: false
Relational Operators int a = 1; boolean b = a <= 1; value of b: true
Relational Operators int a = 1; int b = 2; int c = 3; boolean b = a < b < c; Does it compile?
Relational Operators a < b < c left associative
Relational Operators true < c ?
Relational Operators int a = 1; int b = 2; int c = 3; boolean b = a < b &b< c;
Boolean Logical Operators논리 연산자 !, ^, &, | 결과가 true / false evaluates to true / false
! – NOT (logical complement) boolean b; 1: b = ! true; 2: b = ! (5 > 2); in 1 and 2, value of b is ‘false’
| - OR (inclusive or) one or the other (or both) are true
| - OR (inclusive or) boolean b; 1: b = true | false; 2: b = false | true; 3: b = true | true; 4: b = false | false; true true true false
^ - XOR (exclusive or) One or the other, but not both, are true
^ - XOR (exclusive or) boolean b; 1: b = true ^ false; 2: b = false ^ true; 3: b = true ^ true; 4: b = false ^ false; true true false false
& - AND both are true
& - AND boolean b; 1: b = true & false; 2: b = false & true; 3: b = true & true; 4: b = false & false; false false true false
& - AND boolean a = false; int i = 0; boolean b = a & i++ > 0; System.out.print( b + “ “ + i); Output File Edit WindowHelp false 1
&&, || - shortcut operators단락 연산자 boolean a = false; int i = 0; boolean b = a &&i++ > 0; System.out.print( b + “ “ + i); Output File Edit WindowHelp false 0
&&, || - shortcut operators boolean a = true; int i = 0; boolean b = a ||i++ > 0; System.out.print( b + “ “ + i); Output File Edit WindowHelp true 0