nyximos.log

[Java] ์—ฐ์‚ฐ์ž ๋ณธ๋ฌธ

Programming/JAVA

[Java] ์—ฐ์‚ฐ์ž

nyximos 2022. 2. 2. 01:14

๐Ÿ‘ฉ‍๐Ÿš€ ๋ชฉํ‘œ

์ž๋ฐ”๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๋‹ค์–‘ํ•œ ์—ฐ์‚ฐ์ž ํ•™์Šตํ•˜๊ธฐ

๐Ÿ‘ฉ‍๐Ÿ’ป ํ•™์Šตํ•  ๊ฒƒ

  • ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž
  • ๋น„ํŠธ ์—ฐ์‚ฐ์ž
  • ๊ด€๊ณ„ ์—ฐ์‚ฐ์ž
  • ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž
  • instanceof
  • assignment(=) operator
  • ํ™”์‚ดํ‘œ(->) ์—ฐ์‚ฐ์ž
  • 3ํ•ญ ์—ฐ์‚ฐ์ž
  • ์—ฐ์‚ฐ์ž ์šฐ์„  ์ˆœ์œ„
  • (optional) Java 13. switch ์—ฐ์‚ฐ์ž

์—ฐ์‚ฐ์ž๋Š” ํ”ผ์—ฐ์‚ฐ์ž๋“ค์— ๋Œ€ํ•ด ํŠน์ • ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํŠน์ˆ˜ ๊ธฐํ˜ธ์ด๋‹ค.

์—ฐ์‚ฐ์ž๊ฐ€ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฐ˜๋“œ์‹œ ์—ฐ์‚ฐ์˜ ๋Œ€์ƒ์ด ์žˆ์–ด์•ผ ํ•˜๋Š”๋ฐ ์ด๊ฒƒ์„ ํ”ผ์—ฐ์‚ฐ์ž๋ผ ๋ถ€๋ฅธ๋‹ค.

 

์—ฐ์‚ฐ์ž์—๋Š” ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž, ๊ด€๊ณ„ ์—ฐ์‚ฐ์ž, ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž, ๋Œ€์ž… ์—ฐ์‚ฐ์ž, ํ˜•๋ณ€ํ™˜ ์—ฐ์‚ฐ์ž, ์‚ผํ•ญ ์—ฐ์‚ฐ์ž, instanceof ์—ฐ์‚ฐ์ž, ์ฆ๊ฐ ์—ฐ์‚ฐ์ž, ๋ถ€ํ˜ธ ์—ฐ์‚ฐ์ž ๋“ฑ์ด ์žˆ๋‹ค.

๐Ÿš—์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž

์‚ฌ์น™ ์—ฐ์‚ฐ๊ณผ ๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ์„ ํ•˜๋Š” ์—ฐ์‚ฐ์ž

 

๐Ÿคฏ ํ”ผ์—ฐ์‚ฐ์ž๊ฐ€ ์ •์ˆ˜ํ˜•์ผ ๊ฒฝ์šฐ 0์œผ๋กœ ๋‚˜๋ˆŒ์‹œ ArithmeticException์ด ๋ฐœ์ƒํ•œ๋‹ค!

๐Ÿ˜ฎ ๋ฌธ์ž๋„ ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•˜๋‹ค → ์ €์žฅ๋  ๋•Œ ์œ ๋‹ˆ์ฝ”๋“œ๋กœ ์ €์žฅ๋˜๊ธฐ ๋•Œ๋ฌธ์—

 

Operator Description  
+ ๋ง์…ˆ ์—ฐ์‚ฐ์ž Additive operator (also used for String concatenation)
- ๋บ„์…ˆ ์—ฐ์‚ฐ์ž Subtraction operator
* ๊ณฑ์…ˆ ์—ฐ์‚ฐ์ž Multiplication operator
/ ๋‚˜๋ˆ—์…ˆ ์—ฐ์‚ฐ์ž Division operator
% ๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ์ž Remainder operator

 

