120 likes | 255 Views
Computer Programming. Your First Java Program: HelloWorld.java. HelloWorld.java. // Name: your name here // Date: today’s date here // Program: HelloWorld.java public class HelloWorld { public static void main (String[] args) { System.out.println ("Hello World!"); } }.
E N D
Computer Programming Your First Java Program: HelloWorld.java
HelloWorld.java • // Name: your name here • // Date: today’s date here • // Program: HelloWorld.java • public class HelloWorld • { • public static void main (String[] args) • { • System.out.println ("Hello World!"); • } • } IT: Beginning Computer Programming- First Java Program
Comments • // Name: your name here • // Date: today’s date here • // Program: Hello World Lines 1-3 are comments. Comments begin with // Comments are ignored by the compiler Used to give information to the reader IT: Beginning Computer Programming- First Java Program
Program Declaration • public class HelloWorld Line 4 is the program declaration. “public” is a key word indicating that the class file is usable by the public. “class” indicates that the file is a program. Classes are the building blocks of Java. “HelloWorld” is the name of the file. Java is case-sensitive. The file must be saved as “HelloWorld.java”. IT: Beginning Computer Programming- First Java Program
Braces • { Line 5 begins the program with an opening curly brace({) Braces enclose statements that make up a programming block. Each opening brace must have a matching closing brace. IT: Beginning Computer Programming- First Java Program
The main method • public static void main (String[] args) Line 6 is the main method declaration. A method contains programming statements. Every Java application must have a “main” method. (String[] args) is the parameter for the main method. Parameters will be discussed in detail later. IT: Beginning Computer Programming- First Java Program
Braces • { Line 7 begins the main method with an opening curly brace({). Braces enclose statements that make up a programming block. Each opening brace must have a matching closing brace. IT: Beginning Computer Programming- First Java Program
The main method • System.out.println ("Hello World!"); Line 8 prints a line of text. The text that will be printed is enclosed in parentheses. The statement ends with a semicolon. “Hello World” is the text that will be displayed. Text enclosed in quotes is called a string. IT: Beginning Computer Programming- First Java Program
Closing Braces • } • } Line 9 and 10 close the braces that were opened on lines 5 and 7. Indenting is not necessary but helps to make the program more readable. IT: Beginning Computer Programming- First Java Program
Running the Program When you run the program, Hello World! Should display in the output window. IT: Beginning Computer Programming- First Java Program
Introducing Errors Programmers are always fixing errors. We will explore a few types of errors: Line 6: public static void main (String[] args) Change it to: public static vod main (String[] args) Compile and you will see the error: “cannot find symbol class vod” – therefore you need to correct the spelling. This is a compile-time error. IT: Beginning Computer Programming- First Java Program
Ending the Lesson Play around making changes and noting the errors. Do not introduce more than one error at a time! IT: Beginning Computer Programming- First Java Program