๋ชฉํ
์๋ฐ๊ฐ ์ ๊ณตํ๋ ๋ค์ํ ์ฐ์ฐ์๋ฅผ ํ์ต.
ํ์ตํ ๊ฒ
- ์ฐ์ ์ฐ์ฐ์
- ๋นํธ ์ฐ์ฐ์
- ๊ด๊ณ ์ฐ์ฐ์
- ๋ ผ๋ฆฌ ์ฐ์ฐ์
- instanceof
- assignment(=) operator
- ํ์ดํ(->) ์ฐ์ฐ์
- 3ํญ ์ฐ์ฐ์
- ์ฐ์ฐ์ ์ฐ์ ์์
- (optional) Java 13. switch ์ฐ์ฐ์
+) ์๋ฐ์ ์ฐ์ฐ์ ํ๊ฐ ์์
int a = 2;
int v = ++a + ++a * ++a;
์ธํฐํ๋ฆฌํฐ๋ ํญ์ ์ฐ์ฐ์๋ฅผ ์ผ์ชฝ โ ์ค๋ฅธ์ชฝ์ผ๋ก ํ๊ฐํ๋ค. ๋ฐ๋ผ์ ์ ์ฝ๋์์ *
๊ณฑ์
์ด +
๋ง์
๋ณด๋ค ๋จผ์ ์ํ๋ ์ง๋ผ๋, +
์ฐ์ฐ์๋ ๋จผ์ ํ๊ฐ๋์ด v๋ 3 + 4 * 5
์ธ 23
์ด ๋๋ค.
์ฐ์ ์ฐ์ฐ์
- ๋ง์
+
- String concatenation operator ๋ก๋ ๋์ํ๋ค.
- Java๋ ๋ชจ๋ primitive type์ ๋ํด string ๋ณํ์ ์ ๊ณตํ๊ธฐ ๋๋ฌธ์ โHelloโ + 123 : โHello 123โ ์ด ๋๋ค.
toString()
์ ์ ์ํ ํด๋์ค๋ ํด๋น ๋ฉ์๋๊ฐ ์คํ๋๋ค.
- ๋นผ๊ธฐ
-
- ๊ณฑ์
*
- ๋๋์
/
- modulo
%
- floating-point์๋ ๋์ํ๋ค. ex)
4.3 % 2.1
์ 0.1
- floating-point์๋ ๋์ํ๋ค. ex)
๋น๊ต ์ฐ์ฐ์
==
,!=
NaN
์ ์ด๋ค ๊ฐ๊ณผ๋ ๊ฐ์ง ์์.- positive zero์ negative zero๋ ๊ฐ์ ๊ฒ์ผ๋ก ์น๋ค.
<
>
<=
>=
๋ ผ๋ฆฌ ์ฐ์ฐ์
&&
and||
or!
not&
and (๋นํธ ์ฐ์ฐ์๋ ์ฌ์ฉ.&&
์๋ ๋ฌ๋ฆฌ ํผ์ฐ์ฐ์ ๋ ๊ฐ์ ๊ฐ์ ์ ๋ถ ํ์ธํ๋ค)|
or (๋นํธ ์ฐ์ฐ์๋ ์ฌ์ฉ.||
์๋ ๋ฌ๋ฆฌ ํผ์ฐ์ฐ์ ๋ ๊ฐ์ ๊ฐ์ ์ ๋ถ ํ์ธํ๋ค)^
xor (๋นํธ ์ฐ์ฐ์๋ ์ฌ์ฉ)
๋นํธ ์ฐ์ฐ์
~
not&
and|
or^
xor<<
left shift>>
right shift>>>
unsigned right shift>>
๋ MSB ๋ถํธ๋ฅผ ๊ทธ๋๋ก ์ ์งํ๋ฉด์ ์ฎ๊ธฐ๋ ๋ฐ๋ฉด,>>>
๋ ํญ์ 0์ผ๋ก ์ฑ์ด๋ค.
instanceof
ํ์ ์ ํ์ธํ๋ ์ฐ์ฐ์.
// True: ๋ชจ๋ ๋ฌธ์์ด์ String ํด๋์ค์ ์ธ์คํด์ค์ด๋ค.
"string" instanceof String
// True: ๋ชจ๋ ๋ฌธ์์ด์ Object ํด๋์ค์ ์ธ์คํด์ค์ด๋ค.
"" instanceof Object
// False: null์ ์ด๋ค ๊ฒ์ ์ธ์คํด์ค๋ ์๋๋ค.
null instanceof String
Object o = new int[] {1,2,3};
o instanceof int[] // True
o instanceof byte[] // False : byte ๋ฐฐ์ด์ด ์๋๋ค.
o instanceof Object // True: ๋ชจ๋ ๋ฐฐ์ด์ Object์ ์ธ์คํด์ค์ด๋ค.
// ์์ ํ ํ์
์บ์คํ
์ ์ํด์๋ instanceof๋ก ํ์ธํ์.
if (object instanceof Point) {
Point p = (Point) object;
}
assignment(=) operator
- ๊ฐ์ ์ ์ฅ(=ํ ๋น)ํ ๋ ์ด๋ค.
- ์ค๋ฅธ์ชฝ โ ์ผ์ชฝ ์์๋๋ก ํ ๋น๋๋ค
a=b=c
โโa=(b=c)
ํ์ดํ(->) ์ฐ์ฐ์
Lambda์์ ํํ์ ์ํ ์ฐ์ฐ์.
3ํญ ์ฐ์ฐ์
if else
๋ฌธ์ ํ ์ค์ ํํ์์ผ๋ก ๋ง๋ ๊ฒ โ <condition> ? <if true> : <if false>
์ฐ์ฐ์ ์ฐ์ ์์
Operators | Precedence |
---|---|
postfix increment and decrement | ++ -- |
prefix increment and decrement, and unary | ++ -- + - ~ ! |
multiplicative | * / % |
additive | + - |
shift | << >> >>> |
relational | < > <= >= instanceof |
equality | == != |
bitwise AND | & |
bitwise exclusive OR | ^ |
bitwise inclusive OR | | |
logical AND | && |
logical OR | || |
ternary | ? : |
assignment | = += -= *= /= %= &= ^= |= <<= >>= >>>= |
Java 13. switch ์ฐ์ฐ์
์ฐธ๊ณ - https://openjdk.org/jeps/354
switch
๊ตฌ๋ฌธ์ ํํ์์ผ๋ก ์ธ ์ ์๋ค (๊ฐ ํํ๋ก ์ฌ์ฉ๊ฐ๋ฅ)case L:
ํํ์ ๊ตฌ๋ฌธ์case L ->
๊ตฌ๋ฌธ์ผ๋ก ๋ณ๊ฒฝํด์ ์ธ ์ ์๋ค.
// ๊ธฐ์กด switch ๊ตฌ๋ฌธ(๋ค๋ฅธ ์ธ์ด๋ค๊ณผ ํํ๊ฐ ๋น์ท). ์ฝ๋๊ฐ ์ฅํฉํด์ง๊ธฐ ์ฝ๋ค.
switch (day) {
case MONDAY:
case FRIDAY:
case SUNDAY:
System.out.println(6);
break;
case TUESDAY:
System.out.println(7);
break;
case THURSDAY:
case SATURDAY:
System.out.println(8);
break;
case WEDNESDAY:
System.out.println(9);
break;
}
// ๋ฐ๋ `->` ๊ตฌ๋ฌธ. case ๋ด๋ถ์ ์ ์ธ๋ ๋ณ์์ scope๋ฅผ case ์์ผ๋ก ์ ํํ๋ค.
switch (day) {
case MONDAY, FRIDAY, SUNDAY -> System.out.println(6);
case TUESDAY -> System.out.println(7);
case THURSDAY, SATURDAY -> System.out.println(8);
case WEDNESDAY -> System.out.println(9);
}
// ์ฌ๋ฌ ์ค๋ก ์ฐ๋ ค๋ฉด `yield`ํค์๋๋ฅผ ์ฌ์ฉํ๋ค.
int result = switch (s) {
case "Foo":
yield 1;
case "Bar":
yield 2;
default:
System.out.println("Neither Foo nor Bar, hmmm...");
yield 0;
};
// ํํ์
int numLetters = switch (day) {
case MONDAY, FRIDAY, SUNDAY -> 6;
case TUESDAY -> 7;
case THURSDAY, SATURDAY -> 8;
case WEDNESDAY -> 9;
};