Clever Geek Handbook
πŸ“œ ⬆️ ⬇️

Java Bytecode

Java bytecode is a set of instructions executed by a Java virtual machine . Each bytecode opcode is one byte. Not all 256 possible values ​​of operation codes are used. 51 of them are reserved for future use.

Content

Java Communication

For programming in Java or other JVM-compatible languages, knowledge of the bytecode features is optional. However, as follows from a publication in the IBM developerWorks magazine, "understanding the bytecode and understanding how it is generated by the Java compiler helps the Java programmer as well as knowing the assembler language helps the C or C ++ programmer ." [1] [2]

Instructions

One byte has 256 possible values, so there are a total of 256 possible operation codes in the bytecode. The CA 16 code is reserved for use by the debugger and is not used in language, as are the FE 16 and FF 16 codes, which are reserved for use by the virtual machine and debuggers. Codes in the CB 16 β€” FD 16 range are not used in the current version of the JVM and are reserved for future additions.

Instructions can be divided into several groups:

  • loading and saving (e.g. ALOAD_0 , ISTORE ),
  • arithmetic and logical operations (for example, IADD , FCMPL ),
  • type conversion (e.g. I2B , D2I ),
  • creation and transformation of an object (for example, NEW , PUTFIELD ),
  • stack management (e.g. DUP , POP ),
  • jump operators (e.g. GOTO , IFEQ ),
  • method calls and returns (e.g. INVOKESTATIC , IRETURN ).

There are also several instructions that perform specific tasks, such as throwing exceptions, synchronizing, and so on.

Many instructions have prefixes or suffixes corresponding to their operands:

Prefix or suffixType of operand
Iinteger
Llong
Sshort
Bbyte
Ccharacter
Ffloat
Ddouble
Areference

For example, the operation IADD is the addition of two integers, while FADD is the addition of floating-point numbers.

Example

Consider the following Java code example.

  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 );
   }

The Java compiler can translate the above Java code into the following bytecode:

  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
 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 

Generation

In most cases, Java bytecode is generated for execution on the Java virtual machine from Java source code. The only original compiler to convert Java code to Java bytecode is Javac , created by Sun Microsystems. But since all Java bytecode specifications are available, there are third-party compilers that generate this bytecode. Examples of such compilers:

  • Jikes - compiles Java code into Java bytecode (developed by IBM , written in C ++ ),
  • Espresso - compiles Java code into Java bytecode (for Java version 1.0),
  • GCJ ( G NU C ompiler for J ava) - compiles Java code into Java bytecode, is also able to compile into native machine code. It is part of the GNU Compiler Collection .

For some projects, compilers exist that allow you to generate bytecode for the JVM from source code in another programming language. Among these projects:

  • ColdFusion
  • JRuby and Jython (for Ruby and Python respectively),
  • Groovy (a Java-based scripting language),
  • Scala ,
  • Kotlin ,
  • JGNAT and AppletMagic ,
  • C compilers in Java bytecode,
  • Clojure
  • MIDletPascal ,
  • JavaFX Script .

Execution

The Java bytecode is designed to run on the Sun Java Virtual Machine (JVM, J ava V irtual M achine). Various JVM implementations, both free and commercial, are also available today.

If JVM bytecode execution is undesirable, Java or Java source code can be compiled directly into native machine code using, for example, the GNU Java compiler . Some processors can directly execute Java bytecode.

Support for languages ​​with dynamic typing

The Java virtual machine has support for languages ​​with dynamic typing . Most existing JVM instructions have static typing . The method signatures in the places they are called are checked at compile time. There is no mechanism to transfer this check to runtime. [3]

See also

  • Bytecode
  • Java virtual machine
  • Java

Links

  • Chapter 6. The Java Virtual Machine Instruction Set

Notes

  1. ↑ Peter Haggar, Understanding bytecode makes you a better programmer // IBM DeveloperWorks, 01 Jul 2001
  2. ↑ A Formal Introduction to the Compilation of Java, Stephan Diehl, "Software - Practice and Experience", Vol. 28 (3), pages 297-327, March 1998.
  3. ↑ Nutter, Charles InvokeDynamic: Actually Useful? (unspecified) (January 3, 2007). Date of treatment January 25, 2008. Archived April 30, 2013.
Source - https://ru.wikipedia.org/w/index.php?title=Java&oldid=87708935 bytecode


More articles:

  • Vintage Dead
  • Aeneas Gazsky
  • Historic Dead
  • Gorsky, Alexander Konstantinovich
  • Transfiguration Plant
  • Principality of Mezets
  • Lukovsky Village Council (Altai Territory)
  • City settlement Zmeinogorsk city
  • Matsourek, Milos
  • Mvula, Laura

All articles

Clever Geek | 2019