Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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. • 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! 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. About Java 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 HTML Only Just Ultimate Programming Language” or another web-content language useful for web applets Another Vacuous Acronym 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. 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) Compilation “Source Code” [.java] Execute program Java compiler OS-specific “Object Code” Generic “Byte Code” [.class] OS-specific JVM interpreter Need one of these for every different OS 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... Sample 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 Then we execute by typing: java HelloWorld Hello World! Demo >javac HelloWorld.java >java HelloWorld Hello World! > Quick Trix • The name of the file must match the name of the class EXACTLY!!! • File: Bubba.java • Contains: Capitalization counts class Bubba {... • Everything must be EXACTLY correct!!! 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 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 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!”); } } A look ahead... • Pseudocode • Java if some_boolean then a <- a + 1 else a <- a + 2 b <- 7 endif 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 <- (more later) Must have parens in Java