스칼라 if문
if (2 + 2 == 5) {
println("Hello from 1984.")
} else if (2 + 2 == 3) {
println("Hello from Remedial Math class?")
} else {
println("Hello from a non-Orwellian future.")
}
val configFile = new java.io.File("somefile.txt")
val configFilePath = if (configFile.exists()) {
configFile.getAbsolutePath()
} else {
configFile.createNewFile()
configFile.getAbsolutePath()
}
- 스칼라는 문장이 값을 결과로 돌려주는 식이다.
- if 식의 결과 값을 다른 변수에 저장할 수 있다.
- configFilePath는 설정 파일이 존재하지 않는 경우를 내부에서 처리하고 절대 경로를 돌려주는 if문이다.
- 3항 조건식은 중복 기능이므로 지원하지 않는다.