66
Introduction to programming in the Java programming language

Introduction to programming in the Java programming language

Embed Size (px)

Citation preview

Page 1: Introduction to programming in the Java programming language

Introduction to programming in the Java programming language

Page 2: Introduction to programming in the Java programming language

Similarity between a computer program and a book

• The structure of a computer program resembles the structure of a text document, like a book

You all know what a book look like and I will use the structure of a book to illustrate the structure of a (Java) program

Page 3: Introduction to programming in the Java programming language

Similarity between a computer program and a book (cont.)

• Quick review of what a book look like:

• A book consists of a number of chapters

• Each chapter consists of a number of paragraphs

• Each paragraph consists of a number of sentences

• Each sentence must obey the syntax rules in the English language

Page 4: Introduction to programming in the Java programming language

Similarity between a computer program and a book (cont.)

• Quick overview of what a Java program look like:

• A Java program consists of a number of classes

• Each class consists of a number of methods (and some variables)

• Each method consists of a number of (program) statements

• Each statement must obey the syntax rules in the Java programming language

Page 5: Introduction to programming in the Java programming language

What a Java program look like

• The following picture shows what a Java program look like in its entirety:

Each classes is stored in a separate (UNIX) file with the extension .java

Page 6: Introduction to programming in the Java programming language

What a Java program look like (cont.)

• The execution of a Java program begins with the method with the name main()

(So: one of the methods in the Java program must be named main()...)

Page 7: Introduction to programming in the Java programming language

The first Java program

• The simplest Java program consists of 1 class and the class consists of 1 method called main.

Example:

Page 8: Introduction to programming in the Java programming language

The first Java program (cont.)

• Remember that:

Because you only have 1 method... so the method must be named main --- (in this case, you have no choice...)

• You must have the method main in every Java program

Page 9: Introduction to programming in the Java programming language

The first Java program (cont.)

• This Java program is written as follows:

We will use this simple program to illustrate some concepts of the Java programming language

Page 10: Introduction to programming in the Java programming language

The first Java program (cont.)

• Example Program: (Demo above code)     – Prog file:

http://192.168.1.3/~cheung/teaching/web/170/Syllabus/03/Progs/Hello.java      

• How to run the program:  

• Right click on link and save in a scratch directory

• To compile:   javac Hello.java

• To run:          java Hello

Page 11: Introduction to programming in the Java programming language

Statement: the smallest unit of execution in a Java program

• Statement:

(You cannot execute a portion of a statement)

A statement is like a sentence:   it is the smallest unit that you write in a book.

• Statement = the smallest (unit) of execution in a Java program

Page 12: Introduction to programming in the Java programming language

Statement: the smallest unit of execution in a Java program (cont.)

• Examples of a statement:

• System.out.println("Hello Class");

Page 13: Introduction to programming in the Java programming language

Statement: the smallest unit of execution in a Java program (cont.)

• Effect of a statement:

•A statement tells the computer to do something

Example:

• When the statement       

System.out.println("Hello Class");

is executed, the computer will print the text "Hello Class" to the terminal

Page 14: Introduction to programming in the Java programming language

Types of statements

• There are different kinds (types) of statements in Java

• We will discuss the details of each statement in Java later in the course

Page 15: Introduction to programming in the Java programming language

Syntax of statements

• Each type of statement follows a specific syntax

• We will discuss the details of the syntax of each type of statement in Java later in the course

• This is similar to the English language

• Each type of sentence in English has its own syntax Example:

•An ordinary sentence follows the syntax: subject verb

• A question (sentence) follows syntax: verb subject

Page 16: Introduction to programming in the Java programming language

Method: a container for (many) statements that perform a complex task

• Important facts:

• One statement can only perform a simple operation

• One statement is insufficient to solve a complex problem

• A complex task can be performed by many statements

Page 17: Introduction to programming in the Java programming language

Method: a container for (many) statements that perform a complex task (cont.)

• Method:

• For convenience, you can put a number of statement into a unit called a method

• The method is given a unique name to identify the group of statements.

Page 18: Introduction to programming in the Java programming language

Method: a container for (many) statements that perform a complex task (cont.)

• Example:

Page 19: Introduction to programming in the Java programming language

Defining a method

• Terminology:

• You must use a specify syntax to define a new method

We will discuss a simplified syntax on how to define a method here and discuss the details on how to define methods later

• Define a method = constructing a method

Page 20: Introduction to programming in the Java programming language

Defining a method

• Simplified syntax used to define a method:

MethodProperties METHOD_NAME( ... )

{

statements contained in the method

}

Page 21: Introduction to programming in the Java programming language

Defining a method (cont.)

• Explanation:

• The MethodProperties describes the properties of the new method

• The METHOD_NAME is the name of the method (used for identification)

• The brackets ( ... ) contains parameters for the method

Page 22: Introduction to programming in the Java programming language

Defining a method (cont.)

• You have seen parameters before if you have used a TI-83 calculator:

The X in the formula Y1 = 4sin(X) is a parameter

BTW, sin(..) is a method !!!

Page 23: Introduction to programming in the Java programming language

Defining a method (cont.)

• Parameters provide information to the method • Finally, the braces { ..... } encloses the statements

• Example:

Page 24: Introduction to programming in the Java programming language

Method invocation: executing a method

• The computer can execute a method

• Computer jargon:

• When a method is invoked (executed), then:

• A method is a larger execution unit than a (single) statement

• Invoke a method = execute a method

• all statements contained between the braces { .... } are executed one statement at a time

Page 25: Introduction to programming in the Java programming language

Method invocation: executing a method (cont.)

• Example:

• Java program:

Page 26: Introduction to programming in the Java programming language

Method invocation: executing a method (cont.)

When the Hello.java is run using the command:

the computer will invoke the main() method

• java Hello

Page 27: Introduction to programming in the Java programming language

Method invocation: executing a method (cont.)

• The execution of the main() method will execute the statements contained in the main() method one at a time:

System.out.println("Hello Class"); System.out.println(" How is... ");

Page 28: Introduction to programming in the Java programming language

Header and body of a method

• Computer Science jargon:

• Body of a method = the part of the method definition that is enclosed between the braces { ... }

• Header of a method = the part of the method definition before the body of the method

Page 29: Introduction to programming in the Java programming language

Header and body of a method (cont.)

• Graphically explained:

Page 30: Introduction to programming in the Java programming language

Header and body of a method (cont.)

• Note

• When a method is executed, the statements in its body are executed

Page 31: Introduction to programming in the Java programming language

Keywords (or reserved words)

• Each programming language has reserved a number of words for some specific purpose

• Keyword:

• Keyword = a reserved word in a programming language

Page 32: Introduction to programming in the Java programming language

Keywords (or reserved words) (cont.)

• Examples: public, static and void

Page 33: Introduction to programming in the Java programming language

Keywords (or reserved words) (cont.)

• Keywords have special meaning in a programming language:

• A keyword must be used for that specified purpose

Page 34: Introduction to programming in the Java programming language

Keywords (or reserved words) (cont.)

• Example:

• A public method is a method that has the highest level of accessibility

(Java allows you to define methods with more limited accessibility with the keyword private.

• You will learn about this topic much later - in another course)

Page 35: Introduction to programming in the Java programming language

Identifiers

• Rules to form the name of an identifier in Java:

There are some rules in Java that you must follow to form the identifier name

• Identifier = a name chosen by the programmer to identify something defined inside a program

Page 36: Introduction to programming in the Java programming language

Identifiers (cont.)

• Identifier:

• An identifier consists of a number of characters

• There is no limit on the number of characters in the identifier (but do not try using identifiers that are too long because you will have to type it yourself...)

• The first character of an identifier must be one of the following:

• A letter (a, b, ..., z, A, B, ..., Z), or

• The underscore character

Page 37: Introduction to programming in the Java programming language

Identifiers (cont.)

• The subsequent characters of an identifier must be one of the following:

• Identifiers are case-sensitive !

• You cannot use a keyword as identifier (keywords are reserved for a specific purpose !)

• A letter (a, b, ..., z, A, B, ..., Z), or

• The underscore character, or

• A digit (0, 1, ..., 9)

Page 38: Introduction to programming in the Java programming language

Identifiers (cont.)

• Examples of correct identifiers:

age

Age (is different from age) greaterCommonDivisor

R2D2

radius_of_the_circle

Page 39: Introduction to programming in the Java programming language

Identifiers (cont.)

• Examples of illegal identifiers:

3cpo (cannot start with a digit)

radius-of-the-circle (cannot have minus sign in an identifier)

public (cannot use a keyword !)

Page 40: Introduction to programming in the Java programming language

Class: container for methods

• Important facts:

• One method is used to perform one complex task

• In order to solve one problem, you may need to perform multiple complex tasks For each complex task, you will have to write one method to perform the task.

• The different methods have a common purpose• It makes sense to collect methods that serve

similar purpose together.

Page 41: Introduction to programming in the Java programming language

Class: container for methods (cont.)

• Class:

• For organizational purpose, you can put a number of methods into a unit called a class

• The class is given a unique name

Page 42: Introduction to programming in the Java programming language

Defining (constructing) a class

• You must use a specify syntax to define a new class

• Syntax to define a class:

ClassProperties class CLASSNAME

{

methodDefinitions

}

Page 43: Introduction to programming in the Java programming language

Defining (constructing) a class (cont.)

• Explanation:

• The ClassProperties describes the properties of the new class

• A word class is a keyword The use of this keyword tells Java that you want to define a new class

• The CLASSNAME is an identified used as the name of the new class • The CLASSNAME must be unique (chosen by

the programmer)

• The name of the class must be the same as the name of the file that contain the class

Page 44: Introduction to programming in the Java programming language

Defining (constructing) a class (cont.)

