SlideShare a Scribd company logo
Java Virtual MachineJava Virtual Machine
ArchitectureArchitecture
and APIsand APIs
22 Oct 200722 Oct 2007
National University of SingaporeNational University of Singapore
School of ComputingSchool of Computing
OH KWANG SHINOH KWANG SHIN
The JVM Architecture
and APIs
22 Oct 2007 2
AgendaAgenda
• Big Picture of Java
• The Java Virtual Machine Architecture
– Data Types & Storage
– Java Instruction Set
– Exceptions and Errors
– Binary Classes
– The Java Native Interface
• Completing the Platform: APIs
– Java Platforms
– Java APIs: Serializability
The JVM Architecture
and APIs
22 Oct 2007 3
Big Picture of JavaBig Picture of Java
Java Source
( *.java )
Java Compiler
( javac )
Java Class
( *.class )
Java Platform
Java Virtual
Machine
Java APIs
Object.class
The JVM Architecture
and APIs
22 Oct 2007 4
JVM – Data TypesJVM – Data Types
• Primitive Data Types
– int, char, byte, short, float, double
– Implementation-dependent fashion
• Defined according to the values they can take, not
the number of bits of storage
• Integer in the range -231
to +231
-1
– 32-bit words + two’s complement
– Could use more storage bits
• References
– Can hold reference values
• Reference value points to an object stored in memory
The JVM Architecture
and APIs
22 Oct 2007 5
JVM – Data TypesJVM – Data Types
• Objects and Arrays
– Object
• Composed of primitive data types and
references that may point to other object
– Array
• Has a fixed number of elements
• Elements of an array
– Must all be of the same primitive type
– Must all be references which point to objects of
the same type
The JVM Architecture
and APIs
22 Oct 2007 6
JVM – Data StorageJVM – Data Storage
• Global Storage
– Main memory, where globally declared
variables reside
• Local Storage
– Temporary storage for variables that are local
to a method
• Operand Storage
– Holds variables while they are being operated
on by the functional instructions
The JVM Architecture
and APIs
22 Oct 2007 7
JVM – Data StorageJVM – Data Storage
• The Stack
– Local and operand storage
are allocated on the stack
– Not arrays and objects,
but only references and
individual array elements
on the stack
– As each method is called,
a stack frame is allocated
Locals
Operands
Arguments
Locals
Operands
Arguments
Locals
Operands
Java Stack Structure
The JVM Architecture
and APIs
22 Oct 2007 8
JVM – Data StorageJVM – Data Storage
• Memory Hierarchy
The JVM Architecture
and APIs
22 Oct 2007 9
JVM – Java Instruction SetJVM – Java Instruction Set
• Instruction Formats
(a) opcode
(b) opcode index
(c) opcode index1 index2
(d) opcode data
(e) opcode data1 data2
Typical Bytecode Instruction Formats
The JVM Architecture
and APIs
22 Oct 2007 10
JVM – Java Instruction SetJVM – Java Instruction Set
• Data-Movement Instructions
– Push constant values onto the stack
•iconst1: single-byte instruction that pushes
the integer constant 1 onto the stack
•bipush data, sipush data1 data2,
ldc index, ldc_w index1 index2, etc
– Manipulate the stack entries
•pop: pops the top element from the stack
and discards it
•swap: swaps the positions of the top two
stack elements
The JVM Architecture
and APIs
22 Oct 2007 11
JVM – Java Instruction SetJVM – Java Instruction Set
• Data-Movement Instructions
– Moves values : local storage ↔ operand stack
• iload_1: takes the integer from local storage slot 1
and pushes it onto the stack
• istore_1: moves data from the stack to local
storage in the current stack frame
– Deal with global memory data (objects, arrays)
• new: new instance of the object is created on the
heap and initialized. A reference to the object is
pushed onto the stack
• newarray: creates an array containing elements of a
specified primitive type
• getfield, putfield: accesses data held in objects
The JVM Architecture
and APIs
22 Oct 2007 12
JVM – Java Instruction SetJVM – Java Instruction Set
• Type Conversion
– Convert one type of data item on the
stack to another
•i2f: pops an integer from the stack,
converts it to a float, and pushes the float
back onto the stack
The JVM Architecture
and APIs
22 Oct 2007 13
JVM – Java Instruction SetJVM – Java Instruction Set
• Functional Instructions
– Take input operands, perform operations on
them, and produce a result
– Single byte: operands are always taken from the
stack and results are placed onto the stack
– Example
• iadd: pops two integers from the stack  adds them
 pushes the sum onto the stack
