How does java store literals?

user12208242 :
System.out.println(5678);

This literal is directly used in the printing statement. But does java store it directly in a memory or it creates an automatic variable and then stores it there? If the second case is to be true, what would happen if anyone happens to access that variable accidentally using the same variable name?

Zabuza :

Explanation

Whatever Java does under the hood is completely hidden from the user and programmer.

As long as you are still writing Java code, it is impossible to mess with it. Might be a different story of course if you try to hookup the JVM process and inject C code, or interact with it via its native interface.

It is also completely up to the JVM how it actually handles this in memory. Memory management is fully hidden from the programmer in Java.


Bytecode

Variable inlined

That being said, let us take a look at the resulting byte code for this snippet (see javabytes.io or use javap -c Test.class):

// Source code
public class Test {
    public static void main(String [] args) {
        int value = 4000;
        System.out.println(value);

        System.out.println(5678);
    }
}

// Byte code
public class Test {
  public Test();
    Code:
       0: aload_0
       1: invokespecial #1    // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: sipush        4000
       3: istore_1
       4: getstatic     #2    // Field java/lang/System.out:Ljava/io/PrintStream;
       7: iload_1
       8: invokevirtual #3    // Method java/io/PrintStream.println:(I)V
      11: getstatic     #2    // Field java/lang/System.out:Ljava/io/PrintStream;
      14: sipush        5678
      17: invokevirtual #3    // Method java/io/PrintStream.println:(I)V
      20: return
}

As you see, both variants are actually the same. The sipush command loads the values directly, no sign of any variables at all.

sipush pushes a short directly onto the stack, from where it is then picked up by the invokevirtual which calls the print method (see Java bytecode instruction list for details).

Why is that the case? Well, the compiler is smart. It figured that the variable value serves no purpose and actually completely got rid of it. It changed the code to System.out.println(4000) and removed value completely.

Variable present

But we want to see some variables, so let us make it a bit more complex so that Java does not inline the variable anymore by introducing a dependency that can only be computed at run-time:

// Source code
public class Test {
    public static void main(String [] args) {
        int value = (int) System.currentTimeMillis();
        System.out.println(value);

        System.out.println(5678);
    }
}

// Byte code
public class Test {
  public Test();
    Code:
       0: aload_0
       1: invokespecial #1    // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #2    // Method java/lang/System.currentTimeMillis:()J
       3: l2i
       4: istore_1
       5: getstatic     #3    // Field java/lang/System.out:Ljava/io/PrintStream;
       8: iload_1
       9: invokevirtual #4    // Method java/io/PrintStream.println:(I)V
      12: getstatic     #3    // Field java/lang/System.out:Ljava/io/PrintStream;
      15: sipush        5678
      18: invokevirtual #4    // Method java/io/PrintStream.println:(I)V
      21: return
}   

Finally we see some variable action! The variable is computed by the method, converted to int and then stored via istore_1. Then, it is loaded dynamically onto the stack by iload_1 and passed to the print method.

So via variable, we need to istore and iload it to the method. With a literal, we can directly load it to the method using sipush.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=305683&siteId=1