1. Java에서의 null 체크

// case 1
public boolean checkStarts(String str) {
  return str.startsWith("A");
}

// case 2
public boolean checkStarts(String str) {
  if (str == null) {
    throw new IllegalArgumentException("null이 들어와선 안됩니다.");
  }
  return str.startsWith("A");
}

// case 3
public boolean checkStarts(String str) {
  if (str == null) {
    return false;
  }
  return str.startsWith("A");
}

// case 4
public Boolean checkStarts(String str) {
  if (str == null) {
    return null;
  }
  return str.startsWith("A");
}

2. Kotlin에서의 null 체크 - Safe Call

// case 1
fun checkStarts(str: String): Boolean {
  return str.startsWith("A")
}

// case 2
fun checkStarts(str: String?): Boolean {
  if (str == null){
    throw IllegalArgumentException("null이 들어와선 안됩니다.")
  }
  return str.startsWith("A")
}

// case 3
fun checkStarts(str: String?): Boolean {
  if (str == null){
    return false
  }
  return str.startsWith("A")
}

// case 4
fun checkStarts(str: String?): Boolean? {
//  if (str == null){
//    return null
//  }
//  return str.startsWith("A")
  return str?.startsWith("A")
}

3. Kotlin에서의 null 체크 - Safe Call + Elvis

// case 2
fun checkStarts(str: String?): Boolean {
  return str?.startsWith("A")
    ?: throw IllegalArgumentException("null이 들어와선 안됩니다.")
}

// case 3
fun checkStarts(str: String?): Boolean {
  return str?.startsWith("A") ?: false
}