16
Compiling and the Java Virtual Machine (JVM)

Compiling and the Java Virtual Machine (JVM)

  • Upload
    teigra

  • View
    29

  • Download
    0

Embed Size (px)

DESCRIPTION

Compiling and the Java Virtual Machine (JVM). Working 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. - PowerPoint PPT Presentation

Citation preview

Page 1: Compiling and the Java Virtual Machine (JVM)

Compiling and the Java Virtual Machine (JVM)

Page 2: Compiling and the Java Virtual Machine (JVM)

• 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 won’t

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!

Working with a real language

Page 3: Compiling and the Java Virtual Machine (JVM)

The First Part• Initially we’ll use Java to write procedural code just to let you get the feel of a real language.

• In fact, we’ll 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 we’ll use Java as it was intended.

Page 4: Compiling and the Java Virtual Machine (JVM)

About Java

Page 5: Compiling and the Java Virtual Machine (JVM)

Introduction 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

Page 6: Compiling and the Java Virtual Machine (JVM)

Real 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.

Page 7: Compiling and the Java Virtual Machine (JVM)

A 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)

Page 8: Compiling and the Java Virtual Machine (JVM)

“Source Code” [.java]

Javacompiler

Generic“Byte Code” [.class]

OS-specificJVM

interpreter

OS-specific“Object Code”

Executeprogram

Compilation

Need one of these for every different

OS

Page 9: Compiling and the Java Virtual Machine (JVM)

Structure of Java Programs• Initially we’ll 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.

• Let’s take a look...

Page 10: Compiling and the Java Virtual Machine (JVM)

Sample Application

public class HelloWorld{ public static void main(String argv[]) { System.out.println(“Hello World!”); }}

We create a file (using an editor) called “HelloWorld.java”

We compile by typing (at the OS prompt):

javac HelloWorld.java

Which produces HelloWorld.class

Then we execute by typing:

java HelloWorld

Hello World!

Page 11: Compiling and the Java Virtual Machine (JVM)

Demo>javac HelloWorld.java>java HelloWorldHello World!>

Page 12: Compiling and the Java Virtual Machine (JVM)

Quick Trix• The name of the file must match the name of the class EXACTLY!!!• File: Bubba.java• Contains:

• Everything must be EXACTLY correct!!!

class Bubba{...

Capitalization counts

Page 13: Compiling and the Java Virtual Machine (JVM)

Eventually...• 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 METHODSMETHODS– The .class file that you send to java must contain a method named mainmain

– 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

Page 14: Compiling and the Java Virtual Machine (JVM)

Some 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(“Hello World!”); }}

Page 15: Compiling and the Java Virtual Machine (JVM)

A look ahead...• Pseudocodeif some_boolean then

a <- a + 1else

a <- a + 2b <- 7

endif

Note: Indentation is used in both cases. Means nothing to compiler.

= instead of <- (more later)

Must have parens in Java

• Javaif(some_boolean)

a = a + 1;else{

a = a + 2;b = 7;

}Note

;placement

Page 16: Compiling and the Java Virtual Machine (JVM)