30
Jun 16, 2022 1 Java Virtual Machine © Katrin Becker All rights reserved. Java Virtual Machine

CS Lesson: Introduction to the Java virtual Machine

Embed Size (px)

DESCRIPTION

An introduction to the Java Virtual Machine

Citation preview

Page 1: CS Lesson: Introduction to the Java virtual Machine

Apr 11, 2023 1Java Virtual Machine© Katrin BeckerAll rights reserved.

Java Virtual Machine

Page 2: CS Lesson: Introduction to the Java virtual Machine

Apr 11, 2023 2Java Virtual Machine© Katrin BeckerAll rights reserved.

Java Virtual Machine - start

JVM on Windows PCJVM on Windows PC

Java ‘Compiler’Java ‘Compiler’

ByteCodeByteCode

Java Source ProgramJava Source Program

JVM on iMACJVM on iMAC

JVM on UNIXJVM on UNIX

JVM on LINUX PCJVM on LINUX PC

ProgramRunningProgramRunning

ProgramRunningProgramRunning

ProgramRunningProgramRunning

ProgramRunningProgramRunning

Page 3: CS Lesson: Introduction to the Java virtual Machine

Apr 11, 2023 3Java Virtual Machine© Katrin BeckerAll rights reserved.

Java Virtual Machine - source

Java Source ProgramJava Source Program -Written in standard Java-Target machine irrelevant-Doesn’t matter which machine

it’s created on.

Page 4: CS Lesson: Introduction to the Java virtual Machine

Apr 11, 2023 4Java Virtual Machine© Katrin BeckerAll rights reserved.

Java Virtual Machine - compile

Java ‘Compiler’Java ‘Compiler’

Java Source ProgramJava Source Program

>> javac MyProg.java

Java ‘compiler’ does syntactic analysisand parsing.

Translates source into Byte CodeMyProg.class

Page 5: CS Lesson: Introduction to the Java virtual Machine

Apr 11, 2023 5Java Virtual Machine© Katrin BeckerAll rights reserved.

Java Virtual Machine - Byte Code

Java ‘Compiler’Java ‘Compiler’

ByteCodeByteCode

Java Source ProgramJava Source Program

Bytecode is the same for ALL machines and ALL versions of Java

Up to this point everything looks and behaves identicallyon ALL machines

Bytecode is assembler-like, but thereis no really real machine that ‘understands’ bytecode.

Called Bytecode because most instructions are one or twobytes long

Page 6: CS Lesson: Introduction to the Java virtual Machine

Apr 11, 2023 6Java Virtual Machine© Katrin BeckerAll rights reserved.

Java Virtual Machine – ready-set-..

Java ‘Compiler’Java ‘Compiler’

ByteCodeByteCode

Java Source ProgramJava Source Program

•Bytecode can be transported to any machine with a bytecode interpreter [called the “Java Virtual Machine”]•Assemblers run on real machines, so…..•Java Bytecode runs on a “Virtual Machine”

Page 7: CS Lesson: Introduction to the Java virtual Machine

Apr 11, 2023 7Java Virtual Machine© Katrin BeckerAll rights reserved.

Java Virtual Machine – go

Java ‘Compiler’Java ‘Compiler’

ByteCodeByteCode

Java Source ProgramJava Source Program

•The Virtual Machine is like a simulator – it simulates a real machine, but…•It also ‘tells’ the real machine on which it is currently running to do the stuff the virtual machine would be doing•This requires it to be ‘tailor-made’ for the machine it is running on…

Page 8: CS Lesson: Introduction to the Java virtual Machine

Apr 11, 2023 8Java Virtual Machine© Katrin BeckerAll rights reserved.

Java Virtual Machine - Linux

Java ‘Compiler’Java ‘Compiler’

ByteCodeByteCode

Java Source ProgramJava Source Program

JVM on LINUX PCJVM on LINUX PC

Page 9: CS Lesson: Introduction to the Java virtual Machine

Apr 11, 2023 9Java Virtual Machine© Katrin BeckerAll rights reserved.

Java Virtual Machine – Linux-b

Java ‘Compiler’Java ‘Compiler’

ByteCodeByteCode

Java Source ProgramJava Source Program

JVM on LINUX PCJVM on LINUX PC

ProgramRunningProgramRunning

Page 10: CS Lesson: Introduction to the Java virtual Machine

Apr 11, 2023 10Java Virtual Machine© Katrin BeckerAll rights reserved.

Java Virtual Machine - Windows

JVM on Windows PCJVM on Windows PC

Java ‘Compiler’Java ‘Compiler’

ByteCodeByteCode

Java Source ProgramJava Source Program

Page 11: CS Lesson: Introduction to the Java virtual Machine

Apr 11, 2023 11Java Virtual Machine© Katrin BeckerAll rights reserved.

Java Virtual Machine – Windows-b

JVM on Windows PCJVM on Windows PC

Java ‘Compiler’Java ‘Compiler’

