Post on 12-Sep-2021
Java and the JVM
Martin Schöberl
Java and the JVM 2
Overview
History and Java featuresJava technologyThe Java languageA first look into the JVMDisassembling of .class files
Java and the JVM 3
History of a Young Java
1992 Oak for a PDA on a SPARC (*7)1995 Official release as Java – Internet1997 picoJava – Sun’s Java processor1998 RTSJ specification start as JSR-011999 split into J2SE and J2EE2000 J2ME2002 RTSJ final release2002 first version of JOP ;-)
Java and the JVM 4
Java features
Simple and object orientedLook and feel of CSimplified object model with single inheritance
PortabilityJava compiler generates bytecodesRuntime systems for various platformsSize and behavior of basic data types definedWrite once, run/debug anywhere
Java and the JVM 5
Java features cont.
AvailabilityWindows, Linux, Solaris,…Embedded systemsCompiler and runtime are freeFree IDEs: Eclipse, Netbeans
LibraryRich class libraryPart of the definitionStandard GUI toolkit
Java and the JVM 6
Java features cont.
Built-in model for concurrencyThreads at the language levelSynchronizationLibraries are thread-safe
SafetyNo Pointer!Extensive compile-time checkingRuntime checkingAutomatic memory management – GC
Java and the JVM 7
Java system overview
Java and the JVM 8
Java Technology
The Java programming languageThe library (JDK)The Java virtual machine (JVM)
Instruction set Binary formatVerification
Java and the JVM 9
Java Primitive Data Typesboolean either true or falsechar 16-bit Unicode character (unsigned)byte 8-bit integer (signed)short 16-bit integer (signed)int 32-bit integer (signed)long 64-bit integer (signed)float 32-bit floating-point (IEEE 754-1985)double 64-bit floating-point (IEEE 754-1985)
Java and the JVM 10
Objects
Everything belongs to an object (or a class)No global variables
Namespace for objectsSingle inheritanceInterfacesAllocated on the heapShared among threadsNo free() – garbage collector
Java and the JVM 11
What is a Virtual Machine?
A virtual machine (VM) is an abstractcomputer architectureSoftware on top of a real hardwareCan run the same application on different machines where the VM is available
Java and the JVM 12
The Java Virtual Machine
An abstract computing machine that executes bytecode programs
An instruction set and the meaning of those instructions – the bytecodesA binary format – the class file formatAn algorithm to verify the class file
Java and the JVM 13
JVM cont.
Runtime environment for JavaImplementation NOT definedRuns Java .class filesHas to conform to Sun‘s specification
Java and the JVM 14
Implementations of the JVM
InterpreterSimple, compactSlow
Just-in-time compilationState-of-the-art for desktop/serverToo resource consuming in embedded systems
Batch compilationHardware implementation
Our topic!
Java and the JVM 15
JVM Data Typesreference Pointer to an object or arrayint 32-bit integer (signed)long 64-bit integer (signed)float 32-bit floating-point (IEEE 754-1985)double 64-bit floating-point (IEEE 754-1985)
No boolean, char, byte, and short typesStack contains only 32-bit and 64-bit dataConversion instructions
Java and the JVM 16
Memory Areas for the JVM
Method areaClass descriptionCodeConstant pool
HeapObjects and ArraysShared by all threadsGarbage collected
Java and the JVM 17
Memory Areas for the JVM
StackThread privateLogical stack that contains:
Invocation frameLocal variable areaOperand stack
Not necessary a single stackLocal variables and operand stack are accessed frequently
Java and the JVM 18
JVM Instruction Set
32 (64) bit stack machineVariable length instruction setSimple to very complex instructionsSymbolic referencesOnly relative branches
Java and the JVM 19
JVM Instruction Set
Load and storeArithmeticType conversionObject creation and manipulationOperand stack manipulationControl transferMethod invocation and return
Java and the JVM 20
Dissassembling Java
Compilejavac Hello.java
Run java Hello
Dissassemblejavap -c Hello
Java and the JVM 21
A Bytecode Example
public class X {
public static voidmain(String[] args) {
add(1, 2);}
public static intadd(int a, int b) {
return a+b;}
}
public static void main(java.lang.String[]);Code:0: iconst_11: iconst_2//Method add:(II)I2: invokestatic #2; 5: pop6: return
public static intadd(int,int);Code:0: iload_01: iload_12: iadd3: ireturn
Java and the JVM 22
Coding: Avoiding garbage
System.out.println("Result = "+i);
getstatic #3; // Field System.out:Ljava/io/PrintStream;new #4; // class StringBufferdupinvokespecial #5; // StringBuffer."<init>":()Vldc #6; // String Result = invokevirtual #7; // StringBuffer.append:(LString;)LStringBufferiload_1invokevirtual #8; // StringBuffer.append:(I)LStringBuffer;invokevirtual #9; // StringBuffer.toString:()LString;invokevirtual #10;// PrintStream.println:(LString;)V
Java and the JVM 23
Coding: Avoiding garbage
System.out.print("Result = ");System.out.println(i);
getstatic #3; //Field System.out:Ljava/io/PrintStream;ldc #4; //String Result = invokevirtual #5; //Method PrintStream.print:(LString;)Vgetstatic #3; //Field System.out:LPrintStream;iload_1invokevirtual #6; //Method PrintStream.println:(I)V
Java and the JVM 24
Java for Embedded Systems?
+ Simpler than C/C+++ Safer than C/C+++ Threads are part of the language- Interpreting JVM is slow- JIT needs a lot of memory- GC and real-time?
Java and the JVM 25
Summary Java/JVM
Java language definitionClass libraryThe Java virtual machine (JVM)
An instruction set – the bytecodesA binary format – the class fileAn algorithm to verify the class file
Java and the JVM 26
Summary Java Features
Safe OO LanguageNo pointersType-safetyGarbage Collection
Built in model for concurrencyPlatform independentVery rich standard library
Java and the JVM 27
More Information
JavaJames Gosling, Bill Joy, Guy Steele, and GiladBracha. The Java Language Specification, Addison-Wesley, 2000, JavaSpec.
JVMTim Lindholm and Frank Yellin. The Java Virtual Machine Specification. Addison-Wesley, 1999, JVMSpec.