public class Main {
	public static void main(String[] args) {
		
		// Additive operator
		int result = 1 + 2;
		System.out.println("1 + 2 = " + result);
		int original_result = result;
		
		// Subtraction operator
		result = result- 1;
		System.out.println(original_result + " - 1 = " +  result);
		original_result = result;
		
		
		// Multiplication operator
		result = result * 3;
		System.out.println(original_result + " * 3 = " +  result);
		original_result = result;
		
		
		// Division operator
		result = result / 2;
		System.out.println(original_result + " / 2 = " + result);
		original_result = result;
		
		
		// Remainder operator
		result = result % 2;
		System.out.println(original_result + " % 2 = " +  result);

	}
}
1 + 2 = 3
3 - 1 = 2
2 * 3 = 6
6 / 2 = 3
3 % 2 = 1

 

 

๐Ÿš“ ๋น„ํŠธ ์—ฐ์‚ฐ์ž

ํ”ผ์—ฐ์‚ฐ์ž๋ฅผ ๋น„ํŠธ ๋‹จ์œ„๋กœ ์—ฐ์‚ฐํ•œ๋‹ค.

ํ”ผ์—ฐ์‚ฐ์ž๋กœ ์ •์ˆ˜๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์‹ค์ˆ˜๋ฅผ ํ”ผ์—ฐ์‚ฐ์ž๋กœ ์‚ฌ์šฉํ•  ์‹œ ์ปดํŒŒ์ผ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

Operator Description
& AND ์—ฐ์‚ฐ์ž
๋ชจ๋“  ๋น„ํŠธ ๊ฐ€ 1์ผ๋•Œ ๊ฒฐ๊ณผ๋Š” 1

์ด์™ธ์—๋Š” 0์„ ๋ฐ˜ํ™˜
| OR ์—ฐ์‚ฐ์ž
๋ชจ๋“  ๋น„ํŠธ์ค‘ ํ•œ ๋น„ํŠธ๊ฐ€ 1์ด๋ฉด ๊ฒฐ๊ณผ๋Š” 1

์ด์™ธ์—๋Š” 0์„ ๋ฐ˜ํ™˜
^ XOR ์—ฐ์‚ฐ์ž
๋น„ํŠธ๊ฐ€ ๊ฐ™์œผ๋ฉด 0 ์„œ๋กœ ๋‹ค๋ฅผ๋•Œ 1
~ NOT ์—ฐ์‚ฐ์ž, ๋น„ํŠธ ์ „ํ™˜ ์—ฐ์‚ฐ์ž
0์€ 1๋กœ 1์€ 0์œผ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.
<< ๋น„ํŠธ ์™ผ์ชฝ ์ด๋™
์™ผ์ชฝ์œผ๋กœ ์ด๋™ ํ›„ ์˜ค๋ฅธ์ชฝ์— 0์„ ์ฑ„์›Œ ๋„ฃ๋Š”๋‹ค.
>> ๋น„ํŠธ ์˜ค๋ฅธ์ชฝ ์ด๋™
์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™ํ›„ ์™ผ์ชฝ์— ํ•ด๋‹น ๋ถ€ํ˜ธ ๋น„ํŠธ๋กœ ์ฑ„์šด๋‹ค.
>>> ๋น„ํŠธ ์˜ค๋ฅธ์ชฝ ์ด๋™ (unsigned)
์™ผ์ชฝ ๋น„ํŠธ๊ฐ€ ๋ถ€ํ˜ธ ๋น„ํŠธ๋กœ ์ฑ„์›Œ์ง€์ง€ ์•Š๊ณ  0์œผ๋กœ ์ฑ„์›Œ์ง„๋‹ค.

 

public class Main {
	public static void main(String[] args) {
		
		// & ๋ชจ๋‘ 1์ด๋ฉด 1
		System.out.println("1&1 = " + (1&1));
		System.out.println("1&0 = " + (1&0));
		
		// | ํ•œ์ชฝ ๊ฐ’์ด 1์ด๋ฉด 1
		System.out.println("1|1 = " + (1|1));
		System.out.println("1|0 = " + (1|0));
		
		// ^ ๊ฐ’์ด ์„œ๋กœ ๋‹ค๋ฅผ ๋•Œ 1
		System.out.println("1^1 = " + (1^1));
		System.out.println("1^0 = " + (1^0));
		
		// ~ ๋น„ํŠธ๊ฐ€ 0์ด๋ฉด 1, 1์ด๋ฉด 0์œผ๋กœ ๋ณ€ํ™˜
		System.out.println("~-2 = " + (~-2));
		System.out.println("~-1 = " + (~-1));
		System.out.println("~0 = " + (~0));
		System.out.println("~1 = " + (~1));
		System.out.println("~2 = " + (~2));
		
	}
}
1&1 = 1
1&0 = 0
1|1 = 1
1|0 = 1
1^1 = 0
1^0 = 1
~-2 = 1
~-1 = 0
~0 = -1
~1 = -2
~2 = -3

 