• iand: pops two integers from the stack  performs a
logical AND on them  pushes the result onto the
stack
• ishfl: pops two integers from the stack  shifts the
top element left by an amount specified by the second
element  pushes the result onto the stack
The JVM Architecture
and APIs
22 Oct 2007 14
JVM – Java Instruction SetJVM – Java Instruction Set
• Control Flow Instructions
– ifeq data1 data2
• pops an integer from the stack  compares it with zero
– True: PC relative branch to an offset found by
concatenating the two data bytes
– if_icmpeq data1 data2
• pops two integer values from the stack  compares the
first with the second
– True: PC relative branch to an offset found by
concatenating the two data bytes
– ifnull data1 data2
• pops a reference from the stack  check it for null
– True: PC relative branch to an offset found by
concatenating the two data bytes
The JVM Architecture
and APIs
22 Oct 2007 15
JVM – Java Instruction SetJVM – Java Instruction Set
• Example Program
– javap: The Java Class File Disassembler
class Rectangle {
protected int sides [];
………………
………………
public int perimeter () {
return 2*(sides[0] + sides[1]);
}
public int area () {
return (sides[0] * sides[1]);
}
}
Java Source Rectangle.java
public int perimeter();
Code:
0: iconst_2
1: aload_0
2: getfield #2; //Field sides:[I
5: iconst_0
6: iaload
7: aload_0
8: getfield #2; //Field sides:[I
11: iconst_1
12: iaload
13: iadd
14: imul
15: ireturn
Disassembled Code
The JVM Architecture
and APIs
22 Oct 2007 16
Rectangle Objectsides[0]
JVM – Java Instruction SetJVM – Java Instruction Set
• Example Program Scenario
public int perimeter () {
return 2*(sides[0] + sides[1]);
}
public int perimeter();
Code:
0: iconst_2
1: aload_0
2: getfield #2;
5: iconst_0
6: iaload
7: aload_0
8: getfield #2;
11: iconst_1
12: iaload
13: iadd
14: imul
15: ireturn
Constant 2
Operand Stack
Pushes a constant 2 onto the operand stack
Pushes local variable 0 onto the stack
(argument  reference to the rectangle object)
Pushes the reference to the sides array
sides Array
Pushes index number 0 onto the stack
Index Number 0
Load element 0 from the array sides
Pushes local variable 0 onto the stack
(argument  reference to the rectangle object)
Pushes the reference to the sides array
Pushes index number 1 onto the stack
Load element 1 from the array sides
Rectangle Object
sides Array
Index Number 1
sides[1]
Pushes addition of the top two stack elements
Pushes multiplication of top two stack elements
Returns with the integer result on top of stack
sides[0]+sides[1]
2*(
sides[0]+sides[1])
The JVM Architecture
and APIs
22 Oct 2007 17
JVM – Exceptions & ErrorsJVM – Exceptions & Errors
• All exceptions must be handled
somewhere
– No global way to turn them off
– If there is no handler, then calling method
takes over
– Overall program robustness consideration
• Errors
– Caused by limitation of the VM implementation
or VM bugs
• Exceptions
– Caused by program behavior that occurs
dynamically – as the program executes
The JVM Architecture
and APIs
22 Oct 2007 18
JVM – Exceptions & ErrorsJVM – Exceptions & Errors
• Exception table with each method
– Makes it possible to specify an exception
handler, depending on where an exception
occurs
From To Target Type
8 12 96 Arithmetic Exception
Exception Table
The JVM Architecture
and APIs
22 Oct 2007 19
JVM – Exceptions & ErrorsJVM – Exceptions & Errors
• Example of exception table
class Rectangle {
protected int sides [];
………………
public int perimeter () {
try {
return 2*(sides[0] + sides[1]);
} catch(ArithmeticException e) {
return -1;
}
}
public int area () {
return (sides[0] * sides[1]);
}
}
public int perimeter();
Code:
0: iconst_2
1: aload_0
2: getfield #2; //Field sides:[I
5: iconst_0
6: iaload
7: aload_0
8: getfield #2; //Field sides:[I
11: iconst_1
12: iaload
13: iadd
14: imul
15: ireturn
16: astore_1
17: iconst_m1
18: ireturn
Exception table:
from to target type
0 15 16 Class java/lang/ArithmeticException
The JVM Architecture
and APIs
22 Oct 2007 20
JVM – Binary ClassesJVM – Binary Classes
• Binary class
– Typically included in a class file
– Code + Metadata
• Metadata is a detailed specification of the
data structures and their relationships
– Can be loaded on demand
• At the time they are needed by the program
• Saves bandwidth for loading binary classes
that are never used
• Allows a Java program to start up quickly
The JVM Architecture
and APIs
22 Oct 2007 21
JVM – Binary Class FormatJVM – Binary Class Format
Magic Number
Version Information
Constant Pool Size
Constant Pool
Access Flags
This Class
Super Class
Interface Count
Interfaces
Field Count
Field Information
Method Count
Methods
Attribute Count
Attributes
• • • • • •
• • • • • •
• • • • • •
• • • • • •
• • • • • •
• • • • • •
• • • • • •
• • • • • •
• • • • • •
• • • • • •
• • • • • •
• • • • • •
• • • • • •
• • • • • •
• • • • • •
Identifier of java binary class
Minor and major version numbers of this class file
Number of entries in the constant pool + 1
All the constant values and references
Provide access information
Name of this class (Valid index of Constant Pool)
Name of super class (Valid index of Constant Pool or 0)
Number of direct superinterfaces
Number of references to the superinterfaces
(Valid index into the Constant Pool table)
Number of field_info structures in the Field Information
field_info structures giving a complete description of
a field in this class or interface
Number of method_info structures in the Methods
method_info structure giving a complete description of
a method in this class or interface
Number of attributes in the Attributes
Detailed information regarding
the other components listed earlier
The JVM Architecture
and APIs
22 Oct 2007 22
JVM – Java Native InterfaceJVM – Java Native Interface
• JNI (Java Native Interface)
– Allows java code and native compiled
code to interoperate
The JVM Architecture
and APIs
22 Oct 2007 23
Java PlatformJava Platform
• Java Platform = JVM + Java APIs
• Java APIs
– A set of standard libraries
– Provide most of the features that are
visible to users and software developers
• Support for secure network computing,
component-based software, graphical user
interfaces (GUIs), etc.
The JVM Architecture
and APIs
22 Oct 2007 24
Java PlatformJava Platform
• J2EE, J2SE and J2ME
The JVM Architecture
and APIs
22 Oct 2007 25
Java APIs - SerializationJava APIs - Serialization
• Process of converting an object into
an implementation-independent form
Object
Platform A-
Dependent
Representation
Serialization Deserialization
Object
Platform B-
Dependent
Representation
Platform-
Independent
Representation
Persistent Storage
Network
The JVM Architecture
and APIs
22 Oct 2007 26
Java APIs - ThreadJava APIs - Thread
• Multithreading support is provided by Java
libraries that are part of java.lang
• Monitors
– Support the synchronization among threads
– Lock and two Java bytecode instructions
• Lock
– Associated with each object and each class
– Operated as a counter, rather than flag
• Two Java bytecode instructions
– Monitorenter
– monitorexit
The JVM Architecture
and APIs
22 Oct 2007 27
210
Java APIs - ThreadJava APIs - Thread
public int perimeter () {
synchronized (sides) {
synchronized (sides) {
return 2*(sides[0] + sides[1]);
}
}
}
public int perimeter();
Code:
………
6: monitorenter
………
13: monitorenter
14: iconst_2
………
35: aload_2
36: monitorexit
………
41: aload_1
42: monitorexit
………
Lock of sides
Acquires the lock for the object, the lock is incremented
Acquiring thread may already hold the lock
, the lock is incremented
Decrements the lock for the object
Decrements the lock for the object
If the lock becomes zero,
then it is released and can
be acquired by a waiting
thread (if there is one)
The JVM Architecture
and APIs
22 Oct 2007 28
ReferencesReferences
• James E. Smith and Ravi Nair, Virtual Machines: Versatile
Platform for Systems and Processes. Morgan Kaufmann
Publishers, 2005.
• Java Technology – The Source for Java Developers
Available: http://java.sun.com
• Bill Venners, Inside the Java 2 Virtual Machine, McGraw-
Hill, 1999.
• Tim Lindholm and Frank Yellin, The JavaTM
Virtual
Machine Specification – Second Edition, Addison-Wesley
Longman Publishing Co., Inc., Boston, MA, 1999.
CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

