When does the binary operators execution happen in Java?

后端 未结 1 593
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-04 18:24

I\'m trying to understand java byte code. I started with simple example:

public class Test
{
    public static void main(String args[])
    {
        System.         


        
相关标签:
1条回答
  • 2021-01-04 19:08

    2 + 1 is a compile-time constant expression. The compiler itself replaces it by 3 in the byte-code.

    See the Java Language Specification, which says:

    Some expressions have a value that can be determined at compile time. These are constant expressions.

    See this other chapter for what constitutes a constant expression

    A constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:

    • Literals of primitive type and literals of type String [...]
    • The additive operators + and - [...]
    0 讨论(0)
提交回复
热议问题