public class Main {
	public static void main(String[] args) {
		
		int x = 3;
		int y = 6;

		System.out.println("~x = " + ~x);
		System.out.println("x & y = " + (x & y));
		System.out.println("x | y = " + (x | y));
		System.out.println("x << 1 = " + (x << 1));
		System.out.println("x >> 1 = " + (x >> 1));
		System.out.println("x >>> 1 = " + (x >>> 1));

	}
}
~x = -4
x & y = 2
x | y = 7
x << 1 = 6
x >> 1 = 1
x >>> 1 = 1

 

 

๐Ÿš• ๊ด€๊ณ„ ์—ฐ์‚ฐ์ž

๋‘ ํ”ผ์—ฐ์‚ฐ์ž๋ฅผ ๋น„๊ตํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์—ฐ์‚ฐ์ž์ด๋‹ค.

์ฃผ๋กœ ์กฐ๊ฑด๋ฌธ์ด๋‚˜ ๋ฐ˜๋ณต๋ฌธ์˜ ์กฐ๊ฑด์‹์— ์‚ฌ์šฉ๋œ๋‹ค.

๊ฒฐ๊ณผ๋Š” true์™€ false ๋‘˜ ์ค‘ ํ•˜๋‚˜๋กœ ๊ณ„์‚ฐ๋œ๋‹ค.

์ดํ•ญ ์—ฐ์‚ฐ์ž์ด๋ฏ€๋กœ ํ”ผ์—ฐ์‚ฐ์ž์˜ ํƒ€์ž…์ด ์„œ๋กœ ๋‹ค๋ฅผ ๊ฒฝ์šฐ์—๋Š” ์ž๋ฃŒํ˜•์ด ๋” ํฐ ์ชฝ์œผ๋กœ ์ž๋™ ํ˜•๋ณ€ํ™˜ํ•˜์—ฌ ํ”ผ์—ฐ์‚ฐ์ž์˜ ํƒ€์ž…์„ ์ผ์น˜์‹œํ‚จํ›„ ๋น„๊ตํ•œ๋‹ค.

 

๊ฐ’์ด ๊ฐ™์€์ง€ ๋‹ค๋ฅธ์ง€ ๋น„๊ตํ•˜๋Š” ๋“ฑ๊ฐ€ ๋น„๊ต ์—ฐ์‚ฐ์ž์™€

ํ”ผ์—ฐ์‚ฐ์ž ๊ฐ’์„ ๋น„๊ตํ•˜๋Š” ๋Œ€์†Œ๋น„๊ต ์—ฐ์‚ฐ์ž๊ฐ€ ์žˆ๋‹ค.

 

Operator Description
== equal to
!= not equal to
> greater then
>= greater than or equal to
< less than
<= less than or equal to
public class Main {
	public static void main(String[] args) {
		
		int a = 1;
		int b = 2;
		
		if(a == b) {
			System.out.println("a == b");
		}
		if(a != b) {
			System.out.println("a != b");
		}
		if(a > b) {
			System.out.println("a > b");
		}
		if(a >= b) {
			System.out.println("a >= b");
		}
		if(a < b) {
			System.out.println("a < b");
		}
		if(a <= b) {
			System.out.println("a <= b");
		}

	}
}
a != b
a < b
a <= b

 

 

๐Ÿ›บ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž

'๊ทธ๋ฆฌ๊ณ (AND)' ์™€ '๋˜๋Š”(OR)' ์œผ๋กœ ์กฐ๊ฑด์„ ์—ฐ๊ฒฐํ•œ๋‹ค.

๐Ÿ˜ฎ ๊ฐ™์€ ์กฐ๊ฑด์‹์ด๋ผ๋„ ํ”ผ์—ฐ์‚ฐ์ž์˜ ์œ„์น˜์— ๋”ฐ๋ผ ์—ฐ์‚ฐ ์†๋„๊ฐ€ ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ๋‹ค.

 

