Question

I was reading this wiki article on how some code is translated to Java bytecode. I came across this example:

Consider the following Java code:

  outer:
  for (int i = 2; i < 1000; i++) {
      for (int j = 2; j < i; j++) {
          if (i % j == 0)
              continue outer;
      }
      System.out.println (i);
  }

A Java compiler might translate the Java code above into byte code as follows, 
assuming the above was put in a method:

  0:   iconst_2
  1:   istore_1
  2:   iload_1
  3:   sipush  1000
  6:   if_icmpge       44
  9:   iconst_2
  10:  istore_2
  11:  iload_2
  12:  iload_1
  13:  if_icmpge       31
  16:  iload_1
  17:  iload_2
  18:  irem
  19:  ifne    25
  22:  goto    38
  25:  iinc    2, 1
  28:  goto    11
  31:  getstatic       #84; //Field java/lang/System.out:Ljava/io/PrintStream;
  34:  iload_1
  35:  invokevirtual   #85; //Method java/io/PrintStream.println:(I)V
  38:  iinc    1, 1
  41:  goto    2
  44:  return
  1. I do not understand a single line of the bytecode. I want to understand some part of it if not all. or what some of these lines mean such as iconst_2?
  2. is bytecode and .class file same or .class files contains bytecode?. The picture below shows both .class and bytecode are same
  3. If they are different, how is bytecode extracted from .class files by the JVM?

some of the posts in SO talk about bytecode in general, but I do not see any post that explains the relationship between classand bytecode if any and how to read bytecode contents as a user (not as a JVM).enter image description here

Was it helpful?

Solution

Sometimes I find there is an assumption it has to be terribly complication when it is not. It is actually very simple.

I do not understand a single line of the bytecode. I want to understand some part of it if not all. or what some of these lines mean such as iconst_2?

iconst_2 stands for the integer constant 2

is bytecode and .class file same or .class files contains bytecode?.

The .class contains more than byte code. It also contains some constants e.g. String literals and large primitives. However, you can treat them as one and the same.

The picture below shows both .class and bytecode are same

The diagram is a simplified view. In simple terms they are the same thing.

If they are different, how is bytecode extracted from .class files by the JVM?

A .class has a specific format which if you follow that format you will find the byte code. As you can see there is more than just byte code in the file, but the byte code is the only bit you should care about. (Actually you shouldn't really care about the byte code in 99% of cases)

From the Class file format linked above.

A class file consists of a single ClassFile structure:

ClassFile {
    u4 magic;
    u2 minor_version;
    u2 major_version;
    u2 constant_pool_count;
    cp_info constant_pool[constant_pool_count-1];
    u2 access_flags;
    u2 this_class;
    u2 super_class;
    u2 interfaces_count;
    u2 interfaces[interfaces_count];
    u2 fields_count;
    field_info fields[fields_count];
    u2 methods_count;
    method_info methods[methods_count];
    u2 attributes_count;
    attribute_info attributes[attributes_count];
}

OTHER TIPS

The best way to learn about bytecode and the classfile format is to just read the JVM specification. It's freely available online and not that hard to understand (you can skip all the Prolog junk). That's how I got started. After that, it's just a matter of practice and poking around. You can see how it's used in practice by compiling and disassembling various classes.

Technically, bytecode only refers to the actual instructions in the Code attributes, but it's pretty much meaningless without the rest of the classfile, so usually when I say bytecode, I'm referring to the entire classfile format.

I also wrote a bytecode tutorial from the perspective of writing bytecode in Krakatau assembler. Unfortunately, it doesn't go very far because I stopped writing it due to lack of interest, but it may still help. You can find it here

I could recommend a book "Principles of Computer Organization and Assembly Language":

Today’s incoming students are more likely to be exposed to Java than ever before. Focusing on a modern architecture (the Java Virtual Machine, or JVM), this text provides a thorough treatment of the principles of computer organization in the context of today’s portable computer. Students are given simple but realistic examples to gain a complete understanding of how computation works on such a machine. Juola makes the material useful and relevant in a course that is often difficult for second-year CS students.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top