Is it possible to pass null array to Java varargs?

Viewed 792

I have the following code in Java:

  public static void main(String[] args) {
    checkVarargs(null);
  }

  public static void checkVarargs(String... o) {
    System.out.println(o);
  }

When I try to auto-convert the main method to Kotlin I get the following:

    @JvmStatic
    fun main(args: Array<String>) {
      checkVarargs(null)
    }

However, In the Java case a null array is passed, and it prints null while on Kotlin case it prints [Ljava.lang.String;@548c4f57 (array that contains null).

Is it possible to pass null array to Java varargs from Kotlin code?

1 Answers

The question is, why does Java behave like this? Does it make sense? Don't you want to pass a null String rather? Kotlin doesn't support this vague concept of "nullable varargs", you cannot achieve it as you could in Java.

checkVarargs() will pass an empty array

checkVarargs(null) will pass an array with null in it

Related