Professional Documents
Culture Documents
CS 241 Lec 2
CS 241 Lec 2
CS 241: OBJECT
ORIENTED
PROGRAMMING
Dr. Ibrahim Eldesoky
Ibrahim_desoky@hotmail.com
2
Identifiers
• We make up words for class names, method names, and
variables.
• These words are called identifiers.
• For example,
• Example1
is identifier in our simple program.
8
Identifiers (cont.)
• An identifier can be composed of any combination of
letters, digits, the under score character, and the dollar
sign; but it cannot start with a digit.
• We can use both upper case letters and lower case letters
in identifiers. But Java is case sensitive. Identifiers Val,
val and VAL are all different variables.
• Some Legal Identifiers: x val count_flag Test1 $amount
val1 stockItem
Identifiers (cont.)
• Although we can choose any legal identifier to be
used, but it is nice to follow certain style
guidelines when make up an identifier.
• Choose meaningful names (not too long, not too short,
descriptive words)
• First character of a class name should be an uppercase
letter.
• First character of a method name and a variable should
be a lower case letter.
10
Reserved Words
• Reserved words are identifiers that have a
special meaning in a programming language.
• For example,
• public, void, class, static are reserved
words in our simple programs.
• In Java, all reserved words are lower case
identifiers (Of course we can use just lower case
letters for our own identifiers too)
• We cannot use the reserved words as our own
identifiers (i.e. we cannot use them as variables,
class names, and method names).
11
/*
HelloWorld application program
*/
public class HelloWorld // Class header
{ // Start class body
public static void main(String argv[]) //main method
{
System.out.println(“HelloWorld!”);
} // end of main
} // end HelloWorld
/*
HelloWorld application program
*/
public class HelloWorld // Class header
{ // Start class body
public static void main(String argv[]) //main method
{
System.out.println(“HelloWorld!”);
} // end of main
} // end HelloWorld
/*
HelloWorld application program
*/
public class HelloWorld // Class header
{ // Start class body
public static void main(String argv[]) //main method
{
System.out.println (“HelloWorld!”);
} // end of main
} // end HelloWorld
Circled words that are not in the language, but were used by
other programmers to make the library
15
import package.*
we will be able to access all classes in that package.
example: import java.awt.*
19
Structure of a Method
Executable Statements
• All statements which are executed during the
execution of a program
-- assignment statement
-- method call statement
-- conditional statement
-- loop statement
Examples:
System.out.println (“Hello there, World!”);
f(1,2);
x = y+2;
if (x<1) x=x+1;
else x=x-1;
22
System.out.println(3+4+"=
System.out.println ("Answer = " + 7); 7");
Output: Output:
Answer = 7 7=7
28
Variables
• A variable is a name for a location in memory
• A variable must be declared, specifying the variable's
name and the type of information that will be held in it
int total;
Variables
• A variable can be given an initial value in the declaration
• int sum = 0;
• int base = 32, max = 149;
• When a variable is referenced in a program, its current value is
used
30
Assignment
• An assignment statement changes the value of
a variable total = 5;
Constants
Constants (cont.)
Data Types
Arithmetic Expressions
• Arithmetic Operators:
+ addition
- subtraction
* multiplication
/ division
% mod operator (remainder)
++,-- increment, decrement operators (they can be
postfix or prefix)
+, - unary operators
37
boolean
• A boolean value represents a true or false
condition.
int i, j;
boolean x, y;
x = true;
y = (i < j);
38
Characters
Characters (cont.)
• ASCII character set is a subset of the Unicode
character set. It uses only 8 bits (256
characters). In fact, the first 256 characters of
the Unicode character set are ASCII
characters.
• 32 space
• 48-57 0 to 9
• 65-90 A to Z
• 97-122 a to z
• Character literals:
• ‘a’ ‘A’ ‘1’ ‘0’ ‘+’
• Note that ‘1’ and 1 are different literals (character
and integer)
40
Escape Sequences
• What if we wanted to print a double quote character?
• The following line would confuse the compiler because
it would interpret the second quote as the end of the
string
\b backspace
\t tab
\n newline
\r carriage return
\" double quote
\' single quote
\\ backslash
42
Literals
• Literals are explicit values used in a program.
• Certain data types can have literals.
• String literal – “Hello there,World!”
• int literals -- 12 3 77
• double literals – 12.1 3.45
• char literals – ‘a’ ‘1’
• boolean literals -- true false
43
Statements
• Conditional Statements
if statement
switch
• Loops
while
do-while
for
45
if Statement
if-then structure: condition
if-statement Examples
if (y != 0)
x = x / y;
else
System.out.println(“y is zero”);
if (x >= 0)
System.out.println (“positive”);
else
System.out.println (“negative”);
47
Compound Statements
if (discountRate != 0) {
discount = price * discountRate;
price = price – discount;
}
if (x>y) {
temp = x ;
x = y;
y = temp;
}
if (x != y) {
System.out.println (“x and y have different values”);
System.out.println (“x: “+x+” y: “+y);
}
else
System.out.println (“x and y have same value: “ + x);
49
Boolean Expressions
• Boolean Operators
&& logical AND
|| logical OR
! negation (NOT)
^ exclusive or (XOR)
E1 E2 E1 && E2 E1 || E2 E1 ^ E2
false false false false false
false true false true true
true false false true true
true true true true false
E !E
false true
true false
54
Operator Precedence
Precedence Operator Operation Associates
Level
4 + - addition, subtraction L to R
5 < <= > >= less than, less than equal, greater than, greater than L to R
equal
7 ^ xor L to R
9 || logical or L to R
10 ?: conditional operator R to L
Boolean Assignment:
• we may declare Boolean variables and save Boolean values in
those variables.
boolean flag;
flag = (x>y);
56
Nested if-statements
• If-statements can also be used to implement decisions
involving more than two alternatives.
• A nested if-statement is an if-statement whose true-
statement and/or false-statement are also if-
statements.
if (x < 10)
System.out.println (“x has one digit”);
else if (x < 100)
System.out.println (“x has two digits”);
else
System.out.println (“x has more than two
digits”);
58