Examine bytecode

Examination of the bytecode of a class is not a common task. However, if you're interested, it's possible to view bytecode using the javap tool included in the JDK. Here's an example of its use, where it shows the effect on bytecode of setting fields explicitly to their default initial values:

>javap -c -classpath . Quark
Compiled from Quark.java
public final class Quark extends java.lang.Object {
    public Quark(java.lang.String,double);
}

Method Quark(java.lang.String,double)
   0 aload_0
   1 invokespecial #1 <Method java.lang.Object()>
   4 aload_0
   5 aconst_null
   6 putfield #2 <Field java.lang.String fName>
   9 aload_0
  10 dconst_0
  11 putfield #3 <Field double fMass>
  14 aload_0
  15 aload_1
  16 putfield #2 <Field java.lang.String fName>
  19 aload_0
  20 dload_2
  21 putfield #3 <Field double fMass>
  24 return

Here is the source class itself :


public final class Quark {

  public Quark(String aName, double aMass){
    fName = aName;
    fMass = aMass;
  }

  //PRIVATE

  //WITHOUT redundant initialization to default values
  //private String fName;
  //private double fMass;

  //WITH redundant initialization to default values
  private String fName = null;
  private double fMass = 0.0d;
} 


Bytecode instructions are defined by the JVM specification.

See Also :
Initializing fields to 0-false-null is redundant
Would you use this technique?
Yes   No   Undecided   
© 2014 Hirondelle Systems | Source Code | Contact | License | RSS
Individual code snippets can be used under this BSD license - Last updated on September 21, 2013.
Over 2,000,000 unique IPs last year - Built with WEB4J.
- In Memoriam : Bill Dirani -