• Example

Page 45: Introduction to programming in the Java programming language

Block: grouping unit in Java

• Block:

• Example:

Because this block is part of a class definition, it is called a class block

• Block = a pair of "{" and "}" braces that groups components in a Java program together

Page 46: Introduction to programming in the Java programming language

Block: grouping unit in Java (cont.)

• Blocks can be nested: one block can be placed inside another block

Example:

The outer block is a class block

The inner block is the body of a method and it is called a method block

Page 47: Introduction to programming in the Java programming language

Block: grouping unit in Java (cont.)

• Notes:

• An opening brace "{" must be matched with a closing brace "}“

Programming tip:

• Whenever you type an opening brace "{", immediately type a closing brace "}" on the next line

• Go back and insert the rest of the program.

Page 48: Introduction to programming in the Java programming language

Java is case-sensitive

• Every letter in a Java program is case-sensitive

Meaning:

• A lower-case letter and its corresponding upper-case letter are counted as different

Example:

• public and Public are not the same.

(You can use Public as an identifier, but not public !)

Page 49: Introduction to programming in the Java programming language

Comments

• Comment:

• Comment = text inside a Java program that is ignored by the Java compiler

• Comments are used to annotate the program to help humans understand the operation of the Java program

Page 50: Introduction to programming in the Java programming language

Comments (cont.)

• Syntax for a comment in Java:

• Single line comment syntax:

The text on the line following the symbol // will be ignored

// ... single line comment

Page 51: Introduction to programming in the Java programming language

Comments (cont.)

• Multiple lines comment syntax:

All text between the comment brackets /* ..... */ will be ignored

/* comment line 1 comment line 2 ... ... */

Page 52: Introduction to programming in the Java programming language

Comments (cont.)

Example:

Page 53: Introduction to programming in the Java programming language

Comments (cont.)

Example Program: (Demo above code)    

• The Hello prog file with comments: http://192.168.1.3/~cheung/teaching/web/170/Syllabus/03/Progs/Hello2.java

How to run the program:    

          • Right click on link and save in a scratch directory

• To compile:   javac Hello2.java

• To run:          java Hello2

Page 54: Introduction to programming in the Java programming language

Java programs are format-free

• The Java compiler (translator) consider white space characters (i.e., SPACE, TAB and New line) as insignificant

• All that the Java compiler cares about is syntactical correctness

Page 55: Introduction to programming in the Java programming language

Java programs are format-free (cont.)

• Consequence:

• You can make a Java program look very ugly and it may still be syntactically correct to the Java compiler

Page 56: Introduction to programming in the Java programming language

Java programs are format-free (cont.)

• Example:

public class Hello2 { public static void main(String[] args) { System.out.println("Hello Class"); System.out.println(" How is everyone doing so far ?"); } }

Page 57: Introduction to programming in the Java programming language

Java programs are format-free (cont.)

• Same program with many insignificant white spaces:

public class Hello3 { public static void main (String[] args) { System.out.println ( "Hello Class"); System.out.println( " How is everyone doing so far ?"); }}

Page 58: Introduction to programming in the Java programming language

Java programs are format-free (cont.)

• (But... I had to call it Hello3 because the name Hello2 is already used)

Page 59: Introduction to programming in the Java programming language

Java programs are format-free (cont.)

• Example Program: (Try it out ! It will run and print the same thing)         – Prog file:

http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/03/Progs/Hello3.java     

• How to run the program:  

• Right click on link and save in a scratch directory

• To compile:   javac Hello3.java

• To run:          java Hello3

Page 60: Introduction to programming in the Java programming language

Java programs are format-free (cont.)

• Moral of this lesson:

• Do yourself a favor

• Don't abuse the format-free feature of Java

• Indent your programs properly so the structure of the algorithm is easily visible It will help you understand what the program is doing and find errors

Page 61: Introduction to programming in the Java programming language

Summary

What a Java program look like abstractly:

Page 62: Introduction to programming in the Java programming language

Summary (cont.)

What a Java program look like more concretely:

Page 63: Introduction to programming in the Java programming language

Summary (cont.)

• Keyword = a (English) word that is reserved for a special purpose

• Identifier = an (artificial) name made up by the programmer

• Each keyword has a special meaning in Java

• The (artificial) name is used to identify things defined in a java program

Page 64: Introduction to programming in the Java programming language

Summary (cont.)

• Statement = the smallest unit of execution in Java

• Method = contains multiple statements and identified by a method name which is an identifier

• Class = contains methods that serve a similar purpose

Each class is identified by a class name which is an identifier

• When a method is invoked, all statements contained in the method are executed

Page 65: Introduction to programming in the Java programming language

Summary (cont.)

• Syntax to define a public class:

public class CLASSNAME

{

(method definitions)

}

Page 66: Introduction to programming in the Java programming language

Summary (cont.)

• Syntax to define the main method in Java:

public static void main(String[] args)

{

(statements)

}