Operator Description
! NOT ์—ฐ์‚ฐ
ํ”ผ์—ฐ์‚ฐ์ž๊ฐ€ ์ฐธ์ด๋ฉด ๊ฑฐ์ง“
ํ”ผ์—ฐ์‚ฐ์ž๊ฐ€ ๊ฑฐ์ง“์ด๋ฉด ์ฐธ
&& AND ์—ฐ์‚ฐ
ํ”ผ์—ฐ์‚ฐ์ž ๋ชจ๋‘ ์ฐธ์ด๋ฉด ์ฐธ ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๊ฑฐ์ง“
|| OR ์—ฐ์‚ฐ
ํ”ผ์—ฐ์‚ฐ์ž ๋‘˜์ค‘ ํ•˜๋‚˜๋งŒ ์ฐธ์ด๋ฉด ์ฐธ, ๋ชจ๋‘ ๊ฑฐ์ง“์ด๋ฉด ๊ฑฐ์ง“
x y x && y x || y !x
false false false false true
false true false true true
true false false true false
true true true true false
public class Main {
	public static void main(String[] args) {
		
		int a = 1;
		int b = 2;
		
		if((a == 1) && (b == 2) ) {
			System.out.println("a is 1 AND b is 2");
		}
		if((a == 1) || (b == 1)) {
			System.out.println("a is 1 OR b is 1");
		}

	}
}
a is 1 AND b is 2
a is 1 OR b is 1

 

 

๐ŸšŽ instanceof

instanceof์—ฐ์‚ฐ์ž๋Š” ๊ฐœ์ฒด๋ฅผ ์ง€์ •๋œ ์œ ํ˜•๊ณผ ๋น„๊ตํ•œ๋‹ค.

 

ํ”ผ์—ฐ์‚ฐ์ž์˜ ํ˜•์‹์€ ์ฐธ์กฐํ˜•์‹ ๋˜๋Š” null ํ˜•์‹์ด์–ด์•ผ ํ•˜๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์ปดํŒŒ์ผ ํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

ReferenceType์ด ์ˆ˜์ • ๊ฐ€๋Šฅํ•œ ์ฐธ์กฐ์œ ํ˜•์„ ๋‚˜ํƒ€๋‚ด์ง€ ์•Š์œผ๋ฉด ์ปดํŒŒ์ผ ํƒ€์ž„ ์—๋Ÿฌ์ด๋‹ค.

ReferenceType์— ๋Œ€ํ•œ RelationalExpression์˜ cast๊ฐ€ ์ปดํŒŒ์ผ ์‹œ๊ฐ„ ์˜ค๋ฅ˜๋กœ ๊ฑฐ๋ถ€๋˜๋ฉด instanceof ๊ด€๊ณ„์‹๋„ ์ปดํŒŒ์ผ ํƒ€์ž„ ์—๋Ÿฌ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

๋Ÿฐํƒ€์ž„์— RelationalExpression ๊ฐ’์ด null์ด ์•„๋‹ˆ๋ฉด์„œ ClassCastException์„ ์˜ฌ๋ฆฌ์ง€ ์•Š๊ณ  cast ํ•  ์ˆ˜ ์žˆ์œผ๋ฉด ๊ฒฐ๊ณผ๊ฐ€ ์ฐธ์ด๋‹ค.

 

๐Ÿš‘ assignment(=) operator

๋Œ€์ž… ์—ฐ์‚ฐ์ž๋Š” ์šฐ๋ณ€์˜ ๊ฐ’์„ ์ขŒ๋ณ€์— ์ €์žฅํ•˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค.

๊ฐ์ฒด ์ฐธ์กฐ๋ฅผ ํ• ๋‹นํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ์ฒด์— ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

int number = 10;
System.out.println(number); // 10
Person person = new Person("nyximos", 19200);

 

๋ณตํ•ฉ ๋Œ€์ž… ์—ฐ์‚ฐ์ž

๋Œ€์ž… ์—ฐ์‚ฐ์ž๋ฅผ ๋‹ค๋ฅธ ์—ฐ์‚ฐ์ž์™€ ๊ฒฐํ•ฉํ•˜์—ฌ ์†Œ์Šค๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

