Lec 01 02 Introduction Review
Lec 01 02 Introduction Review
Programming in Java
Lecture 01 : Introduction and Review
Overview
´ It was introduced in 1995 and it's popularity has grown quickly since
5 Java Program Structure
class body
}
8 A Basic Java Program
´ Printing on console;
´ System.out.print("hello"); // print a String
´ System.out.print(16); // print an integer
´ System.out.print(5.5 * .2); // print a floating-point number
´ String Concatenation
int x = 20, y = 10;
System.out.println("x: " + x + "\ny: " + y);
´ Output
x: 20
y: 10
12 Comments
Program Development
18 Program Development
´ translating the program into a form that the computer can execute
´ Software tools can be used to help with all parts of this process
19 Development Environments
´ The compiler will find syntax errors and other basic problems
(compile-time errors)
´ If compile-time errors exist, an executable version of the program is
not created
Edit and
save program
errors
errors
Compile program
´ An object has:
´ state - descriptive characteristics (attributes)
´ behaviors - what it can do (or what can be done to it)
´ The state of a bank account includes its account number and its
current balance
A class An object
(the concept) (the realization)
´ Examples:
"This is a string literal."
"123 Main Street"
"X"
´ Every character string is an object in Java, defined by the String class
´ Every string literal represents a String object
31 The println Method
method
information provided to the method
name
(parameters)
32 The print Method
int total;
int count, temp, result;
int sum = 0;
int base = 32, max = 149;
total = 55;
The expression on the right is evaluated and the result is stored in the
variable on the left
´ The compiler will issue an error if you try to change the value of
a constant
´ Example declarations:
char topGrade = 'A';
char terminator = ';', separator = ' ';
´ The ASCII character set is older and smaller than Unicode, but is still quite
popular
´ The ASCII characters are a subset of the Unicode character set, including:
uppercase letters A, B, C, …
lowercase letters a, b, c, …
punctuation period, semi-colon, …
digits 0, 1, 2, …
special symbols &, |, \, …
control characters carriage return, tab, ...
49 Boolean
´ The reserved words true and false are the only valid values
for a boolean type
Addition +
Subtraction -
Multiplication *
Division /
Remainder %
´ If both operands to the division operator (/) are integers, the result
is an integer (the fractional part is discarded)
14 / 3 equals 4
8 / 12 equals 0
The remainder operator (%) returns the remainder after dividing the
second operand into the first
14 % 3 equals 2
8 % 12 equals 8
Operator Precedence
53
a + b + c + d + e a + b * c - d / e
1 2 3 4 3 1 4 2
a / (b + c) - d % e
2 1 4 3
a / (b * (c + (d - e)))
4 3 2 1
55 Expression Trees
+
a + (b – c) / d
a /
- d
b c
56 Assignment Revisited
count = count + 1;
count++
´ prefix form:
++count
´ When used as part of a larger expression, the two forms can have different
effects
is equivalent to
+= x += y x = x + y
-= x -= y x = x - y
*= x *= y x = x * y
/= x /= y x = x / y
%= x %= y x = x % y
62 Assignment Operators
´ Therefore
is equivalent to
money = dollars
´ Only widening conversions can happen via assignment
if (condition){
// TRUE block
}
else{
//FALSE block
}
71 Loops in java
´ for loop
for(int i=0; i<10; i++){
//body of the loop
}
´ while loop
while( condition){
//body of the loop
}
72 Acknowledgment