More Related Content

What's hot (20)

PPTX
Java virtual machine
Nikhil Sharma
 
PDF
New Features Of JDK 7
Deniz Oguz
 
PPTX
Java history, versions, types of errors and exception, quiz
SAurabh PRajapati
 
PDF
What is-java
Shahid Rasheed
 
PPTX
Java architecture
Rakesh
 
PDF
An Introduction to Java Compiler and Runtime
Omar Bashir
 
PDF
Java and Java platforms
Ilio Catallo
 
PDF
Core Java
Prakash Dimmita
 
PPT
CS Lesson: Introduction to the Java virtual Machine
Katrin Becker
 
PPSX
Java & advanced java
BASAVARAJ HUNSHAL
 
PPT
Java basic tutorial by sanjeevini india
Sanjeev Tripathi
 
PDF
Core Java Tutorial
eMexo Technologies
 
PDF
Dynamic Proxy by Java
Kan-Han (John) Lu
 
PDF
Understanding Java Dynamic Proxies
Rafael Luque Leiva
 
PPTX
Java introduction
The icfai university jaipur
 
PPTX
Core java
Ravi varma
 
PPTX
Java features
Prashant Gajendra
 
PPTX
Java byte code presentation
Mahnoor Hashmi
 
PPSX
Introduction to java
Ajay Sharma
 