Operator Description
= ์™ผ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž์— ์˜ค๋ฅธ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž๋ฅผ ๋Œ€์ž…
+= ์™ผ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž์— ์˜ค๋ฅธ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž ๋”ํ•œ ํ›„,
๋”ํ•œ ๊ฐ’์„ ์™ผ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž์— ๋Œ€์ž… 
-= ์™ผ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž์— ์˜ค๋ฅธ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž ๋บ€ ํ›„,
๋บ€ ๊ฐ’์„ ์™ผ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž์— ๋Œ€์ž… 
*= ์™ผ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž์— ์˜ค๋ฅธ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž ๊ณฑํ•œ ํ›„,
๊ณฑํ•œํ•œ ๊ฐ’์„ ์™ผ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž์— ๋Œ€์ž… 
/= ์™ผ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž์— ์˜ค๋ฅธ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž ๋‚˜๋ˆˆ ํ›„,
๋‚˜๋ˆˆ ๊ฐ’์„ ์™ผ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž์— ๋Œ€์ž… 
%= ์™ผ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž์— ์˜ค๋ฅธ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž ๋‚˜๋ˆˆ ํ›„,
๋‚˜๋จธ์ง€ ๊ฐ’์„ ์™ผ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž์— ๋Œ€์ž… 
&= ์™ผ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž๋ฅผ ์˜ค๋ฅธ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž์™€ ๋น„ํŠธ AND ์—ฐ์‚ฐ ํ›„,
์—ฐ์‚ฐํ•œ ๊ฐ’์„ ์™ผ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž์— ๋Œ€์ž…ํ•จ.
|= ์™ผ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž๋ฅผ ์˜ค๋ฅธ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž์™€ ๋น„ํŠธ OR ์—ฐ์‚ฐ ํ›„,
์—ฐ์‚ฐํ•œ ๊ฐ’์„ ์™ผ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž์— ๋Œ€์ž…ํ•จ.
^= ์™ผ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž๋ฅผ ์˜ค๋ฅธ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž์™€ ๋น„ํŠธ XOR ์—ฐ์‚ฐํ•œ ํ›„, ๊ทธ ๊ฒฐ๊ด๊ฐ’์„ ์™ผ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž์— ๋Œ€์ž…ํ•จ.
<<= ์™ผ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž๋ฅผ ์˜ค๋ฅธ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž๋งŒํผ ์™ผ์ชฝ ์‹œํ”„ํŠธํ•œ ํ›„, ๊ทธ ๊ฒฐ๊ด๊ฐ’์„ ์™ผ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž์— ๋Œ€์ž…ํ•จ.
>>= ์™ผ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž๋ฅผ ์˜ค๋ฅธ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž๋งŒํผ
๋ถ€ํ˜ธ๋ฅผ ์œ ์ง€ํ•˜๋ฉฐ ์˜ค๋ฅธ์ชฝ ์‹œํ”„ํŠธํ•œ ํ›„,
๊ฒฐ๊ด๊ฐ’์„ ์™ผ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž์— ๋Œ€์ž…ํ•จ.
>>>= ์™ผ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž๋ฅผ ์˜ค๋ฅธ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž๋งŒํผ
๋ถ€ํ˜ธ์— ์ƒ๊ด€์—†์ด ์˜ค๋ฅธ์ชฝ ์‹œํ”„ํŠธํ•œ ํ›„,
๊ฒฐ๊ด๊ฐ’์„ ์™ผ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž์— ๋Œ€์ž…ํ•จ.

 

 

๐Ÿš’ ํ™”์‚ดํ‘œ(->) ์—ฐ์‚ฐ์ž

ํ™”์‚ดํ‘œ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋žŒ๋‹ค ํ‘œํ˜„์‹์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

Jaba 8์— ์ถ”๊ฐ€๋œ ๋žŒ๋‹ค์‹์„ ์ง€์›ํ•˜๊ธฐ์œ„ํ•œ ์—ฐ์‚ฐ์ž์ด๋‹ค.

 

Lambda expression ๋žŒ๋‹ค ํ‘œํ˜„์‹

