Your First Java Program: HelloWorld.java Computer Programming Your First Java Program: HelloWorld.java Copyright © Texas Education Agency, 2013 Trade & Industrial Education
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!"); } © UNT in partnership with TEA IT: Beginning Computer Programming- First Java Program Trade & Industrial Education
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 Copyright © Texas Education Agency, 2013 IT: Beginning Computer Programming- First Java Program Trade & Industrial Education
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”. Copyright © Texas Education Agency, 2013 IT: Beginning Computer Programming- First Java Program Trade & Industrial Education
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. Copyright © Texas Education Agency, 2013 IT: Beginning Computer Programming- First Java Program Trade & Industrial Education
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. Copyright © Texas Education Agency, 2013 IT: Beginning Computer Programming- First Java Program Trade & Industrial Education
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. Copyright © Texas Education Agency, 2013 IT: Beginning Computer Programming- First Java Program Trade & Industrial Education
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. Copyright © Texas Education Agency, 2013 IT: Beginning Computer Programming- First Java Program Trade & Industrial Education
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. Copyright © Texas Education Agency, 2013 IT: Beginning Computer Programming- First Java Program Trade & Industrial Education
Running the Program When you run the program, Hello World! Should display in the output window. Copyright © Texas Education Agency, 2013 IT: Beginning Computer Programming- First Java Program Trade & Industrial Education
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. Copyright © Texas Education Agency, 2013 IT: Beginning Computer Programming- First Java Program Trade & Industrial Education
Ending the Lesson Play around making changes and noting the errors. Do not introduce more than one error at a time! Copyright © Texas Education Agency, 2013 IT: Beginning Computer Programming- First Java Program Trade & Industrial Education