SlideShare a Scribd company logo
JAVED AHMED SAMO
Course Title
Presented to:
Presented by:
JAVA BYTE CODE
• Bytecode is an intermediate code that runs on JVM
• A Java virtual machine (JVM) is an abstract
computing machine that enables a computer to run
a Java program
JAVA BYTE CODE
JAVA BYTE CODE
JAVA BYTE CODE
JAVA BYTE CODE
JAVA BYTE CODE
JAVA BYTE CODE
P-code (various forms of instruction sets designed for efficient execution by a
software interpreter as well as being suitable for further compilation into machine
code. /
Java bytecode is the instruction set of the Java virtual machine. Each bytecode is
composed by one, or in some cases two, bytes that represent the instruction (opcode),
along with zero or more bytes for passing parameters.
Advantages of Bytecode
• Bytecode is architecture independent (and writing a VM is easier than rewriting a
compiler for every architecture)
• Just In-Time (JIT) compiling helps achieve same or better speed than traditional
compiled code
Introduction to Byte Code
Other languages that have an intermediate representation
• C#-----------------MSIL
• Perl ---------------perlcompiler
• PHP ---------------bcompiler
• Python -------------
• Jikes, compiles from Java to Java bytecode (developed by IBM, implemented in C++)
• Espresso, compiles from Java to Java bytecode (Java 1.0 only)
• GCJ, the GNU Compiler for Java, compiles from Java to Java bytecode; it is also able to compile to
native machine code and is available as part of the GNU Compiler Collection (GCC).
• Some projects provide Java assemblers to enable writing Java bytecode by hand. Assembly code may be
also generated by machine, for example by a compiler targeting a Java virtual machine. Notable Java
assemblers include:
• Jasmin, takes textual descriptions for Java classes, written in a simple assembly-like syntax using
Java Virtual Machine instruction set and generates a Java class file
• Jamaica, a macro assembly language for the Java virtual machine. Java syntax is used for class or
interface definition. Method bodies are specified using bytecode instructions.
• Krakatau currently contains three tools: a decompiler and disassembler for Java classfiles and an
assembler to create classfiles
• Lilac, an assembler and disassembler for the Java virtual machine.
Some projects provide Java assemblers to enable writing Java bytecode
• JVM-Types and their prefixes
▫ Byte b
▫ Short s
▫ Integer i (java booleans are mapped to jvm ints!)
▫ Long l
▫ Character c
▫ Single float f
▫ double float d
▫ References a to Classes, Interfaces, Arrays
• These Prefixes used in opcodes (iadd, astore,...)
Bytecode Basics
Instructions
The JVM has 256 instructions for:
•Arithmetic operations
•Branch operations
•Constant loading operations
•Locals operations
•Stack operations
•Class operations
•Method operations
Of the 256 possible byte-long opcodes, as of 2015, 198 are in use (~77%),
54 are reserved for future use, and 3 instructions (~1%) are set aside as
permanently unimplemented.
Instructions fall into a number of broad groups:
•Load and store (e.g. aload_0, istore)
•Arithmetic and logic (e.g. ladd, fcmpl)
•Type conversion (e.g. i2b, d2i)
•Object creation and manipulation (new, putfield)
•Operand stack management (e.g. swap, dup2)
•Control transfer (e.g. ifeq, goto)
•Method invocation and return (e.g. invokespecial,
areturn)
Instructions Conti…
▫ Byte b
▫ Short s
▫ Integer i
▫ Long l
▫ Character c
▫ Single float f
▫ double float d
▫ References a
Arithmetic Operations
Operands Operations
Instructions Conti…
Java Bytecode Explanation
Opcode Mnemonic Description
0 nop Does nothing
1 aconst_null Push null on the stack
3 iconst_0 Push int 0 on the stack
4 iconst_1 Push int 1 on the stack
18 ldc <value> Push a one-word (4 bytes)
constant onto the stack
ldc “Hello”
ldc 201
Constant may be an int, float or String
Instructions Conti…
Other types of Instructions
• Control Flow (~20 instructions)
– if, goto, return
• Method Calls (4 instructions)
• Loading and Storing Variables (65
instructions)
• Creating objects (1 instruction)
• Using object fields (4 instructions)
• Arrays (3 instructions)
Javap examples
public class Test1 {
public int add(int a, int b) {
int c= a+b;
return c;
}
}
javap -c Test1javac -g Test1.java
Javap included with Java Development Kit (JDK)
…
public int add(int, int);
Code:
0: iload_1
1: iload_2
2: iadd
3: istore_3
4: iload_3
5: ireturn
// add var 1 and 2
// store as local var 3
// store onto stack
// return int
// push onto stack
// push onto stack
public class Test1 {
public int add(int a, int b) {
int c= a+b;
return c;
}
}
…
public int add(int a, int b)
{
int c = a + b;
// 0 0:iload_1
// 1 1:iload_2
// 2 2:iadd
// 3 3:istore_3
return c;
// 4 4:iload_3
// 5 5:ireturn
}
jad -a Test1javac -g Test1.java
JAD is free, but not included with Java Development Kit (JDK)
JAD examples
JAVA BYTE CODE

More Related Content

PPTX
Java bytecode and classes
PPTX
Java byte code & virtual machine
PDF
Introduction to the Java bytecode - So@t - 20130924
PDF
What is-java
PPT
CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs
PPTX
Java byte code presentation
PPTX
Java virtual machine
Java bytecode and classes
Java byte code & virtual machine
Introduction to the Java bytecode - So@t - 20130924
What is-java
CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs
Java byte code presentation
Java virtual machine

What's hot (20)

PDF
An Introduction to Java Compiler and Runtime
PPT
Java Virtual Machine
PPTX
Jvm Architecture
ODP
Java compilation
PPTX
Java architecture
PPTX
Architecture diagram of jvm
PPT
CS Lesson: Introduction to the Java virtual Machine
PDF
Java Virtual Machine - Internal Architecture
PDF
Java and Java platforms
PPT
Java-java virtual machine
PPTX
Java virtual machine
PPTX
Java introduction
PPT
Byte code jvm
PPSX
Java &amp; advanced java
DOCX
JDK,JRE,JVM
PPTX
Java history, versions, types of errors and exception, quiz
PPTX
Java features
An Introduction to Java Compiler and Runtime
Java Virtual Machine
Jvm Architecture
Java compilation
Java architecture
Architecture diagram of jvm
CS Lesson: Introduction to the Java virtual Machine
Java Virtual Machine - Internal Architecture
Java and Java platforms
Java-java virtual machine
Java virtual machine
Java introduction
Byte code jvm
Java &amp; advanced java
JDK,JRE,JVM
Java history, versions, types of errors and exception, quiz
Java features
Ad

Similar to JAVA BYTE CODE (20)

PPTX
Java ce241
PPT
Presentation on java
PPTX
A tour of Java and the JVM
PDF
FTD JVM Internals
PPTX
JAVA PROGRAM CONSTRUCTS OR LANGUAGE BASICS.pptx
PPTX
1 java programming- introduction
PPTX
PPTX
ppt_on_java.pptx
PPTX
Java Basic PART I
PDF
Code lifecycle in the jvm - TopConf Linz
PPTX
PDF
Swift language - A fast overview of some features
PPTX
White and Black Magic on the JVM
PPTX
Java programing language unit 1 introduction
PPTX
1 Module 1 Introduction.pptx
PPTX
Fundamentals of java --- version 2
PPTX
Java Programming and J2ME: The Basics
PDF
Java 8 selected updates
PPTX
Ahead-Of-Time Compilation of Java Applications
Java ce241
Presentation on java
A tour of Java and the JVM
FTD JVM Internals
JAVA PROGRAM CONSTRUCTS OR LANGUAGE BASICS.pptx
1 java programming- introduction
ppt_on_java.pptx
Java Basic PART I
Code lifecycle in the jvm - TopConf Linz
Swift language - A fast overview of some features
White and Black Magic on the JVM
Java programing language unit 1 introduction
1 Module 1 Introduction.pptx
Fundamentals of java --- version 2
Java Programming and J2ME: The Basics
Java 8 selected updates
Ahead-Of-Time Compilation of Java Applications
Ad

JAVA BYTE CODE

  • 1. JAVED AHMED SAMO Course Title Presented to: Presented by:
  • 3. • Bytecode is an intermediate code that runs on JVM • A Java virtual machine (JVM) is an abstract computing machine that enables a computer to run a Java program
  • 10. P-code (various forms of instruction sets designed for efficient execution by a software interpreter as well as being suitable for further compilation into machine code. / Java bytecode is the instruction set of the Java virtual machine. Each bytecode is composed by one, or in some cases two, bytes that represent the instruction (opcode), along with zero or more bytes for passing parameters. Advantages of Bytecode • Bytecode is architecture independent (and writing a VM is easier than rewriting a compiler for every architecture) • Just In-Time (JIT) compiling helps achieve same or better speed than traditional compiled code Introduction to Byte Code Other languages that have an intermediate representation • C#-----------------MSIL • Perl ---------------perlcompiler • PHP ---------------bcompiler • Python -------------
  • 11. • Jikes, compiles from Java to Java bytecode (developed by IBM, implemented in C++) • Espresso, compiles from Java to Java bytecode (Java 1.0 only) • GCJ, the GNU Compiler for Java, compiles from Java to Java bytecode; it is also able to compile to native machine code and is available as part of the GNU Compiler Collection (GCC). • Some projects provide Java assemblers to enable writing Java bytecode by hand. Assembly code may be also generated by machine, for example by a compiler targeting a Java virtual machine. Notable Java assemblers include: • Jasmin, takes textual descriptions for Java classes, written in a simple assembly-like syntax using Java Virtual Machine instruction set and generates a Java class file • Jamaica, a macro assembly language for the Java virtual machine. Java syntax is used for class or interface definition. Method bodies are specified using bytecode instructions. • Krakatau currently contains three tools: a decompiler and disassembler for Java classfiles and an assembler to create classfiles • Lilac, an assembler and disassembler for the Java virtual machine. Some projects provide Java assemblers to enable writing Java bytecode
  • 12. • JVM-Types and their prefixes ▫ Byte b ▫ Short s ▫ Integer i (java booleans are mapped to jvm ints!) ▫ Long l ▫ Character c ▫ Single float f ▫ double float d ▫ References a to Classes, Interfaces, Arrays • These Prefixes used in opcodes (iadd, astore,...) Bytecode Basics
  • 13. Instructions The JVM has 256 instructions for: •Arithmetic operations •Branch operations •Constant loading operations •Locals operations •Stack operations •Class operations •Method operations
  • 14. Of the 256 possible byte-long opcodes, as of 2015, 198 are in use (~77%), 54 are reserved for future use, and 3 instructions (~1%) are set aside as permanently unimplemented. Instructions fall into a number of broad groups: •Load and store (e.g. aload_0, istore) •Arithmetic and logic (e.g. ladd, fcmpl) •Type conversion (e.g. i2b, d2i) •Object creation and manipulation (new, putfield) •Operand stack management (e.g. swap, dup2) •Control transfer (e.g. ifeq, goto) •Method invocation and return (e.g. invokespecial, areturn) Instructions Conti… ▫ Byte b ▫ Short s ▫ Integer i ▫ Long l ▫ Character c ▫ Single float f ▫ double float d ▫ References a
  • 16. Java Bytecode Explanation Opcode Mnemonic Description 0 nop Does nothing 1 aconst_null Push null on the stack 3 iconst_0 Push int 0 on the stack 4 iconst_1 Push int 1 on the stack 18 ldc <value> Push a one-word (4 bytes) constant onto the stack ldc “Hello” ldc 201 Constant may be an int, float or String Instructions Conti…
  • 17. Other types of Instructions • Control Flow (~20 instructions) – if, goto, return • Method Calls (4 instructions) • Loading and Storing Variables (65 instructions) • Creating objects (1 instruction) • Using object fields (4 instructions) • Arrays (3 instructions)
  • 18. Javap examples public class Test1 { public int add(int a, int b) { int c= a+b; return c; } } javap -c Test1javac -g Test1.java Javap included with Java Development Kit (JDK) … public int add(int, int); Code: 0: iload_1 1: iload_2 2: iadd 3: istore_3 4: iload_3 5: ireturn // add var 1 and 2 // store as local var 3 // store onto stack // return int // push onto stack // push onto stack
  • 19. public class Test1 { public int add(int a, int b) { int c= a+b; return c; } } … public int add(int a, int b) { int c = a + b; // 0 0:iload_1 // 1 1:iload_2 // 2 2:iadd // 3 3:istore_3 return c; // 4 4:iload_3 // 5 5:ireturn } jad -a Test1javac -g Test1.java JAD is free, but not included with Java Development Kit (JDK) JAD examples