SlideShare ist ein Scribd-Unternehmen logo
1 von 29
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

Weitere ähnliche Inhalte

Was ist angesagt?

Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaJava basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini india
Sanjeev Tripathi
 

Was ist angesagt? (20)

Java virtual machine
Java virtual machineJava virtual machine
Java virtual machine
 
New Features Of JDK 7
New Features Of JDK 7New Features Of JDK 7
New Features Of JDK 7
 
Java history, versions, types of errors and exception, quiz
Java history, versions, types of errors and exception, quiz Java history, versions, types of errors and exception, quiz
Java history, versions, types of errors and exception, quiz
 
What is-java
What is-javaWhat is-java
What is-java
 
Java architecture
Java architectureJava architecture
Java architecture
 
An Introduction to Java Compiler and Runtime
An Introduction to Java Compiler and RuntimeAn Introduction to Java Compiler and Runtime
An Introduction to Java Compiler and Runtime
 
Java and Java platforms
Java and Java platformsJava and Java platforms
Java and Java platforms
 
Core Java
Core JavaCore Java
Core Java
 
CS Lesson: Introduction to the Java virtual Machine
CS Lesson: Introduction to the Java virtual MachineCS Lesson: Introduction to the Java virtual Machine
CS Lesson: Introduction to the Java virtual Machine
 
Java & advanced java
Java & advanced javaJava & advanced java
Java & advanced java
 
Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaJava basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini india
 
Core Java Tutorial
Core Java TutorialCore Java Tutorial
Core Java Tutorial
 
Dynamic Proxy by Java
Dynamic Proxy by JavaDynamic Proxy by Java
Dynamic Proxy by Java
 
Understanding Java Dynamic Proxies
Understanding Java Dynamic ProxiesUnderstanding Java Dynamic Proxies
Understanding Java Dynamic Proxies
 
Java introduction
Java introductionJava introduction
Java introduction
 
Core java
Core java Core java
Core java
 
Java features
Java featuresJava features
Java features
 
Java byte code presentation
Java byte code presentationJava byte code presentation
Java byte code presentation
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
java: basics, user input, data type, constructor
java:  basics, user input, data type, constructorjava:  basics, user input, data type, constructor
java: basics, user input, data type, constructor
 

Andere mochten auch

5.interface and packages
5.interface and packages5.interface and packages
5.interface and packages
Deepak Sharma
 
Quick introduction to Java Garbage Collector (JVM GC)
Quick introduction to Java Garbage Collector (JVM GC)Quick introduction to Java Garbage Collector (JVM GC)
Quick introduction to Java Garbage Collector (JVM GC)
Marcos García
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in Java
Ravi_Kant_Sahu
 

Andere mochten auch (20)

Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 
Java Magazine : The JAVA Virtual Machine alternative languages
Java Magazine : The JAVA Virtual Machine alternative languagesJava Magazine : The JAVA Virtual Machine alternative languages
Java Magazine : The JAVA Virtual Machine alternative languages
 
Data types, Variables, Expressions & Arithmetic Operators in java
Data types, Variables, Expressions & Arithmetic Operators in javaData types, Variables, Expressions & Arithmetic Operators in java
Data types, Variables, Expressions & Arithmetic Operators in java
 
Architecture diagram of jvm
Architecture diagram of jvmArchitecture diagram of jvm
Architecture diagram of jvm
 
Java package
Java packageJava package
Java package
 
5.interface and packages
5.interface and packages5.interface and packages
5.interface and packages
 
Java virtual machine
Java virtual machineJava virtual machine
Java virtual machine
 
Packages in java
Packages in javaPackages in java
Packages in java
 
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in JavaAn Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
 
Java beans
Java beansJava beans
Java beans
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 
Virtual instrumentation (LabVIEW)
Virtual instrumentation (LabVIEW)Virtual instrumentation (LabVIEW)
Virtual instrumentation (LabVIEW)
 
Sustainability
SustainabilitySustainability
Sustainability
 
العمارة الذكية و العمارة المستدامة
العمارة الذكية و العمارة المستدامةالعمارة الذكية و العمارة المستدامة
العمارة الذكية و العمارة المستدامة
 
العمارة الذكية وعلاقتها بلبيئة
العمارة الذكية وعلاقتها بلبيئةالعمارة الذكية وعلاقتها بلبيئة
العمارة الذكية وعلاقتها بلبيئة
 
Quick introduction to Java Garbage Collector (JVM GC)
Quick introduction to Java Garbage Collector (JVM GC)Quick introduction to Java Garbage Collector (JVM GC)
Quick introduction to Java Garbage Collector (JVM GC)
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in Java
 