PPTX
java: basics, user input, data type, constructor
Shivam Singhal
 
Java virtual machine
Nikhil Sharma
 
New Features Of JDK 7
Deniz Oguz
 
Java history, versions, types of errors and exception, quiz
SAurabh PRajapati
 
What is-java
Shahid Rasheed
 
Java architecture
Rakesh
 
An Introduction to Java Compiler and Runtime
Omar Bashir
 
Java and Java platforms
Ilio Catallo
 
Core Java
Prakash Dimmita
 
CS Lesson: Introduction to the Java virtual Machine
Katrin Becker
 
Java & advanced java
BASAVARAJ HUNSHAL
 
Java basic tutorial by sanjeevini india
Sanjeev Tripathi
 
Core Java Tutorial
eMexo Technologies
 
Dynamic Proxy by Java
Kan-Han (John) Lu
 
Understanding Java Dynamic Proxies
Rafael Luque Leiva
 
Java introduction
The icfai university jaipur
 
Core java
Ravi varma
 
Java features
Prashant Gajendra
 
Java byte code presentation
Mahnoor Hashmi
 
Introduction to java
Ajay Sharma
 
java: basics, user input, data type, constructor
Shivam Singhal
 

Viewers also liked (20)

PPTX
Control Statements in Java
Niloy Saha
 
PDF
Java Magazine : The JAVA Virtual Machine alternative languages
Erik Gur
 
PPSX
Data types, Variables, Expressions & Arithmetic Operators in java
Javed Rashid
 
PPTX
Architecture diagram of jvm
home
 
PPTX
Java package
CS_GDRCST
 
PPTX
5.interface and packages
Deepak Sharma
 
PPT
Packages in java
Abhishek Khune
 
PPT
An Introduction to JVM Internals and Garbage Collection in Java
Abhishek Asthana
 
PDF
Java beans
Ravi Kant Sahu
 
PPT
Packages and interfaces
vanithaRamasamy
 
PPTX
Virtual instrumentation (LabVIEW)
Manipal University Jaipur
 
PDF
Sustainability
Esraa Mashaly
 
PPTX
العمارة الذكية و العمارة المستدامة
Ahmed SHoukry ELhfnawy
 
PPTX
العمارة الذكية وعلاقتها بلبيئة
Yaser Al-shahethi
 
ODP
Quick introduction to Java Garbage Collector (JVM GC)
Marcos García
 
PPT
Java interfaces
Raja Sekhar
 
PDF
Control structures in Java
Ravi_Kant_Sahu
 
PPTX
Operators in java
Then Murugeshwari
 
PDF
What is tackled in the Java EE Security API (Java EE 8)
Rudy De Busscher
 
Control Statements in Java
Niloy Saha
 
