unit 2: java introduction to programming 2.1 initial example

12
Unit 2: Java Introduction to Programming 2.1 Initial Example

Upload: gloria-wells

Post on 24-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Unit 2: Java Introduction to Programming 2.1 Initial Example

Unit 2: Java Introduction to Programming2.1 Initial Example

Page 2: Unit 2: Java Introduction to Programming 2.1 Initial Example

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");}

}

Page 3: Unit 2: Java Introduction to Programming 2.1 Initial Example

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.

Page 4: Unit 2: Java Introduction to Programming 2.1 Initial Example

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”.

Page 5: Unit 2: Java Introduction to Programming 2.1 Initial Example

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, {}.

Page 6: Unit 2: Java Introduction to Programming 2.1 Initial Example

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.

Page 7: Unit 2: Java Introduction to Programming 2.1 Initial Example

Third linepublic 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.

Page 8: Unit 2: Java Introduction to Programming 2.1 Initial Example

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.

Page 9: Unit 2: Java Introduction to Programming 2.1 Initial Example

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.

Page 10: Unit 2: Java Introduction to Programming 2.1 Initial Example

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, {}.

Page 11: Unit 2: Java Introduction to Programming 2.1 Initial Example

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.

Page 12: Unit 2: Java Introduction to Programming 2.1 Initial Example

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.