Introduction to the Java Programming Language

advertisement
Introduction to the Java
Programming Language
Writing a Java Program
1. Write the algorithm (yourself) in a programming language
2. Translate the program using a compiler into machine
instructions
3. Let the computer execute the translated program in machine
instructions
Compiling a Java Program
The translated machine code produced by the Java compiler javac
is called: Java bytecode
Java bytecodes are executed by a Java machine --- a computer that
executes machine instructions in Java bytecodes
Running a Java Program
• There does not exist a physical computer (machine) that can
execute Java byte codes
• Virtual machines do the same thing as a real machine, but
is not a physical machine
• A Java virtual machine is a computer program (java) that
can execute an algorithm written in Java bytecode
Computer Program
Computer program = an algorithm + (lots of) information
• 1. Computer instructions that tells
a computer precisely what to do to accomplish the job
• 2. Information (that is stored in data structure)to guide the
algorithm to do the job
Variable: storing the information
A variable = a identifiable memory
cell stores information that is necessary to solve a problem
Types of Variable (Data Types)
• Each kind of information uses its own information encoding
method
Example:
Gender information: 0 = male and 1 = female
Marital status information: 0 = single, 1 = married
Types of Variable (Data Types)
• Information is stored in variables, consequently, there
are different types of variables:
1. Numerical variables, that are used to store data such as: age, salary,
and so on.
2. Textual variables, that are used to store alpha-numerical data such
as text in books, news paper articles, webpages, and so on.
3. Boolean (logical) variables, that are used to store the logical
value TRUE and FALSE
One data type uses one (same) encoding method (to
represent the same kind of information)
Variable Definition: Creating Variable
• A variable is a storage location for information (data)
• A variable in a program is created by a variable definition
clause
• A variable definition clause will tell the computer to create
the variable that will be identified by that (unique)
name
• This is like telling the computer to:
get a new sheet of paper and
tag the new sheet of paper with the name of the variable
Statements: expressing the steps in an
algorithm
• Statement = an instruction written in a (High level)
Programming Language
• A statement tells a computer in very precise terms what
exactly it must do
• Example of statements:
Store the value 26 in the memory cell named "A"
Add the number A to the number B
If the number A ≥ 0 then do this otherwise do that
Block: grouping unit in Java
Block = a pair of "{" and "}" braces that groups components
in a Java program together
Organization of a Java Program
Example Java Program
Executing a computer program
• The execution will start at the statement at the starting
point of the execution.
• In Java, this will be the first statement in main().
Statement: the smallest unit of
execution in a Java program
A statement tells the computer to do something.
Example: System.out.println("Hello Class");
You cannot execute a portion of a statement.
Each type of statement follows a specific syntax.
Method: a container for (many)
statements that perform a task
• One statement can only perform a simple operation
• A complex task can be performed by many statements
• For convenience, you can put a number of statement into
a unit called a method
Defining a method (1)
• Define a method = constructing a method
• 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
Defining a method (2)
• 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)
Defining a method (3)
• Define once and run as many times as you want
When a method is executed, the statements in its
body are executed
Class: container for methods (1)
• One method is used to perform one complex task
• For each complex task, you will have to write one method to
perform the task.
• For organizational purpose, you can put a number of
methods into a unit called a class
Class: container for methods (2)
• 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
Other Issues (1)
Keyword = a reserved word in a programming language
Identifier = a name chosen by the programmer to identify
something defined inside a program
Other Issues (2)
• The first character of an identifier must be:
A letter (a, b, ..., z, A, B, ..., Z), or the underscore character
• The subsequent characters of an identifier must be:
A letter (a, b, ..., z, A, B, ..., Z), or the underscore character, or
a digit (0, 1, ..., 9)
• Identifiers/Java are case-sensitive !
Other Issues (3)
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
Other Issues (4)
Java programs are format-free:
Download