Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Post Timeline

added 69 characters in body
Source Link
Matt Ball
  • 361k
  • 102
  • 655
  • 725

If you're concerned just about the static compilation step, and not the JITthe JIT, this is simple to check by comparing the bytecode generated from two slightly different classes, using javap:

class WithLocalVar {
 
 private static int methodWithSideEffect() {
 System.out.println();
 return 42;
 }
 public static void main(String[] args) {
 int result = methodWithSideEffect();
 }
}
class WithoutLocalVar {
 
 private static int methodWithSideEffect() {
 System.out.println();
 return 42;
 }
 public static void main(String[] args) {
 methodWithSideEffect();
 }
}
✗ javac With*
✗ javap -c WithLocalVar
Compiled from "WithLocalVar.java"
class WithLocalVar extends java.lang.Object{
WithLocalVar();
 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 #4; //Method methodWithSideEffect:()I
 3: istore_1
 4: return
}
✗ javap -c WithoutLocalVar
Compiled from "WithoutLocalVar.java"
class WithoutLocalVar extends java.lang.Object{
WithoutLocalVar();
 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 #4; //Method methodWithSideEffect:()I
 3: pop
 4: return
}

Therefore, no, the compiler won't optimize away the istore_1. The JIT is another story...The JIT is another story...

If you're concerned just about the static compilation step, and not the JIT, this is simple to check by comparing the bytecode generated from two different classes, using javap:

class WithLocalVar {
 
 private static int methodWithSideEffect() {
 System.out.println();
 return 42;
 }
 public static void main(String[] args) {
 int result = methodWithSideEffect();
 }
}
class WithoutLocalVar {
 
 private static int methodWithSideEffect() {
 System.out.println();
 return 42;
 }
 public static void main(String[] args) {
 methodWithSideEffect();
 }
}
✗ javac With*
✗ javap -c WithLocalVar
Compiled from "WithLocalVar.java"
class WithLocalVar extends java.lang.Object{
WithLocalVar();
 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 #4; //Method methodWithSideEffect:()I
 3: istore_1
 4: return
}
✗ javap -c WithoutLocalVar
Compiled from "WithoutLocalVar.java"
class WithoutLocalVar extends java.lang.Object{
WithoutLocalVar();
 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 #4; //Method methodWithSideEffect:()I
 3: pop
 4: return
}

Therefore, no, the compiler won't optimize away the istore_1. The JIT is another story...

If you're concerned just about the static compilation step, and not the JIT, this is simple to check by comparing the bytecode generated from two slightly different classes, using javap:

class WithLocalVar {
 
 private static int methodWithSideEffect() {
 System.out.println();
 return 42;
 }
 public static void main(String[] args) {
 int result = methodWithSideEffect();
 }
}
class WithoutLocalVar {
 
 private static int methodWithSideEffect() {
 System.out.println();
 return 42;
 }
 public static void main(String[] args) {
 methodWithSideEffect();
 }
}
✗ javac With*
✗ javap -c WithLocalVar
Compiled from "WithLocalVar.java"
class WithLocalVar extends java.lang.Object{
WithLocalVar();
 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 #4; //Method methodWithSideEffect:()I
 3: istore_1
 4: return
}
✗ javap -c WithoutLocalVar
Compiled from "WithoutLocalVar.java"
class WithoutLocalVar extends java.lang.Object{
WithoutLocalVar();
 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 #4; //Method methodWithSideEffect:()I
 3: pop
 4: return
}

Therefore, no, the compiler won't optimize away the istore_1.The JIT is another story...

Source Link
Matt Ball
  • 361k
  • 102
  • 655
  • 725

If you're concerned just about the static compilation step, and not the JIT, this is simple to check by comparing the bytecode generated from two different classes, using javap:

class WithLocalVar {
 
 private static int methodWithSideEffect() {
 System.out.println();
 return 42;
 }
 public static void main(String[] args) {
 int result = methodWithSideEffect();
 }
}
class WithoutLocalVar {
 
 private static int methodWithSideEffect() {
 System.out.println();
 return 42;
 }
 public static void main(String[] args) {
 methodWithSideEffect();
 }
}
✗ javac With*
✗ javap -c WithLocalVar
Compiled from "WithLocalVar.java"
class WithLocalVar extends java.lang.Object{
WithLocalVar();
 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 #4; //Method methodWithSideEffect:()I
 3: istore_1
 4: return
}
✗ javap -c WithoutLocalVar
Compiled from "WithoutLocalVar.java"
class WithoutLocalVar extends java.lang.Object{
WithoutLocalVar();
 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 #4; //Method methodWithSideEffect:()I
 3: pop
 4: return
}

Therefore, no, the compiler won't optimize away the istore_1. The JIT is another story...

lang-java

AltStyle によって変換されたページ (->オリジナル) /