Presentation is loading. Please wait.

Presentation is loading. Please wait.

How do we make our HelloTester.java program do something? The java in our HelloTester.java file won’t do anything by itself. We need to tell the computer.

Similar presentations


Presentation on theme: "How do we make our HelloTester.java program do something? The java in our HelloTester.java file won’t do anything by itself. We need to tell the computer."— Presentation transcript:

1 How do we make our HelloTester.java program do something? The java in our HelloTester.java file won’t do anything by itself. We need to tell the computer to execute the program in the file. This is a two-stage process. First stage: Compile the program into a concise set of instructions your computer can carry out directly >javac HelloTester.java Second stage: Tell the computer to carry out the compiled instructions >java HelloTester Welcome to Java javac stands for java compiler Tells the computer to execute the compiled java instructions in HelloTester.class javac puts the compiled instructions in a new file HelloTester.class

2 Translating from high to low..... Human speaks English Programmer encodes problem in a high-level Language (Java) Compiler Translates Java into compiled code Computer executes machine code instructions

3 public class HelloTester { public static void main(String[] args){ System.out.println(“Hello World”); } Java source code in a file called HelloTester.java 3%(*$&£.... (*&£*&£ *(&^^!__£+ ~@{W}{~@W}{~@ (£()*&(*H###~}{}{} (*(*(*(*£ Java machine code in file HelloTester.class 10010101001010010001111110001010 Program instructions carried out JAVAC compiles the source code JAVA virtual machine executes code

4 Writing your first program Open a new file in Notepad (or any other simple editor; not Word, tho!). You’ll find Notepad under Programs\Accessories. Type the HelloTester.java code into this new file. Save the file in the Java folder in your home directory, with the name HelloTester.java You have now written and saved your first draft of the program. Next you need to compile your program (convert it into a form that can do something), and once compiled, to run it (see what it does). Exit Notepad.

5 Compiling your program Open the command prompt on your computer. You’ll find this under Programs\Accessories. The command prompt is a window that allows you to type commands directly to the computer. Type cd Java  (this command puts you in the Java folder in your C: disk) Type dir  to get a dir ectory of all files in C:\Java (you will see HelloTester.java) Type C:  at the command prompt (this puts you in your C: disk) Type mkdir Java  (this command creates a Java folder for you in your C: disk)

6 Compiling your program (continued) Type dir  to get a dir ectory of all files in C:\Java (you will now see HelloTester.java and HelloTester.class, if there were no compilation errors) Type javac HelloTester.java  The javac command compiles your high-level HelloTester.java file into a bytecode program in a file HelloTester.class If there are any “grammatical” errors in your HelloTester.java program (and there often are!) javac will print them out.

7 Running your compiled program Type java HelloTester  to run the HelloTester program The java command gets the file HelloTester.class and executes the machine code (bytecode) commands in that file. If your HelloTester.java program compiled correctly (with no errors), you will now have a HelloTester.class that can be run. Commands java HelloTester.class or java HelloTester.java won’t work because the java command will look for HelloTester.class.class or HelloTester.java.class If your program is written correctly, when run it’ll display Welcome to Java! on the command prompt

8 Homework Read Horstmann, Chapter 1 Try programming exercise P1.1. You can use the HelloTester.java program as a template for this programming exercise. You will also be doing programming exercises in your practicals. Next we will take a tour around Java using the BouncingBall program. At the end of that tour you will have an understanding of some important ideas in java.

9 Computer labs start next week (15 th ) Where are the labs? Go up the stairs in the computer science building to the first floor. Turn right and go down to the end of the corridor; the first year labs are facing you. The labs are called the “active learning labs”. Bring your laptop!!! Labs are Tuesday 2-4 or Thursday 2-4; pick one time.


Download ppt "How do we make our HelloTester.java program do something? The java in our HelloTester.java file won’t do anything by itself. We need to tell the computer."

Similar presentations


Ads by Google