ByteCodeByteCode

Java Source ProgramJava Source Program

ProgramRunningProgramRunning

Page 12: CS Lesson: Introduction to the Java virtual Machine

Apr 11, 2023 12Java Virtual Machine© Katrin BeckerAll rights reserved.

Java Virtual Machine - iMAC

Java ‘Compiler’Java ‘Compiler’

ByteCodeByteCode

Java Source ProgramJava Source Program

JVM on iMACJVM on iMAC

Page 13: CS Lesson: Introduction to the Java virtual Machine

Apr 11, 2023 13Java Virtual Machine© Katrin BeckerAll rights reserved.

Java Virtual Machine – iMAC-b

Java ‘Compiler’Java ‘Compiler’

ByteCodeByteCode

Java Source ProgramJava Source Program

JVM on iMACJVM on iMAC

ProgramRunningProgramRunning

Page 14: CS Lesson: Introduction to the Java virtual Machine

Apr 11, 2023 14Java Virtual Machine© Katrin BeckerAll rights reserved.

Java Virtual Machine - UNIX

Java ‘Compiler’Java ‘Compiler’

ByteCodeByteCode

Java Source ProgramJava Source Program

JVM on UNIXJVM on UNIX

Page 15: CS Lesson: Introduction to the Java virtual Machine

Apr 11, 2023 15Java Virtual Machine© Katrin BeckerAll rights reserved.

Java Virtual Machine – UNIX-b

Java ‘Compiler’Java ‘Compiler’

ByteCodeByteCode

Java Source ProgramJava Source Program

JVM on UNIXJVM on UNIX ProgramRunningProgramRunning

Page 16: CS Lesson: Introduction to the Java virtual Machine

Apr 11, 2023 16Java Virtual Machine© Katrin BeckerAll rights reserved.

Java Virtual Machine ..TA-Da

JVM on Windows PCJVM on Windows PC

Java ‘Compiler’Java ‘Compiler’

ByteCodeByteCode

Java Source ProgramJava Source Program

JVM on iMACJVM on iMAC

JVM on UNIXJVM on UNIX

JVM on LINUX PCJVM on LINUX PC

ProgramRunningProgramRunning

ProgramRunningProgramRunning

ProgramRunningProgramRunning

ProgramRunningProgramRunning

Page 17: CS Lesson: Introduction to the Java virtual Machine

Apr 11, 2023 17Java Virtual Machine© Katrin BeckerAll rights reserved.

Once more from the topA slightly different

perspective….

Page 18: CS Lesson: Introduction to the Java virtual Machine

Apr 11, 2023 18Java Virtual Machine© Katrin BeckerAll rights reserved.

Java Source :: JVM -1JVM source written in CJVM source written in C

Somebody writes the “Java Virtual Machine”;Jim Gosling probably wrote the first one.

Page 19: CS Lesson: Introduction to the Java virtual Machine

Apr 11, 2023 19Java Virtual Machine© Katrin BeckerAll rights reserved.

Java Source :: JVM -2JVM source written in CJVM source written in C

C compiler compiles ‘JVM.c’C compiler compiles ‘JVM.c’

JVM.c gets compiled to produce….

Page 20: CS Lesson: Introduction to the Java virtual Machine

Apr 11, 2023 20Java Virtual Machine© Katrin BeckerAll rights reserved.

Java Source :: JVM -3JVM source written in CJVM source written in C

C compiler compiles ‘JVM.c’C compiler compiles ‘JVM.c’

JVM.exe(executable version of

JVM)

JVM.exe(executable version of

JVM)

There has to be a specially tailored one of these for the specific

machine we are currently using.

Page 21: CS Lesson: Introduction to the Java virtual Machine

Apr 11, 2023 21Java Virtual Machine© Katrin BeckerAll rights reserved.

Java Source :: JVM –3bJVM source written in CJVM source written in C

C compiler compiles ‘JVM.c’C compiler compiles ‘JVM.c’

JVM.exe(executable version of

JVM)

JVM.exe(executable version of

JVM)

The ‘Java Virtual Machine’ is now ready to be used (invoked).

It can sit around on the system indefinitely.

Page 22: CS Lesson: Introduction to the Java virtual Machine

Apr 11, 2023 22Java Virtual Machine© Katrin BeckerAll rights reserved.

Java Source :: JVM -4JVM source written in CJVM source written in C

C compiler compiles ‘JVM.c’C compiler compiles ‘JVM.c’

JVM.exe(executable version of

JVM)

JVM.exe(executable version of

JVM)

Java Source Program:MyProg.java

Java Source Program:MyProg.java

Along comes My Program,written in Java

Page 23: CS Lesson: Introduction to the Java virtual Machine

Apr 11, 2023 23Java Virtual Machine© Katrin BeckerAll rights reserved.

Java Source :: JVM -5JVM source written in CJVM source written in C

C compiler compiles ‘JVM.c’C compiler compiles ‘JVM.c’

JVM.exe(executable version of

JVM)

JVM.exe(executable version of

JVM)

Java Source Program:MyProg.java

Java Source Program:MyProg.java

Java ‘Compiler’Java ‘Compiler’

>> javac MyProg.javaResults in…..

The Java compiler gets invoked –

It takes standard Java source as input

And produces standard Java ByteCode as output

Page 24: CS Lesson: Introduction to the Java virtual Machine

Apr 11, 2023 24Java Virtual Machine© Katrin BeckerAll rights reserved.

Java Source :: JVM -6JVM source written in CJVM source written in C

C compiler compiles ‘JVM.c’C compiler compiles ‘JVM.c’

JVM.exe(executable version of

JVM)

JVM.exe(executable version of

JVM)

Java Source Program:MyProg.java

Java Source Program:MyProg.java

Java ‘Compiler’Java ‘Compiler’

ByteCodeByteCode

Now I am free to take my MyProg.class toany computer I choose, Any brand; any flavour; anywhere in the worldPROVIDED…It has a working JVM.exe

Page 25: CS Lesson: Introduction to the Java virtual Machine

Apr 11, 2023 25Java Virtual Machine© Katrin BeckerAll rights reserved.

Java Source :: JVM –6bJVM source written in CJVM source written in C

C compiler compiles ‘JVM.c’C compiler compiles ‘JVM.c’

JVM.exe(executable version of

JVM)

JVM.exe(executable version of

JVM)

Java Source Program:MyProg.java

Java Source Program:MyProg.java

Java ‘Compiler’Java ‘Compiler’

ByteCodeByteCode

When I say:>> java MyProg.class

The target machine loads, and then starts to run theJava Virtual Machine….

Page 26: CS Lesson: Introduction to the Java virtual Machine

Apr 11, 2023 26Java Virtual Machine© Katrin BeckerAll rights reserved.

Java Source :: JVM -7JVM source written in CJVM source written in C

C compiler compiles ‘JVM.c’C compiler compiles ‘JVM.c’

JVM.exe(executable version of

JVM)

JVM.exe(executable version of

JVM)

Java Source Program:MyProg.java

Java Source Program:MyProg.java

Java ‘Compiler’Java ‘Compiler’

ByteCodeByteCode

Machine-dependent Linker/Loader

Machine-dependent Linker/Loader

Page 27: CS Lesson: Introduction to the Java virtual Machine

Apr 11, 2023 27Java Virtual Machine© Katrin BeckerAll rights reserved.

Java Source :: JVM -8JVM source written in CJVM source written in C

C compiler compiles ‘JVM.c’C compiler compiles ‘JVM.c’

JVM.exe(executable version of

JVM)

JVM.exe(executable version of

JVM)

Java Source Program:MyProg.java

Java Source Program:MyProg.java

Java ‘Compiler’Java ‘Compiler’

ByteCodeByteCode

Machine-dependent Linker/Loader

Machine-dependent Linker/Loader

‘Java’ ProgramRunning bytecode

as input

‘Java’ ProgramRunning bytecode

as input

The JVM expectsa file containingByteCode as input

Page 28: CS Lesson: Introduction to the Java virtual Machine

Apr 11, 2023 28Java Virtual Machine© Katrin BeckerAll rights reserved.

Java Source :: JVM -9JVM source written in CJVM source written in C

C compiler compiles ‘JVM.c’C compiler compiles ‘JVM.c’

JVM.exe(executable version of

JVM)

JVM.exe(executable version of

JVM)

Java Source Program:MyProg.java

Java Source Program:MyProg.java

Java ‘Compiler’Java ‘Compiler’

ByteCodeByteCode

Machine-dependent Linker/Loader

Machine-dependent Linker/Loader

MyProg.class ProgramRunning

MyProg.class ProgramRunning

‘Java’ ProgramRunning bytecode

as input

‘Java’ ProgramRunning bytecode

as input

The output from the JVM is the output that your program produces (unless of course the JVM detects a problem)

Page 29: CS Lesson: Introduction to the Java virtual Machine

Apr 11, 2023 29Java Virtual Machine© Katrin BeckerAll rights reserved.

Java Source :: JVM -10

Java ‘Compiler’Java ‘Compiler’

ByteCodeByteCode

Java Source Program:MyProg.java

Java Source Program:MyProg.java

C compiler compiles ‘JVM.c’C compiler compiles ‘JVM.c’

JVM.exe(executable version of

JVM)

JVM.exe(executable version of

JVM)

Machine-dependent Linker/Loader

Machine-dependent Linker/Loader

MyProg.class ProgramRunning

MyProg.class ProgramRunning

‘Java’ ProgramRunning bytecode

as input

‘Java’ ProgramRunning bytecode

as input

MyProg output

MyProg output

JVM source written in CJVM source written in C

Page 30: CS Lesson: Introduction to the Java virtual Machine

Apr 11, 2023 30Java Virtual Machine© Katrin BeckerAll rights reserved.

The end.