Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compiling and the Java Virtual Machine (JVM). The syntax of Pseudocode is pretty loose –visual validation encourages a permissive approach –emphasized.

Similar presentations


Presentation on theme: "Compiling and the Java Virtual Machine (JVM). The syntax of Pseudocode is pretty loose –visual validation encourages a permissive approach –emphasized."— Presentation transcript:

1 Compiling and the Java Virtual Machine (JVM)

2 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

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

4 About Java

5 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

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

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

8 “Source Code” [.java] Java compiler Generic “Byte Code” [.class] OS-specific JVM interpreter OS-specific “Object Code” Execute program Compilation Need one of these for every different OS

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

10 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!

11 Demo >javac HelloWorld.java >java HelloWorld Hello World! >

12 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

13 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. METHODS –THE MODULES WILL BE CALLED METHODS main –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

14 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!”); }

15 A look ahead... Pseudocode if some_boolean then a <- a + 1 else a <- a + 2 b <- 7 endif Note: Indentation is used in both cases. Means nothing to compiler. = instead of <- (more later) Must have parens in Java Java if(some_boolean) a = a + 1; else { a = a + 2; b = 7; } Note ; placement

16


Download ppt "Compiling and the Java Virtual Machine (JVM). The syntax of Pseudocode is pretty loose –visual validation encourages a permissive approach –emphasized."

Similar presentations


Ads by Google