1 Introduction
1 Introduction
Introduction to Programming
Chapter 1 - Getting Started
Dr. Aiman Hanna
Department of Computer Science & Software Engineering
Concordia University, Montreal, Canada
These slides has been extracted, modified and updated from original slides of Absolute Java 3 rd Edition by Savitch;
which has originally been prepared by Rose Williams of Binghamton University. Absolute Java is published by
Pearson Education / Addison-Wesley.
1-3
Origins of the Java Language
Significance of Java translation process
Writing a compiler (translation program) for each type of
appliance processor would have been very costly
The same rule applies to writing compilers: different
compilers are needed for different systems
(processor/Operating System)
Example:
1-4
Origins of the Java Language
Instead, Java designers developed intermediate language that
is the same for all types of processors: Java byte-code
1-5
Origins of the Java Language
The byte-code can be viewed as suitable to every
machine since it is easily possible to write a small
program (interpreter) to translate byte-code into the
proper machine code for each processor
1-6
Objects and Methods
Java is an object-oriented programming (OOP)
language
Programming methodology that views a program as
consisting of objects that interact with one another by
means of actions (called methods; sometimes referred
to as functions)
All programming constructs in Java, including
methods, are part of a class
Objects of the same kind are said to have the same
type or be in the same class
1-7
Java Application Programs
There are two types of Java programs: applications and
applets
1-8
Applets
A Java applet (little Java application) is a Java program that
is meant to be run from a Web browser
class body
1-10
Java Program Structure
// comments about the class
public class MyProgram
{
1-11
A Sample Java Application
Program
Greetings.java (MS-Word file)
1-12
System.out.println
Java programs work by having things called
objects perform actions
System.out: an object used for sending output
to the screen
The actions performed by an object are called
methods
println: the method or action that the
System.out object performs
1-13
System.out.println
Invoking or calling a method: When an object performs
an action using a method
Examples:
int numOfDoors;
double price;
1-15
Using = and +
In Java, the equal sign (=) is used as the
assignment operator
The variable on the left side of the assignment
operator is assigned the value of the expression on the
right side of the assignment operator
numOfDoors = 4;
1-16
Using = and +
In Java, the plus sign (+) can be used to denote
addition or concatenation
Examples:
int total;
total = 2 + 5;
Only run the class that contains the main method (the
system will automatically load and run the other classes, if
any)
Follow the run command by the name of the class only (no
.java or .class extension)
java FirstProgram
1-19
Syntax and Semantics
Syntax: The arrangement of words and
punctuations that are legal in a language, the
grammar rules of a language
1-20
Tip: Error Messages
Bug: A mistake in a program
The process of eliminating bugs is called debugging
1-21
Tip: Error Messages
Run-time error: An error that is not detected until a
program is run
The compiler cannot detect these errors: an error message is
not generated after compilation, but after execution
1-22
Identifiers
Identifier: The name of a variable or other item (class,
method, object, etc.) defined in a program
1-23
Identifiers
Which of the following is a valid identifier?
IntRate (A: Valid, B: Not Valid)
Five_speed (A: Valid, B: Not Valid)
5_speed (A: Valid, B: Not Valid)
_5 speed (A: Valid, B: Not Valid)
MercedesSL500 (A: Valid, B: Not Valid)
Time.and.space (A: Valid, B: Not Valid)
___3 (A: Valid, B: Not Valid)
Less<5 (A: Valid, B: Not Valid)
1-24
Identifiers
Keywords and Reserved words: Identifiers that have a
predefined meaning in Java
Cannot be used to name anything else
public class void static
1-26
Variable Declarations
Every variable in a Java program must be declared before it is used
A variable declaration tells the compiler what kind of data (type) will be
stored in the variable
Variables are typically declared just before they are used or at the start of
a block (indicated by an opening brace { )
1-28
Assignment Statements With Primitive Types
1-29
Assignment Statements With Primitive Types
1-33
Shorthand Assignment Statements
1-35
Assignment Compatibility
More generally, a value of any type in the following list can be
assigned to a variable of any type that appears to the right of it
byteshortintlongfloatdouble
char
Note that as your move down the list from left to right, the range of
allowed values for the types becomes larger
1-37
Constants
Constants of type char are expressed by placing a
single character in single quotes (e.g., 'Z')
1-38
Naming Constants
Instead of using "anonymous" numbers in a program, always
declare them as named constants, and use their name instead
1-41
Increment & Decrement Operators
The increment operator (++) adds one to the value
of a variable
If n is equal to 2, then n++ or ++n will change the
value of n to 3
1-43
Parentheses and Precedence
Rules
An expression can be fully parenthesized in order to
specify exactly what sub-expressions are combined with
each operator
1-44
Precedence Rules
1-45
Precedence and Associativity
Rules
When the order of two adjacent operations must be
determined, the operation of higher precedence (and its
apparent arguments) is grouped before the operation of
lower precedence
base + rate * hours is evaluated as
base + (rate * hours)
1-46
Precedence and Associativity
Rules
Unary operators of equal precedence are grouped right-to-left
+-+rate is evaluated as +(-(+rate))
1-47
Pitfall: Round-Off Errors in Floating-Point
Numbers
Floating point numbers are only approximate quantities
Mathematically, the floating-point number 1.0/3.0 is equal to
0.3333333 . . .
1-48
Integer and Floating-Point
Division
When one or both operands are a floating-point type, division
results in a floating-point type
15.0/2 evaluates to 7.5
1-49
The % (Modula) Operator
The % operator is used with operands of type int to
recover the information lost after performing integer
division
15/2 evaluates to the quotient 7
15%2 evaluates to the remainder 1
16%4 evaluates to the remainder 0
1-50
Type Casting
A type cast takes a value of one type and produces a value of
another type with an "equivalent" value
Note also that the type and value of the variable to be cast does not
change
1-52
Escape Sequences
A backslash (\) immediately preceding a
character (i.e., without any space) denotes an
escape sequence or an escape character
1-54
Comments
A line comment begins with the symbols //, and causes
the compiler to ignore the remainder of the line
This type of comment is used for the code writer or for a
programmer who modifies the code
1-55
Comments & Named Constant
1-56
The Class String
There is no primitive type for strings in Java
1-57
Concatenation of Strings
Concatenation: Using the + operator on two strings in order to
connect them to form one longer string
If greeting is equal to "Hello ", and javaClass is equal to
"class", then greeting + javaClass is equal to "Hello
class"
1-58
Classes, Objects, and Methods
A class is the name for a type whose values are objects
1-59
Classes, Objects, and Methods
Invoking or calling a method: a method is called into
action by writing the name of the calling object,
followed by a dot, followed by the method name,
followed by parentheses
1-60
String Methods
The String class contains many useful methods for string-
processing applications
1-61
Some Methods in the Class String
(Part 1 of 8)
1-62
Some Methods in the Class String
(Part 2 of 8)
1-63
Some Methods in the Class String
(Part 3 of 8)
1-64
Some Methods in the Class String
(Part 4 of 8)
1-65
Some Methods in the Class String
(Part 5 of 8)
1-66
Some Methods in the Class String
(Part 6 of 8)
1-67
Some Methods in the Class String
(Part 7 of 8)
1-68
Some Methods in the Class String
(Part 8 of 8)
1-70
String Processing
A String object in Java is considered to be immutable, i.e., the
characters it contains cannot be changed
1-71
Character Sets
ASCII: A character set used by many programming languages
that contains all the characters normally used on an English-
language keyboard, plus a few special characters
Each character is represented by a particular number
1-72
Program Documentation
Java comes with a program called javadoc that
will automatically extract documentation from
block comments in the classes you define
As long as their opening has an extra asterisk (/**)
1-73