140 likes | 256 Vues
This guide covers essential Java concepts like unary increment operators (prefix and postfix), modulus operations to check for even or odd numbers, and type conversions including narrowing and widening conversions. It explores various examples demonstrating the use of unary operators, compound assignment operators, and the importance of explicit casting for certain data types. By mastering these fundamental concepts, you will enhance your programming skills and understand how data types interact within Java.
E N D
연산자 unary increment prefix: ++x; int x = 5; int y = ++x; System.out.print( x + “, “ + y); unary increment postfix: x++; int x = 5; int y = x++; System.out.print( x + “, “ + y); Output Output File Edit WindowHelp File Edit WindowHelp 6, 6 6, 5
modulus - % int x = 5 % 3; System.out.print(x); Output File Edit WindowHelp 2
modulus - % 짝수? 홀수? if(x % 2 == 0)… 짝수 else…홀수
Compound Operators • x += 5; • x -= 5; • x *= 5; • x /= 5; • x %= 5; int x = 10; • x = x + 5; • x = x – 5; • x = x * 5; • x = x / 5; • x = x % 5; • 15 • 5 • 50 • 2 • 0
Implicit Narrowing Conversion byte b = 2; b = b + 10; byte b = 2; b +=10;
무슨 conversion? • short > int • byte > long • float > long • char > int • char > short • byte > char
무슨 conversion? • short > int • byte > long • float > long • char > int • char > short • byte > char • widening • widening • narrowing • widening • narrowing • narrowing
conversions byte short int long char
conversions between byte, short and char are ALWAYS narrowing conversions requiring explicit cast byte short int long char
conversions between byte, short and char are ALWAYS narrowing conversions requiring explicit cast byte short char Narrowing Conversion
conversions between byte, short and char are ALWAYS narrowing conversions requiring explicit cast byte char short Narrowing Conversion
conversions between byte, short and char are ALWAYS narrowing conversions requiring explicit cast byte b = 16; char c = No! b;
conversions between byte, short and char are ALWAYS narrowing conversions requiring explicit cast byte b = 16; char c = b; (char)
65535 127 byte -128 char 0