[Kotlin] If 와 When 은 Expression 이다.

[Kotlin] If 와 When 은 Expression 이다.

IF

자바의 IF 문은 Statement (문) 이고

코틀린의 IF 문은 Expression (식) 이다.

Expression VS Statement

Expression => 값을 생성 한다.

한다. Statement => 값을 생성하지 않는다.

코틀린에서의 if 는 아래처럼 값을 생성하여 할당할 수 있다. -> Expression

val kotlinIf = if(true) 1 else 2

자바에서의 if 는 값을 생성하여 할당할 수는 없다.

Expression 과는 다르게 내가 직접 return 을 명시해줘야 한다.

public static void main(String[] args) { int a = statement(true); } private static int statement(boolean bool) { if (bool) { return 1; } else { return 2; } }

from http://robin00q.tistory.com/105 by ccl(A) rewrite - 2021-12-05 14:27:57