ํ•จ์ˆ˜(๋ฉ”์„œ๋“œ)๋ฅผ ๊ฐ„๋‹จํ•œ ์‹์œผ๋กœ ํ‘œํ˜„ํ•œ๊ฒƒ

// ๊ธฐ์กด ์‹
int max(int a, int b){
	return a > b ? a : b;
}

// ๋žŒ๋‹ค์‹
(a,b) -> a > b ? a: b

 

๐Ÿค” ํ•จ์ˆ˜์™€ ๋ฉ”์„œ๋“œ์˜ ์ฐจ์ด

  • ๊ทผ๋ณธ์ ์œผ๋กœ ๋™์ผํ•˜๋‚˜ ํ•จ์ˆ˜๋Š” ์ผ๋ฐ˜์  ์šฉ์–ด์ด๊ณ , ๋ฉ”์„œ๋“œ๋Š” ๊ฐ์ฒด์ง€ํ–ฅ๊ฐœ๋…์˜ ์šฉ์–ด์ด๋‹ค.
  • ํ•จ์ˆ˜๋Š” ํด๋ž˜์Šค์— ๋…๋ฆฝ์ ์ด๊ณ , ๋ฉ”์„œ๋“œ๋Š” ํด๋ž˜์Šค์— ์ข…์†์ ์ด๋‹ค.

๐Ÿ‘ฉ‍๐Ÿ’ป ๋žŒ๋‹ค์‹ ์ž‘์„ฑํ•˜๊ธฐ

  1. ๋ฉ”์„œ๋“œ ์ด๋ฆ„๊ณผ ๋ฐ˜ํ™˜ํƒ€์ž…์„ ์ œ๊ฑฐํ•˜๊ณ  -> ๋ฅผ ๋ธ”๋ก ์•ž์— ์ถ”๊ฐ€ํ•œ๋‹ค.
  2. ๋ฐ˜ํ™˜๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ ์‹์ด๋‚˜ ๊ฐ’๋งŒ ์ ๊ณ  return๋ฌธ ์ƒ๋žต ๊ฐ€๋Šฅ ( ๋์— ; ์•ˆ ๋ถ™์ธ๋‹ค. )
  3. ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ํƒ€์ž…์ด ์ถ”๋ก  ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ ์ƒ๋žต ๊ฐ€๋Šฅํ•˜๋‹ค. ( ๋Œ€๋ถ€๋ถ„ ์ƒ๋žต ๊ฐ€๋Šฅ)

๐Ÿ˜ฎ ๋žŒ๋‹ค์‹ ์ž‘์„ฑ์‹œ ์ฃผ์˜์‚ฌํ•ญ

  1. ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ํ•˜๋‚˜์ธ ๊ฒฝ์šฐ, ๊ด„ํ˜ธ ( ) ์ƒ๋žต ๊ฐ€๋Šฅ (ํƒ€์ž…์ด ์—†์„ ๋•Œ๋งŒ)
  2. ๋ธ”๋ก ์•ˆ์˜ ๋ฌธ์žฅ์ด ํ•˜๋‚˜ ๋ฟ์ผ ๋•Œ, ๊ด„ํ˜ธ { } ์ƒ๋žต ๊ฐ€๋Šฅ ( ๋์— ; ์•ˆ ๋ถ™์ธ๋‹ค. )
    ํ•˜๋‚˜๋ฟ์ธ ๋ฌธ์žฅ์ด return๋ฌธ์ด๋ฉด ๊ด„ํ˜ธ ์ƒ๋žต ๋ถˆ๊ฐ€๋Šฅ

 

 

๐Ÿšœ 3ํ•ญ ์—ฐ์‚ฐ์ž

์„ธ๊ฐœ์˜ ํ”ผ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์—ฐ์‚ฐ์ž์ด๋‹ค.

์กฐ๊ฑด์‹ ? ์ฒซ๋ฒˆ์งธ์‹ : ๋‘๋ฒˆ์งธ์‹

์กฐ๊ฑด์‹์˜ ๊ฒฐ๊ณผ๊ฐ€ true์ผ๋•Œ ์ฒซ๋ฒˆ์งธ ์‹์ด ๋ฐ˜ํ™˜๋˜๊ณ , false์ผ๋•Œ๋Š” ๋‘๋ฒˆ์งธ์‹์ด ๋ฐ˜ํ™˜๋œ๋‹ค.

 

