Skip to content

Instantly share code, notes, and snippets.

@ALRubinger
Last active August 29, 2015 14:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ALRubinger/e14a3f905561f9015575 to your computer and use it in GitHub Desktop.
Save ALRubinger/e14a3f905561f9015575 to your computer and use it in GitHub Desktop.
Java Puzzler
public class NullVarArgs {
public static void main(String... args) {
// 1
exec(null);
// 2
exec((String) null);
// 3
final String s = null;
exec(s);
}
private static void exec(String... args) {
System.out.println(args);
}
}
@ALRubinger
Copy link
Author

Output:

null
[Ljava.lang.String;@5a07e868
[Ljava.lang.String;@76ed5528

@qmx
Copy link

qmx commented Jun 26, 2014

// class version 52.0 (52)
// access flags 0x21
public class meh/NullVarArgs {

  // compiled from: NullVarArgs.java

  // access flags 0x1
  public <init>()V
   L0
    LINENUMBER 3 L0
    ALOAD 0
    INVOKESPECIAL java/lang/Object.<init> ()V
    RETURN
   L1
    LOCALVARIABLE this Lmeh/NullVarArgs; L0 L1 0
    MAXSTACK = 1
    MAXLOCALS = 1

  // access flags 0x89
  public static transient varargs main([Ljava/lang/String;)V
   L0
    LINENUMBER 8 L0
    ACONST_NULL
    INVOKESTATIC meh/NullVarArgs.exec ([Ljava/lang/String;)V
   L1
    LINENUMBER 11 L1
    ICONST_1
    ANEWARRAY java/lang/String
    DUP
    ICONST_0
    ACONST_NULL
    CHECKCAST java/lang/String
    AASTORE
    INVOKESTATIC meh/NullVarArgs.exec ([Ljava/lang/String;)V
   L2
    LINENUMBER 14 L2
    ACONST_NULL
    ASTORE 1
   L3
    LINENUMBER 15 L3
    ICONST_1
    ANEWARRAY java/lang/String
    DUP
    ICONST_0
    ALOAD 1
    AASTORE
    INVOKESTATIC meh/NullVarArgs.exec ([Ljava/lang/String;)V
   L4
    LINENUMBER 16 L4
    RETURN
   L5
    LOCALVARIABLE args [Ljava/lang/String; L0 L5 0
    LOCALVARIABLE s Ljava/lang/String; L3 L5 1
    MAXSTACK = 4
    MAXLOCALS = 2

  // access flags 0x8A
  private static transient varargs exec([Ljava/lang/String;)V
   L0
    LINENUMBER 20 L0
    GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
    ALOAD 0
    INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/Object;)V
   L1
    LINENUMBER 21 L1
    RETURN
   L2
    LOCALVARIABLE args [Ljava/lang/String; L0 L2 0
    MAXSTACK = 2
    MAXLOCALS = 1
}

@qmx
Copy link

qmx commented Jun 26, 2014

Warning:(8, 14) java: non-varargs call of varargs method with inexact argument type for last parameter;
cast to java.lang.String for a varargs call
cast to java.lang.String[] for a non-varargs call and to suppress this warning

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment