No Slide Title

Download Report

Transcript No Slide Title

How Java Programs Work
MIS 3023
Business Programming Concepts II
The University of Tulsa
Professor: Akhilesh Bajaj
All slides in this presentation ©Akhilesh Bajaj, 2008. All rights reserved
Objectives
• Understand how Programs are Executed on a Computer
• Understand how a Java program works
TM
Let’s get started!
How Programs Work on a Computer
Levels of Computer Programming Languages:
-First Generation Language:
Machine Code
1101110100111100
1001101110101010
-Second Generation Language:
Assembler
Load X into register a
-Third Generation Language:
Pascal, Fortran, COBOL, C, C++, Java
X = 7;
-Fourth Generation Language:
Visual Basic, Oracle Developer
Use a mouse & widgets to create a GUI that executes interface
events
How Programs Work on a Computer
• The CPU (Central Processing Unit) executes the instruction set
(machine code)
• Every program needs to be compiled and ultimately fed to the CPU
in the CPU’s language (the instruction set)
• Different CPUs have different instruction sets (e.g., X86 versus
Motorola)
• The quality of the instruction set can play a bigger role in the
performance than other numbers like clock speed.
• A compiler is a computer program that translates the source code
into either machine code or an intermediate language.
How Programs Work on a Computer
• Once the source code has been compiled into an executable (like
a .exe or .bat file on the PC) it can be executed by the CPU.
• First, the executable is loaded into main memory (RAM).
• One of the registers in the CPU is the PC (program counter).
This has the main memory address of the next instruction to be
executed.
• The CPU fetches the first instruction, executes it, and the PC is
updated with the address of the next instruction (which is usually
the next line, but may be a jump to another procedure).
How Programs Work on a Computer
• In addition to instructions, the program also has data (variables, etc)
How are these data stored usually?
• The data locations (referenced by variable names in the source
program) are also in main memory (RAM). The CPU needs to be able
to fetch these data locations, load them into registers and then perform
calculations with them.
• At the hardware level, the CPU is nothing but a a large logical circuit,
that, if fed a certain logical input, generates a predetermined output.
The set of all possible inputs is the instruction set of the CPU.
• The CPU does not “understand” the instructions we feed it in
any sense. It simply responds mechanistically to inputs and produces
the same output each time for a given input.
How Programs Work on a Computer
Figure From our Text Book: Shows the Fetch-and-Execute Cycle
How Programs Work on a Computer
• For every programming language, the compiler is a program that
has a “dictionary” that translates every possible source code statement
into a corresponding set of instructions at the lower level
(machine code or some intermediate language).
• Every program executes as a process. The task manager in
Windows shows us the processes that are running on our machine.
• The CPU can also be interrupted by devices like the keyboard and
secondary storage (hard disk). This allows multi-programming:
E.g., One process is executing, but it needs to read a file from the hard
disk. The CPU sends a request to the hard disk manager, and then
puts this process in its process queue, takes the next process and
executes it. Once the first process’s data is available, the CPU gets the
interrupt, takes that first process back in, and continues to execute it.
How Java Programs Work
• One of the strong points of Java is that it is
compile once-run anywhere.
• How can we do this, if different CPUs (say the X86 CPU in PCs
versus the Motorola CPU in Macintosh versus a SUN SPARC
CPU) have different instruction sets?
• Indirection: Create an intermediate layer of instructions using a
2-stage compiler. The first stage takes the .java file and converts it
into a .class file of the same name. This is done using the javac
program that comes as part of the java SDK. We used this in our first
lab to create HelloWorld.class.
•The code in the .class file is called Java bytecode.
How Java Programs Work
• This Java bytecode is then run on the JVM (Java Virtual Machine).
• The JVM is an interpreter, that goes through each line in the
bytecode and translates it into the set of instructions for that particular
CPU. Clearly different CPUs need different JVMs.
• Once the JVM is built for that CPU, it will execute any .class file.
• So, the same .class files will work on any machine that has a JVM.
• Advantages:
-Programmers do not have to distribute source code, only .class fil
-Programmers do not have to rewrite code if we switch from
say PC to apple machines (reuse same .class files)
-The JVM can act as a security buffer when code is downloaded
from the network (prevents .class files from doing bad things)
How Java Programs Work
• The JVM is called by the command java that we used in our
first lab. So >java HelloWorld executed the .class file and
printed out “Hello World”.
• In order to use Java, we need to make sure our CLASSPATH
environment variable includes the /lib subdirectory that is part of the
SDK. This subdirectory contains all the extensive library classes that
we can use. One of the major benefits of Java is that it has an extensive
set of these classes whose code can be reused.
• The CLASSPATH should also have the . in it, so we can also use
classes we created that are in the current working directory
• Finally, the PATH environment variable should have the /bin
subdirectory in the SDK, since the java and javac programs are
found there.
CONCLUSION
• We learnt how programs are executed
• We learnt how the java environment works
• Supporting material is in sections 1.1-1.3 in our online
textbook.