copyright © texas education agency, 20131 computer programming your first java program:...
TRANSCRIPT
Copyright © Texas Education Agency, 2013 1
Computer Programming
Your First Java Program: HelloWorld.java
HelloWorld.java
1. // Name: your name here
2. // Date: today’s date here
3. // Program: HelloWorld.java
4. public class HelloWorld
5. {
6. public static void main (String[] args)
7. {
8. System.out.println ("Hello World!");
9. }
10. }
© UNT in partnership with TEA IT: Beginning Computer Programming- First Java Program 2
Comments
1. // Name: your name here
2. // Date: today’s date here
3. // 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 3
Program Declaration
4. 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 4
Braces
5. {
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 5
The main method
6. 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 6
Braces
7. {
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 7
The main method
8. 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 8
Closing Braces
9. }
10. }
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 9
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 10
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 11
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 12