intro - Introduction to Programming with Java What is...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Programming with Java What is Programming? A computer program is a list of specific instructions that the computer can execute. We can tell the computer to print something, to add numbers, to repeat a sequence of instructions, etc. Just like there are rules to writing sentences in English, there are rules we must follow when writing computer programs. If we don’t follow the rules, the computer won’t understand what we want. Hello, World Program Here is a very simple Java program that prints out “Hello, World!” to the screen: public class Hello { public static void main(String[] args) { System.out.println(“Hello, World!”); } } This text is stored in the file Hello.java . We will learn much more about what everything means as the course progresses, but for now let’s discuss each line separately: 1) public class Hello – this line begins a class in our program. For now, just remember that the name you give the class (in this case, “ Hello ”) must match the name of the file (in this case, Hello.java ). Capitalization matters – we could not name the class “ hello ”. 2) { – this bracket opens up the class 3) public static void main(String[] args) – this line declares the main method for our program, which is where the program begins. For now, just copy this line into all your programs 4) { – this bracket opens up the main method 5) System.out.println(“Hello, World!”); – this line prints “Hello, World!” to the screen 6) } – this bracket ends the main method 7) } – this line ends the class Compiling Once you have written a computer program, you need to compile it. This process does two things: 1) Checks to see if your program has any errors 2) Converts your program into an executable file that the computer can run To compile your program, open a Command Prompt ( Start->Run , type cmd ). Use
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
cd dirName to change into a directory called dirName . You can also do: dir to list the contents in the current directory. Once you have changed to the directory where your program is saved, you can compile your program like this: javac Name.java (where Name.java is the name of your program). For example, to compile the Hello, World program you would do: javac Hello.java If your program has any errors, the compiler will print a list of what is wrong and where the problem is. If there are no errors, it will create the file Name.class (where Name is the name of the program file). For example when you compile the Hello, World program it will generate the file Hello.class . If you get an error saying that the javac command is not recognized, please refer to the tutorial on Compiling and Running Java programs. Executing After you have compiled your program and generated a class file, then you are ready to run it. At the command prompt, type:
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/02/2010 for the course CIS 200 taught by Professor Staff during the Spring '08 term at Kansas State University.

Page1 / 14

intro - Introduction to Programming with Java What is...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online