Java Magazine : The JAVA Virtual Machine alternative languages
Erik Gur
 
Data types, Variables, Expressions & Arithmetic Operators in java
Javed Rashid
 
Architecture diagram of jvm
home
 
Java package
CS_GDRCST
 
5.interface and packages
Deepak Sharma
 
Packages in java
Abhishek Khune
 
An Introduction to JVM Internals and Garbage Collection in Java
Abhishek Asthana
 
Java beans
Ravi Kant Sahu
 
Packages and interfaces
vanithaRamasamy
 
Virtual instrumentation (LabVIEW)
Manipal University Jaipur
 
Sustainability
Esraa Mashaly
 
العمارة الذكية و العمارة المستدامة
Ahmed SHoukry ELhfnawy
 
العمارة الذكية وعلاقتها بلبيئة
Yaser Al-shahethi
 
Quick introduction to Java Garbage Collector (JVM GC)
Marcos García
 
Java interfaces
Raja Sekhar
 
Control structures in Java
Ravi_Kant_Sahu
 
Operators in java
Then Murugeshwari
 
What is tackled in the Java EE Security API (Java EE 8)
Rudy De Busscher
 
Ad

Similar to CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs (20)

PPTX
Java Virtual Machine
sivanantham s
 
PPTX
Lecture 2 Java Virtual Machine .pptx
AnupamKumar559254
 
PPTX
Simple insites into JVM
Ramakanth Tarimala
 
PPTX
Java UNITbgbgbfdbv v bbfbf cvbgfbvc gf 1.pptx
hofon47654
 
PPT
Object Oriented Programming-JAVA
Home
 
PPTX
UNIT 1.pptx
EduclentMegasoftel
 
PPTX
Java Technologies notes of unit 1 and 2.
sumanyadavdpg
 
PDF
Jvm internals
Luiz Fernando Teston
 
PDF
PJ_M01_C01_PPT_Introduction to Object Oriented Programming Using Java.pdf
projectfora2
 
PPT
Java14
aiter2002
 
PDF
Introduction java programming
Nanthini Kempaiyan
 
PDF
Java quickref
Arduino Aficionado
 
PDF
Inside JVM
Chinh Ngo Nguyen
 
PPT
java introduction
Kunal Sunesara
 
PPTX
imperative programming language, java, android
i i
 
PPT
Java findamentals1
Todor Kolev
 
PPT
Java findamentals1
Todor Kolev
 
PPT
Java findamentals1
Todor Kolev
 
PPT
1- java
Krishna Sujeer
 
PPT
2.ppt
ssuser99ca78
 
Java Virtual Machine
sivanantham s
 
Lecture 2 Java Virtual Machine .pptx
AnupamKumar559254
 
Simple insites into JVM
Ramakanth Tarimala
 
Java UNITbgbgbfdbv v bbfbf cvbgfbvc gf 1.pptx
hofon47654
 
Object Oriented Programming-JAVA
Home
 
UNIT 1.pptx
EduclentMegasoftel
 
Java Technologies notes of unit 1 and 2.
sumanyadavdpg
 
Jvm internals
Luiz Fernando Teston
 
PJ_M01_C01_PPT_Introduction to Object Oriented Programming Using Java.pdf
projectfora2
 
Java14
aiter2002
 
Introduction java programming
Nanthini Kempaiyan
 
Java quickref
Arduino Aficionado
 
Inside JVM
Chinh Ngo Nguyen
 
java introduction
Kunal Sunesara
 
imperative programming language, java, android
i i
 
Java findamentals1
Todor Kolev
 
Java findamentals1
Todor Kolev
 
Java findamentals1
Todor Kolev
 
Ad

More from Kwangshin Oh (7)

PPTX
Ruby Programming Language - Introduction
Kwangshin Oh
 
PPTX
핀테크 코리아 2014 후기 - 오광신
Kwangshin Oh
 
PPT
CS6201 Software Reuse - Design Patterns
Kwangshin Oh
 
PPT
CS6270 Virtual Machines - Retargetable Binary Translators
Kwangshin Oh
 
PPT
CS5261 Group 8 Presentation - US Mobile Industry
Kwangshin Oh
 
PPT
Jini Network Technology
Kwangshin Oh
 
PPT
Object-Oriented Programming Concepts
Kwangshin Oh
 
Ruby Programming Language - Introduction
Kwangshin Oh
 
