Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable 'final' modifier lost in Bytecode?

Analyzing the bytecode of this simple class, I have come to the conclusion that the compiler doesn't retain any information about a local variable being final. This seems weird though, since I believe the HotSpot compiler could actually use this information to do optimizations.

Code:

public static void main(String[] args)
{
    final int i = 10;
    System.out.println(i);
}

Bytecode:

public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
  stack=2, locals=2, args_size=1
     0: bipush        10
     2: istore_1      
     3: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream;
     6: bipush        10
     8: invokevirtual #22                 // Method java/io/PrintStream.println:(I)V
    11: return        
  LineNumberTable:
    line 7: 0
    line 8: 3
    line 9: 11
  LocalVariableTable:
    Start  Length  Slot  Name   Signature
        0      12     0  args   [Ljava/lang/String;
        3       9     1     i   I

Is there any specific reason not to retain the access flags of a local variable, other than saving disk space? Because to me, it seems that being final is a relatively non-trivial property of a variable.

like image 943
Clashsoft Avatar asked May 10 '15 12:05

Clashsoft


People also ask

What is the use of modifier final in method?

The final modifier keyword makes that the programmer cannot change the value anymore. The actual meaning depends on whether it is applied to a class, a variable, or a method.

Where can the final modifier be applied?

The final is a modifier in Java, which can be applied to a variable, a method, or a class. Though, you can have a final block. Also, you can use the final modifier with local variables, class variables, as well as with instance variables.

What does the final modifier in Java do?

The final keyword is a non-access modifier used for classes, attributes and methods, which makes them non-changeable (impossible to inherit or override). The final keyword is useful when you want a variable to always store the same value, like PI (3.14159...). The final keyword is called a "modifier".

Should we use final in Java?

In Java, the final keyword can be used while declaring an entity. Using the final keyword means that the value can't be modified in the future. This entity can be - but is not limited to - a variable, a class or a method.


1 Answers

The final modifier is not present in the bytecode but the compiler already uses this information to make some optimization. Although your example doesn't show it, the compiler may inline the final variable's value in the bytecode representation of the method, leading to a better performance. Something like the below can show the difference:

public int addFinal() {
    final int i = 10;
    final int j = 10;
    return i + j;
}

public int addNonFinal() {
    int i = 10;
    int j = 10;
    return i + j;
}

The generated bytecode are respectively for each method:

// addFinal
bipush 10
istore_1
bipush 10
istore_2
bipush 20
ireturn


// addNonFinal
bipush 10
istore_1
bipush 10
istore_2
iload_1
iload_2
iadd
ireturn
like image 93
M A Avatar answered Oct 04 '22 21:10

M A