Operators in java
Operators in javaOperators in java
Operators in java
 
What is tackled in the Java EE Security API (Java EE 8)
What is tackled in the Java EE Security API (Java EE 8)What is tackled in the Java EE Security API (Java EE 8)
What is tackled in the Java EE Security API (Java EE 8)
 

Ähnlich wie CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs

Perf onjs final
Perf onjs finalPerf onjs final
Perf onjs final
qi yang
 
Станислав Сидоренко «DeviceHive Java Server – миграция на Spring Boot»
Станислав Сидоренко «DeviceHive Java Server – миграция на Spring Boot»Станислав Сидоренко «DeviceHive Java Server – миграция на Spring Boot»
Станислав Сидоренко «DeviceHive Java Server – миграция на Spring Boot»
DataArt
 
Terence Barr - jdk7+8 - 24mai2011
Terence Barr - jdk7+8 - 24mai2011Terence Barr - jdk7+8 - 24mai2011
Terence Barr - jdk7+8 - 24mai2011
Agora Group
 

Ähnlich wie CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs (20)

Java user group 2015 02-09-java8
Java user group 2015 02-09-java8Java user group 2015 02-09-java8
Java user group 2015 02-09-java8
 
Java user group 2015 02-09-java8
Java user group 2015 02-09-java8Java user group 2015 02-09-java8
Java user group 2015 02-09-java8
 
Grow and Shrink - Dynamically Extending the Ruby VM Stack
Grow and Shrink - Dynamically Extending the Ruby VM StackGrow and Shrink - Dynamically Extending the Ruby VM Stack
Grow and Shrink - Dynamically Extending the Ruby VM Stack
 
Perf onjs final
Perf onjs finalPerf onjs final
Perf onjs final
 
Javaforum 20110915
Javaforum 20110915Javaforum 20110915
Javaforum 20110915
 
AMIS Oracle OpenWorld 2013 Review Part 3 - Fusion Middleware
AMIS Oracle OpenWorld 2013 Review Part 3 - Fusion MiddlewareAMIS Oracle OpenWorld 2013 Review Part 3 - Fusion Middleware
AMIS Oracle OpenWorld 2013 Review Part 3 - Fusion Middleware
 
Java 7 New Features
Java 7 New FeaturesJava 7 New Features
Java 7 New Features
 
GOTO Night with Charles Nutter Slides
GOTO Night with Charles Nutter SlidesGOTO Night with Charles Nutter Slides
GOTO Night with Charles Nutter Slides
 
Станислав Сидоренко «DeviceHive Java Server – миграция на Spring Boot»
Станислав Сидоренко «DeviceHive Java Server – миграция на Spring Boot»Станислав Сидоренко «DeviceHive Java Server – миграция на Spring Boot»
Станислав Сидоренко «DeviceHive Java Server – миграция на Spring Boot»
 
Reactive java programming for the impatient
Reactive java programming for the impatientReactive java programming for the impatient
Reactive java programming for the impatient
 
Java 7 Whats New(), Whats Next() from Oredev
Java 7 Whats New(), Whats Next() from OredevJava 7 Whats New(), Whats Next() from Oredev
Java 7 Whats New(), Whats Next() from Oredev
 
Java Future S Ritter
Java Future S RitterJava Future S Ritter
Java Future S Ritter
 
Hadoop cluster performance profiler
Hadoop cluster performance profilerHadoop cluster performance profiler
Hadoop cluster performance profiler
 
Terence Barr - jdk7+8 - 24mai2011
Terence Barr - jdk7+8 - 24mai2011Terence Barr - jdk7+8 - 24mai2011
Terence Barr - jdk7+8 - 24mai2011
 
Module 1.pptx
Module 1.pptxModule 1.pptx
Module 1.pptx
 
What’s expected in Java 9
What’s expected in Java 9What’s expected in Java 9
What’s expected in Java 9
 
Jdbc presentation
Jdbc presentationJdbc presentation
Jdbc presentation
 
Wt unit 3 server side technology
Wt unit 3 server side technologyWt unit 3 server side technology
Wt unit 3 server side technology
 
Wt unit 3 server side technology
Wt unit 3 server side technologyWt unit 3 server side technology
Wt unit 3 server side technology
 
java slides
java slidesjava slides
java slides
 

Mehr von Kwangshin Oh (7)

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

Kürzlich hochgeladen

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Kürzlich hochgeladen (20)

Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 

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.