3ํ•ญ ์—ฐ์‚ฐ์ž๋ฅผ ์ค‘๋ณตํ•ด์„œ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง„๋‹ค.

3ํ•ญ ์—ฐ์‚ฐ์ž๋Š” if๋ฌธ์œผ๋กœ ๋ฐ”๊ฟ” ์“ธ ์ˆ˜ ์žˆ๋‹ค.

public class Main {
	public static void main(String[] args) {
		
		String a = "true";
		String b = "false";
		String result;
		boolean condition = true;
		
		result = condition ? a : b;
		
		System.out.println("condition์€ " + a + "์ด๋‹ค.");

	}
}
condition์€ true์ด๋‹ค.

 

 

๐ŸšŒ์—ฐ์‚ฐ์ž ์šฐ์„  ์ˆœ์œ„

์‹์— ์‚ฌ์šฉ๋œ ์—ฐ์‚ฐ์ž๊ฐ€ ๋‘˜ ์ด์ƒ์ผ ๊ฒฝ์šฐ ์—ฐ์‚ฐ์ž์˜ ์šฐ์„  ์ˆœ์œ„์— ์˜ํ•ด ์—ฐ์‚ฐ ์ˆœ์„œ๊ฐ€ ๊ฒฐ์ •๋œ๋‹ค.

๋™์ผํ•œ ์šฐ์„ ์ˆœ์œ„์˜ ์—ฐ์‚ฐ์ž๊ฐ€ ๋™์ผํ•œ ํ‘œํ˜„์‹์— ๋‚˜ํƒ€๋‚  ๋•Œ ์–ด๋–ค ๊ฒƒ์ด ๋จผ์ € ์—ฐ์‚ฐํ•ด์•ผ๋˜๋Š”์ง€ ๊ฒฐ์ •ํ•ด์•ผ ํ•œ๋‹ค.

ํ• ๋‹น ์—ฐ์‚ฐ์ž์™€ ๋‹จํ•ญ์—ฐ์‚ฐ์ž, ์ฆ๊ฐ€ ์—ฐ์‚ฐ์ž๋Š” ์˜ค๋ฅธ์ชฝ์—์„œ ์™ผ์ชฝ์œผ๋กœ, ์ด์™ธ์˜ ์ดํ•ญ ์—ฐ์‚ฐ์ž๋Š” ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์—ฐ์‚ฐํ•œ๋‹ค.

 

์‚ฐ์ˆ  > ๋น„๊ต > ๋…ผ๋ฆฌ > ๋Œ€์ž…

๋‹จํ•ญ > ์ดํ•ญ > ์‚ผํ•ญ

 

์—ฐ์‚ฐ์ž ์šฐ์„  ์ˆœ์œ„

  Operators   Precedence
1 ์ ‘๋ฏธ์‚ฌ postfix expr++  expr-- 
2 ๋‹จํ•ญ ์—ฐ์‚ฐ์ž unary ++expr  --expr  +expr  -expr  ~  !
3 ๊ณฑ์…ˆ ๋‚˜๋ˆ—์…ˆ ์—ฐ์‚ฐ์ž multiplicative *   /   %
4 ๋ง์…ˆ ๋บ„์…ˆ ์—ฐ์‚ฐ์ž additive +   -
5 ์‰ฌํ”„ํŠธ ์—ฐ์‚ฐ์ž shift <<   >>   >>>
6 ๊ด€๊ณ„(๋น„๊ต) ์—ฐ์‚ฐ์ž relational <   >   <=   >=   instanceof
7 ๊ด€๊ณ„(๋น„๊ต) ์—ฐ์‚ฐ์ž equality ==   !=
8 ๋น„ํŠธ ๋‹จ์œ„์˜ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž bitwise AND &
9 ๋น„ํŠธ ๋‹จ์œ„์˜ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž bitwise exclusive OR ^
10 ๋น„ํŠธ ๋‹จ์œ„์˜ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž bitwise inclusive OR |
11 ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž logical AND &&
12 ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž logical OR ||
13 3ํ•ญ ์—ฐ์‚ฐ์ž ternary ?   :
14 ๋Œ€์ž…, ํ• ๋‹น ์—ฐ์‚ฐ์ž assignment =   +=   -=   *=   /=   %=   &=   ^=
|=   <<=   >>=   >>>=

 

 

