Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit 2: Java Introduction to Programming 2.1 Initial Example.

Similar presentations


Presentation on theme: "Unit 2: Java Introduction to Programming 2.1 Initial Example."— Presentation transcript:

1 Unit 2: Java Introduction to Programming 2.1 Initial Example

2 Example Program Throughout this section, we will be breaking down a small program line by line to explain Java syntax. public class FirstProg { public static void main(String[] args) { System.out.println("Hello World"); }

3 Example Program It is important for you to become familiar with the syntax of Java so that you can make use of it to write your own programs. Keep in mind that the explanations right now will be simplified. There will be more in depth explanations for the syntax in later chapters.

4 First line 1. The first line of code shows the syntax for the declaration of a class: public class FirstProg All user written programs are classes. The class is public, which means it is freely available for general use. The name of the class is “FirstProg”.

5 Second Line 2. The second line of code is quite easy to explain, and it goes with the last line of code. { All class definitions are enclosed in a matched set of braces, {}.

6 Third line 3. The third line of code introduces a method. public static void main(String[] args) The name of this method is “main()”. A stand-alone program has to have a main() method. When you ask the Java system to run the program, the system looks inside the program class and causes this method to run.

7 Third line public static void main(String[] args) The keyword “public” signifies that the method is freely available for the system to call. The keyword “static” signifies that main() is a general purpose method, not one that is used on an object. This will be explained in greater detail later. The keyword “void” indicates that running this method does not cause any value to be returned.

8 Third line public static void main(String[] args) Every method name is followed by a matched pair of parentheses. When methods are referred to in these notes, they will always be referred to by name plus parentheses, such as “main()”. This makes it clear that the name “main” is the name of a method rather than the name of something else.

9 Third line public static void main(String[] args) The main() method has something in the parentheses. These are parameters for command line arguments. This program does not make use of them, but it is still necessary to include the declaration in the code. The actual form of the declaration will be explained in a future set of notes.

10 Fourth and sixth lines 4. The fourth and sixth lines of code are quite easy to explain. Fourth line: { Fifth line: System.out.println("Hello World"); Sixth line: } All method definitions are enclosed in a matched set of braces, {}.

11 Fifth line 5. The fifth line of code does the real work of the program. System.out.println("Hello World"); Here the method println() is called. This causes output to be sent to the Console. In general a method is called “on” something, and that is symbolized by the words System and out and the use of dots. The method is called with the parameter “Hello World”. Calling methods is important and will be a major topic in the note files.

12 What does this program really do? If you were able to compile and run the program when it was given in Unit 1, you know what it does: It prints the message “Hello World” in the Console. The ability to accomplish this is the starting point for learning how to do other things in Java.


Download ppt "Unit 2: Java Introduction to Programming 2.1 Initial Example."

Similar presentations


Ads by Google