Title: Compiling and the Java Virtual Machine JVM
1Compiling and the Java Virtual Machine (JVM)
2Working with a real language
- The syntax of Pseudocode is pretty loose
- visual validation encourages a permissive
approach - emphasized the importance of structure
- Compilers essentially require perfection.
- The procedure for writing your algorithms wont
change (much!) - Write a high level approach focusing on
algorithm. - Write the code concentrating on details.
- Check it by hand before compiling.
- Because we're introducing a real language at the
same time as we're introducing the object
oriented paradigm we will require you to just
write some code. - You may not understand it all immediately
- Eventually we'll explain!
3The First Part
- Initially well use Java to write procedural code
just to let you get the feel of a real language. - In fact, well start by programming some of the
same types of modules that we used in the
procedural pseudocode earlier in the semester. - Then, as we introduce the Object Oriented
paradigm well use Java as it was intended.
4About Java
5Introduction to Java
- What Java is
- A real professional programming language
- (which is good and bad...)
- Portable across any hardware platform that has a
JVM interpreter (more later) - An object-oriented language
- What Java is not
- The Ultimate Programming Language
- HTML or another web-content language
- Only useful for web applets
- Just Another Vacuous Acronym
6Real Languages
- Real languages require the programmer to write
code using a very specific set of rules, keywords
and syntax. - Then this code is transformed into actual
instructions that a specific machine can execute.
Often a complex process - A number of strategies have been invented to
accomplish this process - Assemblers
- Compilers
- Interpreters
- A traditional problem has been the necessity to
have different versions of a program for
different machines.
7A New Idea
- Java was specifically developed to be able to
write code once and run it anywhere - How is this magic accomplished?
- Using an intermediate language! Byte code.
- The Byte code is interpreted (executed) using a
special piece of software (a program) called the
Java Virtual Machine (JVM)
8Compilation
Source Code .java
Java compiler
Generic Byte Code .class
OS-specificJVM interpreter
OS-specific Object Code
Execute program
Need one of these for every different OS
9Structure of Java Programs
- Initially well write programs that fit in one
file. - Create the file with an editor (e.g. emacs)
- Compile with javac producing a .class file
- Execute by running java and sending it the .class
file - Our first (tiny) program will be roughly like a
cross between an algorithm and a procedure. - Lets take a look...
10Sample Application
We create a file (using an editor) called
HelloWorld.java
- public class HelloWorld
-
- public static void main(String argv)
-
- System.out.println(Hello World!)
-
We compile by typing (at the OS prompt) javac
HelloWorld.java Which produces HelloWorld.class Th
en we execute by typing java HelloWorld
Hello World!
11Demo
- gtjavac HelloWorld.java
- gtjava HelloWorld
- Hello World!
- gt
12Quick Trix
- The name of the file must match the name of the
class EXACTLY!!! - File Bubba.java
- Contains
- Everything must be EXACTLY correct!!!
Capitalization counts
class Bubba ...
13Eventually...
- Applications (normal computer programs)
- Each program consists of multiple files.
- Each file contains a class.
- Each class will contain modules which will
resemble procedures and functions. - THE MODULES WILL BE CALLED METHODS
- The .class file that you send to java must
contain a method named main - It will actually look like this
- public static void main(String argv ) ..
-
- the JVM will use the file naming convention to
find the other classes required by this main
program.
As opposed to Applets
14Some basic syntax issues
- Your TA is smarter than the java compiler
- Lines need to terminate with a
- Easier said than done
- Braces will be used to indicate "blocks" of code
- Which essentially act like a single line
public class HelloWorld public static void
main(String argv) System.out.println(He
llo World!)
15A look ahead...
- Pseudocode
- if some_boolean then
- a lt- a 1
- else
- a lt- a 2
- b lt- 7
- endif
- Java
- if(some_boolean)
- a a 1
- else
-
- a a 2
- b 7
-
Note placement
Note Indentation is used in both cases. Means
nothing to compiler. instead of lt- (more
later) Must have parens in Java
16(No Transcript)