핀테크 코리아 2014 후기 - 오광신
Kwangshin Oh
 
CS6201 Software Reuse - Design Patterns
Kwangshin Oh
 
CS6270 Virtual Machines - Retargetable Binary Translators
Kwangshin Oh
 
CS5261 Group 8 Presentation - US Mobile Industry
Kwangshin Oh
 
Jini Network Technology
Kwangshin Oh
 
Object-Oriented Programming Concepts
Kwangshin Oh
 

Recently uploaded (20)

PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 

CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

  • 1. Java Virtual MachineJava Virtual Machine ArchitectureArchitecture and APIsand APIs 22 Oct 200722 Oct 2007 National University of SingaporeNational University of Singapore School of ComputingSchool of Computing OH KWANG SHINOH KWANG SHIN
  • 2. The JVM Architecture and APIs 22 Oct 2007 2 AgendaAgenda • Big Picture of Java • The Java Virtual Machine Architecture – Data Types & Storage – Java Instruction Set – Exceptions and Errors – Binary Classes – The Java Native Interface • Completing the Platform: APIs – Java Platforms – Java APIs: Serializability
  • 3. The JVM Architecture and APIs 22 Oct 2007 3 Big Picture of JavaBig Picture of Java Java Source ( *.java ) Java Compiler ( javac ) Java Class ( *.class ) Java Platform Java Virtual Machine Java APIs Object.class
  • 4. The JVM Architecture and APIs 22 Oct 2007 4 JVM – Data TypesJVM – Data Types • Primitive Data Types – int, char, byte, short, float, double – Implementation-dependent fashion • Defined according to the values they can take, not the number of bits of storage • Integer in the range -231 to +231 -1 – 32-bit words + two’s complement – Could use more storage bits • References – Can hold reference values • Reference value points to an object stored in memory
  • 5. The JVM Architecture and APIs 22 Oct 2007 5 JVM – Data TypesJVM – Data Types • Objects and Arrays – Object • Composed of primitive data types and references that may point to other object – Array • Has a fixed number of elements • Elements of an array – Must all be of the same primitive type – Must all be references which point to objects of the same type
  • 6. The JVM Architecture and APIs 22 Oct 2007 6 JVM – Data StorageJVM – Data Storage • Global Storage – Main memory, where globally declared variables reside • Local Storage – Temporary storage for variables that are local to a method • Operand Storage – Holds variables while they are being operated on by the functional instructions
  • 7. The JVM Architecture and APIs 22 Oct 2007 7 JVM – Data StorageJVM – Data Storage • The Stack – Local and operand storage are allocated on the stack – Not arrays and objects, but only references and individual array elements on the stack – As each method is called, a stack frame is allocated Locals Operands Arguments Locals Operands Arguments Locals Operands Java Stack Structure
  • 8. The JVM Architecture and APIs 22 Oct 2007 8 JVM – Data StorageJVM – Data Storage • Memory Hierarchy
  • 9. The JVM Architecture and APIs 22 Oct 2007 9 JVM – Java Instruction SetJVM – Java Instruction Set • Instruction Formats (a) opcode (b) opcode index (c) opcode index1 index2 (d) opcode data (e) opcode data1 data2 Typical Bytecode Instruction Formats
  • 10. The JVM Architecture and APIs 22 Oct 2007 10 JVM – Java Instruction SetJVM – Java Instruction Set • Data-Movement Instructions – Push constant values onto the stack •iconst1: single-byte instruction that pushes the integer constant 1 onto the stack •bipush data, sipush data1 data2, ldc index, ldc_w index1 index2, etc – Manipulate the stack entries •pop: pops the top element from the stack and discards it •swap: swaps the positions of the top two stack elements
  • 11. The JVM Architecture and APIs 22 Oct 2007 11 JVM – Java Instruction SetJVM – Java Instruction Set • Data-Movement Instructions – Moves values : local storage ↔ operand stack • iload_1: takes the integer from local storage slot 1 and pushes it onto the stack • istore_1: moves data from the stack to local storage in the current stack frame – Deal with global memory data (objects, arrays) • new: new instance of the object is created on the heap and initialized. A reference to the object is pushed onto the stack • newarray: creates an array containing elements of a specified primitive type • getfield, putfield: accesses data held in objects
  • 12. The JVM Architecture and APIs 22 Oct 2007 12 JVM – Java Instruction SetJVM – Java Instruction Set • Type Conversion – Convert one type of data item on the stack to another •i2f: pops an integer from the stack, converts it to a float, and pushes the float back onto the stack
  • 13. The JVM Architecture and APIs 22 Oct 2007 13 JVM – Java Instruction SetJVM – Java Instruction Set • Functional Instructions – Take input operands, perform operations on them, and produce a result – Single byte: operands are always taken from the stack and results are placed onto the stack – Example • iadd: pops two integers from the stack  adds them  pushes the sum onto the stack • iand: pops two integers from the stack  performs a logical AND on them  pushes the result onto the stack • ishfl: pops two integers from the stack  shifts the top element left by an amount specified by the second element  pushes the result onto the stack
  • 14. The JVM Architecture and APIs 22 Oct 2007 14 JVM – Java Instruction SetJVM – Java Instruction Set • Control Flow Instructions – ifeq data1 data2 • pops an integer from the stack  compares it with zero – True: PC relative branch to an offset found by concatenating the two data bytes – if_icmpeq data1 data2 • pops two integer values from the stack  compares the first with the second – True: PC relative branch to an offset found by concatenating the two data bytes – ifnull data1 data2 • pops a reference from the stack  check it for null – True: PC relative branch to an offset found by concatenating the two data bytes
  • 15. The JVM Architecture and APIs 22 Oct 2007 15 JVM – Java Instruction SetJVM – Java Instruction Set • Example Program – javap: The Java Class File Disassembler class Rectangle { protected int sides []; ……………… ……………… public int perimeter () { return 2*(sides[0] + sides[1]); } public int area () { return (sides[0] * sides[1]); } } Java Source Rectangle.java public int perimeter(); Code: 0: iconst_2 1: aload_0 2: getfield #2; //Field sides:[I 5: iconst_0 6: iaload 7: aload_0 8: getfield #2; //Field sides:[I 11: iconst_1 12: iaload 13: iadd 14: imul 15: ireturn Disassembled Code
  • 16. The JVM Architecture and APIs 22 Oct 2007 16 Rectangle Objectsides[0] JVM – Java Instruction SetJVM – Java Instruction Set • Example Program Scenario public int perimeter () { return 2*(sides[0] + sides[1]); } public int perimeter(); Code: 0: iconst_2 1: aload_0 2: getfield #2; 5: iconst_0 6: iaload 7: aload_0 8: getfield #2; 11: iconst_1 12: iaload 13: iadd 14: imul 15: ireturn Constant 2 Operand Stack Pushes a constant 2 onto the operand stack Pushes local variable 0 onto the stack (argument  reference to the rectangle object) Pushes the reference to the sides array sides Array Pushes index number 0 onto the stack Index Number 0 Load element 0 from the array sides Pushes local variable 0 onto the stack (argument  reference to the rectangle object) Pushes the reference to the sides array Pushes index number 1 onto the stack Load element 1 from the array sides Rectangle Object sides Array Index Number 1 sides[1] Pushes addition of the top two stack elements Pushes multiplication of top two stack elements Returns with the integer result on top of stack sides[0]+sides[1] 2*( sides[0]+sides[1])
  • 17. The JVM Architecture and APIs 22 Oct 2007 17 JVM – Exceptions & ErrorsJVM – Exceptions & Errors • All exceptions must be handled somewhere – No global way to turn them off – If there is no handler, then calling method takes over – Overall program robustness consideration • Errors – Caused by limitation of the VM implementation or VM bugs • Exceptions – Caused by program behavior that occurs dynamically – as the program executes
  • 18. The JVM Architecture and APIs 22 Oct 2007 18 JVM – Exceptions & ErrorsJVM – Exceptions & Errors • Exception table with each method – Makes it possible to specify an exception handler, depending on where an exception occurs From To Target Type 8 12 96 Arithmetic Exception Exception Table
  • 19. The JVM Architecture and APIs 22 Oct 2007 19 JVM – Exceptions & ErrorsJVM – Exceptions & Errors • Example of exception table class Rectangle { protected int sides []; ……………… public int perimeter () { try { return 2*(sides[0] + sides[1]); } catch(ArithmeticException e) { return -1; } } public int area () { return (sides[0] * sides[1]); } } public int perimeter(); Code: 0: iconst_2 1: aload_0 2: getfield #2; //Field sides:[I 5: iconst_0 6: iaload 7: aload_0 8: getfield #2; //Field sides:[I 11: iconst_1 12: iaload 13: iadd 14: imul 15: ireturn 16: astore_1 17: iconst_m1 18: ireturn Exception table: from to target type 0 15 16 Class java/lang/ArithmeticException
  • 20. The JVM Architecture and APIs 22 Oct 2007 20 JVM – Binary ClassesJVM – Binary Classes • Binary class – Typically included in a class file – Code + Metadata • Metadata is a detailed specification of the data structures and their relationships – Can be loaded on demand • At the time they are needed by the program • Saves bandwidth for loading binary classes that are never used • Allows a Java program to start up quickly
  • 21. The JVM Architecture and APIs 22 Oct 2007 21 JVM – Binary Class FormatJVM – Binary Class Format Magic Number Version Information Constant Pool Size Constant Pool Access Flags This Class Super Class Interface Count Interfaces Field Count Field Information Method Count Methods Attribute Count Attributes • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • Identifier of java binary class Minor and major version numbers of this class file Number of entries in the constant pool + 1 All the constant values and references Provide access information Name of this class (Valid index of Constant Pool) Name of super class (Valid index of Constant Pool or 0) Number of direct superinterfaces Number of references to the superinterfaces (Valid index into the Constant Pool table) Number of field_info structures in the Field Information field_info structures giving a complete description of a field in this class or interface Number of method_info structures in the Methods method_info structure giving a complete description of a method in this class or interface Number of attributes in the Attributes Detailed information regarding the other components listed earlier
  • 22. The JVM Architecture and APIs 22 Oct 2007 22 JVM – Java Native InterfaceJVM – Java Native Interface • JNI (Java Native Interface) – Allows java code and native compiled code to interoperate
  • 23. The JVM Architecture and APIs 22 Oct 2007 23 Java PlatformJava Platform • Java Platform = JVM + Java APIs • Java APIs – A set of standard libraries – Provide most of the features that are visible to users and software developers • Support for secure network computing, component-based software, graphical user interfaces (GUIs), etc.
  • 24. The JVM Architecture and APIs 22 Oct 2007 24 Java PlatformJava Platform • J2EE, J2SE and J2ME
  • 25. The JVM Architecture and APIs 22 Oct 2007 25 Java APIs - SerializationJava APIs - Serialization • Process of converting an object into an implementation-independent form Object Platform A- Dependent Representation Serialization Deserialization Object Platform B- Dependent Representation Platform- Independent Representation Persistent Storage Network
  • 26. The JVM Architecture and APIs 22 Oct 2007 26 Java APIs - ThreadJava APIs - Thread • Multithreading support is provided by Java libraries that are part of java.lang • Monitors – Support the synchronization among threads – Lock and two Java bytecode instructions • Lock – Associated with each object and each class – Operated as a counter, rather than flag • Two Java bytecode instructions – Monitorenter – monitorexit
  • 27. The JVM Architecture and APIs 22 Oct 2007 27 210 Java APIs - ThreadJava APIs - Thread public int perimeter () { synchronized (sides) { synchronized (sides) { return 2*(sides[0] + sides[1]); } } } public int perimeter(); Code: ……… 6: monitorenter ……… 13: monitorenter 14: iconst_2 ……… 35: aload_2 36: monitorexit ……… 41: aload_1 42: monitorexit ……… Lock of sides Acquires the lock for the object, the lock is incremented Acquiring thread may already hold the lock , the lock is incremented Decrements the lock for the object Decrements the lock for the object If the lock becomes zero, then it is released and can be acquired by a waiting thread (if there is one)
  • 28. The JVM Architecture and APIs 22 Oct 2007 28 ReferencesReferences • James E. Smith and Ravi Nair, Virtual Machines: Versatile Platform for Systems and Processes. Morgan Kaufmann Publishers, 2005. • Java Technology – The Source for Java Developers Available: http://java.sun.com • Bill Venners, Inside the Java 2 Virtual Machine, McGraw- Hill, 1999. • Tim Lindholm and Frank Yellin, The JavaTM Virtual Machine Specification – Second Edition, Addison-Wesley Longman Publishing Co., Inc., Boston, MA, 1999.