Title | JVM Programming Languages |
---|---|
Course | Advanced Programming |
Institution | University of Greenwich |
Pages | 4 |
File Size | 87.5 KB |
File Type | |
Total Downloads | 92 |
Total Views | 148 |
Download JVM Programming Languages PDF
JVM Programming Languages WORA Write Once Run Anywhere o This Java slogan explains in part why java has been so successful o You write Java code once and then run it on almost any device Different platform Different operating systems What makes WORA possible? o Source code is compiled into bytecode o Bytecode is executed by the JVM o Multiple JVM implementations for a variety of operating systems and platforms
JVM JVM stands for Java Virtual Machine What is a Virtual Machine? o runs as an application on the host Operating System o Provides an abstraction of the underlying hardware and operating system This makes it possible to execute code in the same way on any supporting platform o Uses interpretation or JIT (Just-in-Time) compilation to translate the bytecode to machine code Class Loader Loads the class files into memory Loads superclasses and interfaces for each class Links classes o Verifies and prepares the loaded classes Initialises class variables to their starting values JVM Memory Method Area o Contains class level data (e.g. static variables) o Shared across all threads Heap o Contains all objects and their instance variables o Shared across all threads
Stack o Contains local method variables o Thread safe – each thread has its own Registers o Contain the addresses of currently executing instructions o Thread safe – each thread has its own Native Method Stack o Holds native method information Native methods are those specific to the hardware / operating system (e.g. C code) o Thread safe – each thread has its own Stack and Heap Stack o Stores thread-specific and function-related information Local variables, parameters, return types, etc. o Keeps track of what is executing in our code we call a method, all necessary information is added to the stack – when done executing, the information is removed from the stack and control is returned to the calling method, whose information will be on top of the stack now o Content is removed when it loses scope Heap o Stores objects o Keeps information, but doesn’t keep track of execution o Content removed when garbage-collected Stack are executing some code: o call MethodA() o MethodA() calls MethodB() o MethodB() calls MethodC() o MethodC() returns o MethodB() returns o MethodA() returns
Memory Errors There are different types of memory errors which can occur in the JVM o Stack Overflow
The stack runs out of memory Too many methods called in sequence to fit on the stack (or using very large stack variables) o Out of Memory Error The heap runs out of memory Too many objects and associated instance variables are created and referenced Execution Engine Interpreter o Interprets bytecode into machine code at runtime o Faster interpretation o Slower execution o Every time a method is called, it requires a new interpretation JIT (Just-in-Time) Compiler o Compiles bytecode to native code at runtime o Initial overhead of compilation o Faster execution o Better performance when methods are called repeatedly
Garbage Collector The Garbage Collector goes around the Heap and removes unreferenced objects o Why are unreferenced objects deleted? They can not be reached without a reference Continuously runs in the background Can be triggered using System.gc() o This doesn’t guarantee execution, but can be seen as a suggestion JNI and Native Method Libraries Native Method Libraries o These are the collections of native code that the execution engine needs to access JNI (Java Native Interface) o This is the interface that enables the interaction between the JVM’s Execution Engine and native applications (e.g. C code) Class Files
Class files are compiled from source code and contain the bytecode There are 10 sections: 1. Magic number – identifies it as a Java class file 2. Version number – major and minor version of Java 3. Constant pool – list of constants used in the class (e.g. numbers, strings, class/method references, etc.) 4. Access flags – class modifiers (e.g. abstract) 5. This class – name of current class...