SlideShare a Scribd company logo
JAVA BYTECODES
BY EXAMPLE
Ganesh Samarthyam
www.designsmells.com
Let’s jump into the rabbit hole
and explore a new world!
Java Bytecodes
By Example But this low level stuff
is scary - I don't want
to learn it
(1	-	(2	/	3))	+	((4	%	5)	*	6)
Draw the
expression tree
Java Bytecodes by Example
Perform post-order
traversal of the tree
1 2 3 / - 4 5 % 6 * +
post-order
traversal
result
Use a stack for
evaluating this
postfix expression
1 2 3 / - 4 5 % 6 * +
1 2 3 / - 4 5 % 6 * +
1 1
2
1
2
3
1
0
Initial
empty
push 1 push 2 push 3
pop 3
pop 2
push 2 / 3
1
pop 0
pop 1
push 1 - 0
1
push 4
4
1
push 5
4
5
1
pop 5
pop 4
push 4 % 5
4
1
push 6
4
6
1
pop 6
pop 4
push 6 * 4
24
25
pop 24
pop 1
push 24 + 1
Java Bytecodes by Example
1 2 3 / - 4 5 % 6 * +
Initial
empty
1 2 3 / - 4 5 % 6 * +
1
push 1
1 2 3 / - 4 5 % 6 * +
1
2
push 2
1 2 3 / - 4 5 % 6 * +
1
2
3
push 3
1 2 3 / - 4 5 % 6 * +
1
0
pop 3
pop 2
push 2 / 3
1 2 3 / - 4 5 % 6 * +
1
pop 0
pop 1
push 1 - 0
1 2 3 / - 4 5 % 6 * +
1
push 4
4
1 2 3 / - 4 5 % 6 * +
1
push 5
4
5
1 2 3 / - 4 5 % 6 * +
1
pop 5
pop 4
push 4 % 5
4
1 2 3 / - 4 5 % 6 * +
1
push 6
4
6
1 2 3 / - 4 5 % 6 * +
1
pop 6
pop 4
push 6 * 4
24
1 2 3 / - 4 5 % 6 * +
25
pop 24
pop 1
push 24 + 1
1 2 3 / - 4 5 % 6 * +
Let us give
names to these
operations
push 1
push 2
push 3
div
sub
push 4
push 5
mod
push 6
mul
add
int a = 1, b = 2, c = 3, d = 4, e = 5, f = 6;
int r = (a - (b / c)) + ((d % e) * f);
This is what a Java
compiler generates
iload_1
iload_2
iload_3
idiv
isub
iload 4
iload 5
irem
iload 6
imul
iadd
istore 7
push 1
push 2
push 3
div
sub
push 4
push 5
mod
push 6
mul
add
ourbytecode
Javabytecodes
(1	-	(2	/	3))	+	((4	%	5)	*	6)Source code
Java
Compiler
JavaBytecode
JVM
iload_1
iload_2
iload_3
idiv
isub
iload 4
iload 5
irem
iload 6
imul
iadd
istore 7
Java bytecodes supports object oriented programming
Typed intermediate language
Supports primitive types (int, float, double, …) and
reference types (arrays, strings, objects, …)
Instructions can be classified into various types such as:
loading (*load*)
storing (*store*)
method invocation
arithmetic operations
logical operations
control flow
memory allocation
exception handling
…
$ cat Expr.java
class Expr {
public static void main(String []args) {
int a = 1, b = 2, c = 3, d = 4, e = 5, f = 6;
int r = (a - (b / c)) + ((d % e) * f);
System.out.println("" + r);
}
}
$ javac Expr.java
$ java Expr
25
$ javap -c Expr.class
Compiled from "Expr.java"
class Expr {
Expr();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: iconst_1
1: istore_1
...
Java
compiler
JavaVM
Java
disassembler
Use java tool for
disassembling
System.out.println(“Hello World");
Java bytecodes
// disassembled code using javap tool
0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #3 // String Hello World
5: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
int i = 10;
if(i != 20)
i = i*20;
System.out.println(i);
javap -c
0: bipush 10
2: istore_1
3: iload_1
4: bipush 20
6: if_icmpeq 14
9: iload_1
10: bipush 20
12: imul
13: istore_1
14: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
17: iload_1
18: invokevirtual #3 // Method java/io/PrintStream.println:(I)V
21: return
p-code
ucode
java bytecode
uncoldalvik
bytecode
python
bytecodes
Other ILs
public static void
main(java.lang.String[]);
descriptor: ??
flags: ??, ??
Code:
stack=??, locals=??, args_size=??
Pop
Quiz
public static void main(String []args) {
int a = 1, b = 2, c = 3, d = 4, e = 5, f = 6;
int r = (a - (b / c)) + ((d % e) * f);
System.out.println("" + r);
}
public static void
main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=3, locals=8, args_size=1
Answer
1 2 3 / - 4 5 % 6 * +
1 1
2
1
2
3
1
0
Initial
empty
push 1 push 2 push 3
pop 3
pop 2
push 2 / 3
1
pop 0
pop 1
push 1 - 0
1
push 4
4
1
push 5
4
5
1
pop 5
pop 4
push 4 % 5
4
1
push 6
4
6
1
pop 6
pop 4
push 6 * 4
24
25
pop 24
pop 1
push 24 + 1
Answer:
max stack
value is 3
Supplier<String> s = () -> "hello world";
System.out.println(s.get());
Pop
Quiz
What bytecode
instruction would
s.get() generate?
invokedynamic
Answer
Pop
Quiz
0: iconst_0
1: istore_1
2: iconst_0
3: istore_2
4: iload_2
5: bipush 10
7: if_icmpge 20
10: iload_1
11: iload_2
12: iadd
13: istore_1
14: iinc 2, 1
17: goto 4
20: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
23: iload_1
24: invokevirtual #3 // Method java/io/PrintStream.println:(I)V
27: return
Decompile this
assembly code
Answer
public static void main(String []args) {
int sum = 0;
for(int i = 0; i < 10; i++) {
sum += i;
}
System.out.println(sum);
}
0: iconst_0
1: istore_1
2: iconst_0
3: istore_2
4: iload_2
5: bipush 10
7: if_icmpge 20
10: iload_1
11: iload_2
12: iadd
13: istore_1
14: iinc 2, 1
17: goto 4
20: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
23: iload_1
24: invokevirtual #3 // Method java/io/PrintStream.println:(I)V
27: return
PROJECT WORK
The best way to learn Java bytecodes is to implement a Java
disassembler on your own!
For implementation, read the documentation of Java
bytecodes (in the JVM specification) and use javap tool as
the reference implementation.
BOOKSTO READ
Free download here: https://docs.oracle.com/javase/specs/jvms/se8/jvms8.pdf
BOOKSTO READ
BOOKSTO READ
IMAGE CREDITS
• https://pixabay.com/static/uploads/photo/2015/12/28/15/58/ferrari-1111582_960_720.jpg
• http://i.dailymail.co.uk/i/pix/2014/08/29/article-0-0296355F000004B0-113_634x421.jpg
• http://blogs.shell.com/climatechange/wp-content/uploads/2015/01/Check-under-the-hood.jpg
• https://diaryofabusymumdotcom.files.wordpress.com/2015/01/1369952540_be029c8337.jpg
• http://trentarthur.ca/wp-content/uploads/2013/05/gatsby.jpg
• http://cdn.playbuzz.com/cdn/84b94651-08da-4191-9b45-069535cf523f/9c35f887-a6fc-4c8d-861a-f323078709e8.jpg
• http://pad2.whstatic.com/images/thumb/5/54/Draw-a-Simple-Tree-Step-2.jpg/aid594851-728px-Draw-a-Simple-Tree-Step-2.jpg
• http://www.seabreeze.com.au/Img/Photos/Windsurfing/5350271.jpg
• https://d.gr-assets.com/hostedimages/1380222758ra/461081.gif
• http://cdn.shopify.com/s/files/1/0021/6982/products/GW-7693274_large.jpg?v=1283553128
• http://www.fisher-price.com/en_IN/Images/RMA_RWD_rock_a_stack_tcm222-163387.jpg
• http://www.njfamily.com/NJ-Family/January-2011/Learn-How-to-Spot-a-Learning-Disability/Boy-learning-disability.jpg
• https://teens.drugabuse.gov/sites/default/files/styles/medium/public/NIDA-News-What-was-down-the-hole-Alice.jpg?itok=DH19L7F2
• http://archivedemo.cnx.org/resources/4df9b85136bb00ee04456b031aa0c344e54f282e/CNX_Psych_08_04_Knuckles.jpg
• http://archivedemo.cnx.org/resources/4df9b85136bb00ee04456b031aa0c344e54f282e/CNX_Psych_08_04_Knuckles.jpg
• http://www.urbanspaces.co.uk/image/error-message-error-us.jpg
• http://conservationmagazine.org/wordpress/wp-content/uploads/2013/05/dig-deeper.jpg
• http://4.bp.blogspot.com/-BAZm9rddEhQ/TWy441M-p1I/AAAAAAAAAQg/_SKF8PMkVHA/s1600/mr%2Bfixit.tif%2B%2528Converted
%2529--6.jpg
www.designsmells.com bit.ly/sgganesh @GSamarthyam

More Related Content

What's hot (20)

PPTX
PYTHON FOR BEGINNERS (BASICS OF PYTHON)
HemaArora2
 
PDF
RuleML2015: PSOA2Prolog: Object-Relational Rule Interoperation and Implementa...
RuleML
 
ODP
Introduction to Python - Training for Kids
Aimee Maree
 
PDF
Introduction to Recursion (Python)
Thai Pangsakulyanont
 
PDF
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
Eelco Visser
 
PDF
CS4200 2019 | Lecture 2 | syntax-definition
Eelco Visser
 
PDF
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
Eelco Visser
 
PDF
famous placement papers
Ramanujam Ramu
 
PDF
Programming languages
Eelco Visser
 
PDF
Declarative Semantics Definition - Term Rewriting
Guido Wachsmuth
 
PDF
Compiler Construction | Lecture 14 | Interpreters
Eelco Visser
 
PDF
Compiler Construction | Lecture 2 | Declarative Syntax Definition
Eelco Visser
 
PDF
Computer science sqp
Prasanth566435
 
PDF
C Programming Interview Questions
Gradeup
 
PPT
Java Puzzlers
Dmitry Buzdin
 
PDF
Preparation Data Structures 07 stacks
Andres Mendez-Vazquez
 
PDF
Declarative Type System Specification with Statix
Eelco Visser
 
PDF
Declare Your Language: Syntax Definition
Eelco Visser
 
PDF
Big picture of category theory in scala with deep dive into contravariant and...
Piotr Paradziński
 
PPT
python.ppt
shreyas_test_1234
 
PYTHON FOR BEGINNERS (BASICS OF PYTHON)
HemaArora2
 
RuleML2015: PSOA2Prolog: Object-Relational Rule Interoperation and Implementa...
RuleML
 
Introduction to Python - Training for Kids
Aimee Maree
 
Introduction to Recursion (Python)
Thai Pangsakulyanont
 
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
Eelco Visser
 
CS4200 2019 | Lecture 2 | syntax-definition
Eelco Visser
 
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
Eelco Visser
 
famous placement papers
Ramanujam Ramu
 
Programming languages
Eelco Visser
 
Declarative Semantics Definition - Term Rewriting
Guido Wachsmuth
 
Compiler Construction | Lecture 14 | Interpreters
Eelco Visser
 
Compiler Construction | Lecture 2 | Declarative Syntax Definition
Eelco Visser
 
Computer science sqp
Prasanth566435
 
C Programming Interview Questions
Gradeup
 
Java Puzzlers
Dmitry Buzdin
 
Preparation Data Structures 07 stacks
Andres Mendez-Vazquez
 
Declarative Type System Specification with Statix
Eelco Visser
 
Declare Your Language: Syntax Definition
Eelco Visser
 
Big picture of category theory in scala with deep dive into contravariant and...
Piotr Paradziński
 
python.ppt
shreyas_test_1234
 

Viewers also liked (10)

PPTX
CustomerGauge B2b Net Promoter Score Measurement
CustomerGauge
 
PDF
What Would Steve Do? 10 Lessons from the World's Most Captivating Presenters
HubSpot
 
PDF
Les menaces applicatives
Bee_Ware
 
PDF
ASFWS 2012 - Le développement d’applications sécurisées avec Android par Joha...
Cyber Security Alliance
 
PPTX
JVM bytecode - The secret language behind Java and Scala
Takipi
 
PPTX
Java byte code in practice
Rafael Winterhalter
 
PPTX
Net Promoter Score (NPS) - Measure Customer Satisfaction in 1 Question
CheckMarket
 
PPT
Présentation Identités Actives
Fing
 
PDF
StHack 2014 - Ninon Eyrolles Obfuscation 101
StHack
 
PPTX
Réduire la taille de son apk
Jacques GIRAUDEL
 
CustomerGauge B2b Net Promoter Score Measurement
CustomerGauge
 
What Would Steve Do? 10 Lessons from the World's Most Captivating Presenters
HubSpot
 
Les menaces applicatives
Bee_Ware
 
ASFWS 2012 - Le développement d’applications sécurisées avec Android par Joha...
Cyber Security Alliance
 
JVM bytecode - The secret language behind Java and Scala
Takipi
 
Java byte code in practice
Rafael Winterhalter
 
Net Promoter Score (NPS) - Measure Customer Satisfaction in 1 Question
CheckMarket
 
Présentation Identités Actives
Fing
 
StHack 2014 - Ninon Eyrolles Obfuscation 101
StHack
 
Réduire la taille de son apk
Jacques GIRAUDEL
 
Ad

Similar to Java Bytecodes by Example (20)

PDF
Advanced Debugging Using Java Bytecodes
Ganesh Samarthyam
 
PPTX
Java OOP Concepts 1st Slide
sunny khan
 
PDF
Introduction to the Java bytecode - So@t - 20130924
yohanbeschi
 
PDF
College Project - Java Disassembler - Description
Ganesh Samarthyam
 
PDF
Bt0074 oops with java
Techglyphs
 
PDF
Improving Java performance at JBCNConf 2015
Raimon Ràfols
 
PDF
Improving Android Performance at Mobiconf 2014
Raimon Ràfols
 
PDF
Proyect of english
Carlos Alcivar
 
PPTX
Java platform
Visithan
 
PPTX
Introduction to Java Programming
One97 Communications Limited
 
PPT
The bytecode gobbledygook
Raimon Ràfols
 
PPT
Java API, Exceptions and IO
Jussi Pohjolainen
 
PPTX
Java introduction
The icfai university jaipur
 
PPTX
Unit-1_GHD.pptxguguigihihihihihihoihihhi
40NehaPagariya
 
PPTX
Java Virtual Machine
sivanantham s
 
PDF
Jvm internals
Luiz Fernando Teston
 
PPT
Introduction to-programming
BG Java EE Course
 
PPTX
Java UNITbgbgbfdbv v bbfbf cvbgfbvc gf 1.pptx
hofon47654
 
PPTX
Java Android Programming for MAD lab Fundamentals
Abcd463572
 
PPT
Jug dynamic languages_in_jvm
Dmitry Buzdin
 
Advanced Debugging Using Java Bytecodes
Ganesh Samarthyam
 
Java OOP Concepts 1st Slide
sunny khan
 
Introduction to the Java bytecode - So@t - 20130924
yohanbeschi
 
College Project - Java Disassembler - Description
Ganesh Samarthyam
 
Bt0074 oops with java
Techglyphs
 
Improving Java performance at JBCNConf 2015
Raimon Ràfols
 
Improving Android Performance at Mobiconf 2014
Raimon Ràfols
 
Proyect of english
Carlos Alcivar
 
Java platform
Visithan
 
Introduction to Java Programming
One97 Communications Limited
 
The bytecode gobbledygook
Raimon Ràfols
 
Java API, Exceptions and IO
Jussi Pohjolainen
 
Java introduction
The icfai university jaipur
 
Unit-1_GHD.pptxguguigihihihihihihoihihhi
40NehaPagariya
 
Java Virtual Machine
sivanantham s
 
Jvm internals
Luiz Fernando Teston
 
Introduction to-programming
BG Java EE Course
 
Java UNITbgbgbfdbv v bbfbf cvbgfbvc gf 1.pptx
hofon47654
 
Java Android Programming for MAD lab Fundamentals
Abcd463572
 
Jug dynamic languages_in_jvm
Dmitry Buzdin
 
Ad

More from Ganesh Samarthyam (20)

PDF
Wonders of the Sea
Ganesh Samarthyam
 
PDF
Animals - for kids
Ganesh Samarthyam
 
PDF
Applying Refactoring Tools in Practice
Ganesh Samarthyam
 
PDF
CFP - 1st Workshop on “AI Meets Blockchain”
Ganesh Samarthyam
 
PDF
Great Coding Skills Aren't Enough
Ganesh Samarthyam
 
PDF
Coding Guidelines - Crafting Clean Code
Ganesh Samarthyam
 
PDF
Design Patterns - Compiler Case Study - Hands-on Examples
Ganesh Samarthyam
 
PDF
Bangalore Container Conference 2017 - Brief Presentation
Ganesh Samarthyam
 
PDF
Bangalore Container Conference 2017 - Poster
Ganesh Samarthyam
 
PDF
Software Design in Practice (with Java examples)
Ganesh Samarthyam
 
PDF
OO Design and Design Patterns in C++
Ganesh Samarthyam
 
PDF
Bangalore Container Conference 2017 - Sponsorship Deck
Ganesh Samarthyam
 
PDF
Let's Go: Introduction to Google's Go Programming Language
Ganesh Samarthyam
 
PPT
Google's Go Programming Language - Introduction
Ganesh Samarthyam
 
PDF
Java Generics - Quiz Questions
Ganesh Samarthyam
 
PDF
Java Generics - by Example
Ganesh Samarthyam
 
PDF
Software Architecture - Quiz Questions
Ganesh Samarthyam
 
PDF
Docker by Example - Quiz
Ganesh Samarthyam
 
PDF
Core Java: Best practices and bytecodes quiz
Ganesh Samarthyam
 
PDF
Java Class Design
Ganesh Samarthyam
 
Wonders of the Sea
Ganesh Samarthyam
 
Animals - for kids
Ganesh Samarthyam
 
Applying Refactoring Tools in Practice
Ganesh Samarthyam
 
CFP - 1st Workshop on “AI Meets Blockchain”
Ganesh Samarthyam
 
Great Coding Skills Aren't Enough
Ganesh Samarthyam
 
Coding Guidelines - Crafting Clean Code
Ganesh Samarthyam
 
Design Patterns - Compiler Case Study - Hands-on Examples
Ganesh Samarthyam
 
Bangalore Container Conference 2017 - Brief Presentation
Ganesh Samarthyam
 
Bangalore Container Conference 2017 - Poster
Ganesh Samarthyam
 
Software Design in Practice (with Java examples)
Ganesh Samarthyam
 
OO Design and Design Patterns in C++
Ganesh Samarthyam
 
Bangalore Container Conference 2017 - Sponsorship Deck
Ganesh Samarthyam
 
Let's Go: Introduction to Google's Go Programming Language
Ganesh Samarthyam
 
Google's Go Programming Language - Introduction
Ganesh Samarthyam
 
Java Generics - Quiz Questions
Ganesh Samarthyam
 
Java Generics - by Example
Ganesh Samarthyam
 
Software Architecture - Quiz Questions
Ganesh Samarthyam
 
Docker by Example - Quiz
Ganesh Samarthyam
 
Core Java: Best practices and bytecodes quiz
Ganesh Samarthyam
 
Java Class Design
Ganesh Samarthyam
 

Recently uploaded (20)

PDF
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
PPTX
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PDF
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
PDF
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
PPTX
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PDF
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PDF
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PDF
Online Queue Management System for Public Service Offices in Nepal [Focused i...
Rishab Acharya
 
PDF
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Online Queue Management System for Public Service Offices in Nepal [Focused i...
Rishab Acharya
 
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 

Java Bytecodes by Example

  • 1. JAVA BYTECODES BY EXAMPLE Ganesh Samarthyam www.designsmells.com
  • 2. Let’s jump into the rabbit hole and explore a new world!
  • 3. Java Bytecodes By Example But this low level stuff is scary - I don't want to learn it
  • 7. 1 2 3 / - 4 5 % 6 * + post-order traversal result
  • 8. Use a stack for evaluating this postfix expression 1 2 3 / - 4 5 % 6 * +
  • 9. 1 2 3 / - 4 5 % 6 * + 1 1 2 1 2 3 1 0 Initial empty push 1 push 2 push 3 pop 3 pop 2 push 2 / 3 1 pop 0 pop 1 push 1 - 0 1 push 4 4 1 push 5 4 5 1 pop 5 pop 4 push 4 % 5 4 1 push 6 4 6 1 pop 6 pop 4 push 6 * 4 24 25 pop 24 pop 1 push 24 + 1
  • 11. 1 2 3 / - 4 5 % 6 * + Initial empty
  • 12. 1 2 3 / - 4 5 % 6 * + 1 push 1
  • 13. 1 2 3 / - 4 5 % 6 * + 1 2 push 2
  • 14. 1 2 3 / - 4 5 % 6 * + 1 2 3 push 3
  • 15. 1 2 3 / - 4 5 % 6 * + 1 0 pop 3 pop 2 push 2 / 3
  • 16. 1 2 3 / - 4 5 % 6 * + 1 pop 0 pop 1 push 1 - 0
  • 17. 1 2 3 / - 4 5 % 6 * + 1 push 4 4
  • 18. 1 2 3 / - 4 5 % 6 * + 1 push 5 4 5
  • 19. 1 2 3 / - 4 5 % 6 * + 1 pop 5 pop 4 push 4 % 5 4
  • 20. 1 2 3 / - 4 5 % 6 * + 1 push 6 4 6
  • 21. 1 2 3 / - 4 5 % 6 * + 1 pop 6 pop 4 push 6 * 4 24
  • 22. 1 2 3 / - 4 5 % 6 * + 25 pop 24 pop 1 push 24 + 1
  • 23. 1 2 3 / - 4 5 % 6 * + Let us give names to these operations push 1 push 2 push 3 div sub push 4 push 5 mod push 6 mul add
  • 24. int a = 1, b = 2, c = 3, d = 4, e = 5, f = 6; int r = (a - (b / c)) + ((d % e) * f); This is what a Java compiler generates iload_1 iload_2 iload_3 idiv isub iload 4 iload 5 irem iload 6 imul iadd istore 7 push 1 push 2 push 3 div sub push 4 push 5 mod push 6 mul add ourbytecode Javabytecodes
  • 26. Java bytecodes supports object oriented programming Typed intermediate language Supports primitive types (int, float, double, …) and reference types (arrays, strings, objects, …) Instructions can be classified into various types such as: loading (*load*) storing (*store*) method invocation arithmetic operations logical operations control flow memory allocation exception handling …
  • 27. $ cat Expr.java class Expr { public static void main(String []args) { int a = 1, b = 2, c = 3, d = 4, e = 5, f = 6; int r = (a - (b / c)) + ((d % e) * f); System.out.println("" + r); } } $ javac Expr.java $ java Expr 25 $ javap -c Expr.class Compiled from "Expr.java" class Expr { Expr(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]); Code: 0: iconst_1 1: istore_1 ... Java compiler JavaVM Java disassembler Use java tool for disassembling
  • 28. System.out.println(“Hello World"); Java bytecodes // disassembled code using javap tool 0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #3 // String Hello World 5: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
  • 29. int i = 10; if(i != 20) i = i*20; System.out.println(i); javap -c 0: bipush 10 2: istore_1 3: iload_1 4: bipush 20 6: if_icmpeq 14 9: iload_1 10: bipush 20 12: imul 13: istore_1 14: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream; 17: iload_1 18: invokevirtual #3 // Method java/io/PrintStream.println:(I)V 21: return
  • 31. public static void main(java.lang.String[]); descriptor: ?? flags: ??, ?? Code: stack=??, locals=??, args_size=?? Pop Quiz public static void main(String []args) { int a = 1, b = 2, c = 3, d = 4, e = 5, f = 6; int r = (a - (b / c)) + ((d % e) * f); System.out.println("" + r); }
  • 32. public static void main(java.lang.String[]); descriptor: ([Ljava/lang/String;)V flags: ACC_PUBLIC, ACC_STATIC Code: stack=3, locals=8, args_size=1 Answer
  • 33. 1 2 3 / - 4 5 % 6 * + 1 1 2 1 2 3 1 0 Initial empty push 1 push 2 push 3 pop 3 pop 2 push 2 / 3 1 pop 0 pop 1 push 1 - 0 1 push 4 4 1 push 5 4 5 1 pop 5 pop 4 push 4 % 5 4 1 push 6 4 6 1 pop 6 pop 4 push 6 * 4 24 25 pop 24 pop 1 push 24 + 1 Answer: max stack value is 3
  • 34. Supplier<String> s = () -> "hello world"; System.out.println(s.get()); Pop Quiz What bytecode instruction would s.get() generate?
  • 36. Pop Quiz 0: iconst_0 1: istore_1 2: iconst_0 3: istore_2 4: iload_2 5: bipush 10 7: if_icmpge 20 10: iload_1 11: iload_2 12: iadd 13: istore_1 14: iinc 2, 1 17: goto 4 20: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream; 23: iload_1 24: invokevirtual #3 // Method java/io/PrintStream.println:(I)V 27: return Decompile this assembly code
  • 37. Answer public static void main(String []args) { int sum = 0; for(int i = 0; i < 10; i++) { sum += i; } System.out.println(sum); } 0: iconst_0 1: istore_1 2: iconst_0 3: istore_2 4: iload_2 5: bipush 10 7: if_icmpge 20 10: iload_1 11: iload_2 12: iadd 13: istore_1 14: iinc 2, 1 17: goto 4 20: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream; 23: iload_1 24: invokevirtual #3 // Method java/io/PrintStream.println:(I)V 27: return
  • 38. PROJECT WORK The best way to learn Java bytecodes is to implement a Java disassembler on your own! For implementation, read the documentation of Java bytecodes (in the JVM specification) and use javap tool as the reference implementation.
  • 39. BOOKSTO READ Free download here: https://docs.oracle.com/javase/specs/jvms/se8/jvms8.pdf
  • 42. IMAGE CREDITS • https://pixabay.com/static/uploads/photo/2015/12/28/15/58/ferrari-1111582_960_720.jpg • http://i.dailymail.co.uk/i/pix/2014/08/29/article-0-0296355F000004B0-113_634x421.jpg • http://blogs.shell.com/climatechange/wp-content/uploads/2015/01/Check-under-the-hood.jpg • https://diaryofabusymumdotcom.files.wordpress.com/2015/01/1369952540_be029c8337.jpg • http://trentarthur.ca/wp-content/uploads/2013/05/gatsby.jpg • http://cdn.playbuzz.com/cdn/84b94651-08da-4191-9b45-069535cf523f/9c35f887-a6fc-4c8d-861a-f323078709e8.jpg • http://pad2.whstatic.com/images/thumb/5/54/Draw-a-Simple-Tree-Step-2.jpg/aid594851-728px-Draw-a-Simple-Tree-Step-2.jpg • http://www.seabreeze.com.au/Img/Photos/Windsurfing/5350271.jpg • https://d.gr-assets.com/hostedimages/1380222758ra/461081.gif • http://cdn.shopify.com/s/files/1/0021/6982/products/GW-7693274_large.jpg?v=1283553128 • http://www.fisher-price.com/en_IN/Images/RMA_RWD_rock_a_stack_tcm222-163387.jpg • http://www.njfamily.com/NJ-Family/January-2011/Learn-How-to-Spot-a-Learning-Disability/Boy-learning-disability.jpg • https://teens.drugabuse.gov/sites/default/files/styles/medium/public/NIDA-News-What-was-down-the-hole-Alice.jpg?itok=DH19L7F2 • http://archivedemo.cnx.org/resources/4df9b85136bb00ee04456b031aa0c344e54f282e/CNX_Psych_08_04_Knuckles.jpg • http://archivedemo.cnx.org/resources/4df9b85136bb00ee04456b031aa0c344e54f282e/CNX_Psych_08_04_Knuckles.jpg • http://www.urbanspaces.co.uk/image/error-message-error-us.jpg • http://conservationmagazine.org/wordpress/wp-content/uploads/2013/05/dig-deeper.jpg • http://4.bp.blogspot.com/-BAZm9rddEhQ/TWy441M-p1I/AAAAAAAAAQg/_SKF8PMkVHA/s1600/mr%2Bfixit.tif%2B%2528Converted %2529--6.jpg