๐Ÿšš(optional) Java 13. switch ์—ฐ์‚ฐ์ž

switch statement

์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ์‰ฝ๋‹ค.

break๊ฐ€ ๋ถˆํ•„์š”ํ•˜๊ฒŒ ๋“ค์–ด๊ฐ€ ์žฅํ™ฉํ•˜๋‹ค. → ๋””๋ฒ„๊น…ํ•˜๊ธฐ ์–ด๋ ต๋‹ค.

break๊ฐ€ ์—†์œผ๋ฉด ์˜๋„์น˜ ์•Š๊ฒŒ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

int numLetters;
switch (day) {
    case MONDAY:
    case FRIDAY:
    case SUNDAY:
        numLetters = 6;
        break;
    case TUESDAY:
        numLetters = 7;
        break;
    case THURSDAY:
    case SATURDAY:
        numLetters = 8;
        break;
    case WEDNESDAY:
        numLetters = 9;
        break;
    default:
        throw new IllegalStateException("Wat: " + day);
}

์ด๊ฒƒ์„ ๋ฌธ์žฅ์œผ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ์€ ์šฐํšŒ์ ์ด๊ณ  ๋ฐ˜๋ณต์ ์ด๋ฉฐ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ์‰ฝ๋‹ค.

Case L -> ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

int numLetters = switch (day) {
    case MONDAY, FRIDAY, SUNDAY -> 6;
    case TUESDAY                -> 7;
    case THURSDAY, SATURDAY     -> 8;
    case WEDNESDAY              -> 9;
};

 

 

 

Case L -> ์˜ค๋ฅธ์ชฝ์—๋Š” ๋‹จ์ผ ํ‘œํ˜„์‹์„ ๊ฐ€์ง€๋Š”๋ฐ

full block์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ์— ๊ฐ’์€ ์‚ฐ์ถœํ•˜๊ธฐ ์œ„ํ•œ yield๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

๊ธฐ์กด์˜ case L:๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

์ฐธ์กฐ

3์ฃผ์ฐจ ๊ณผ์ œ: ์—ฐ์‚ฐ์ž

๋‚จ๊ถ์„ฑ, ์ž๋ฐ”์˜ ์ •์„ ๊ธฐ์ดˆํŽธ, ๋„์šฐ์ถœํŒ, 2019

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

 

Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)

The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available. See Java Language Changes for a summary of updated

docs.oracle.com

 

์‚ผํ•ญ ์—ฐ์‚ฐ์ž - https://ssdragon.tistory.com/4

 

Java ์‚ผํ•ญ ์—ฐ์‚ฐ์ž(์กฐ๊ฑด ์—ฐ์‚ฐ์ž) - Conditional Operator

์‚ผํ•ญ ์—ฐ์‚ฐ์ž(์กฐ๊ฑด ์—ฐ์‚ฐ์ž)๋ž€ ๋ฌด์—‡์ธ๊ฐ€? ์‚ผํ•ญ ์—ฐ์‚ฐ์ž(์กฐ๊ฑด ์—ฐ์‚ฐ์ž)๋Š” Java 8 Language Specification์—์„œ Conditional Operator ๋ผ๊ณ  ์ •์˜๋˜์–ด ์žˆ๋‹ค. ์•„๋ž˜ ์‚ฌ์ง„์˜ oracle ๊ณต์‹๋ฌธ์„œ์— ๋”ฐ๋ฅด๋ฉด ์กฐ๊ฑด ์—ฐ์‚ฐ์ž๋Š” ํ•œ ํ‘œํ˜„

ssdragon.tistory.com

 

 

Switch Expressions - https://openjdk.java.net/jeps/354

 

JEP 354: Switch Expressions (Second Preview)

JEP 354: Switch Expressions (Second Preview) AuthorGavin BiermanOwnerJan LahodaTypeFeatureScopeSEStatusClosed / DeliveredRelease13Componentspecification / languageDiscussionamber dash dev at openjdk dot java dot netEffortSDurationMRelates toJEP 325

openjdk.java.net

Switch Expressions - https://catch-me-java.tistory.com/31