1
Web Programming with Java
Java Basics
Huynh Huu Viet
Email:
Department of Information Systems
2008 © Department of Information Systems - University of Information Technology
2
Outline
Primitive Data Types and Operations
Control Structures
Selection Statements
Loop Statements
Methods
Arrays
Q&A
2008 © Department of Information Systems - University of Information Technology
3
Identifiers
Identifiers:
Names for the things you will refer to in your
programs
Used for programming entities as variables,
constants, methods, classes, and packages
Rules for naming identifiers:
A sequence of characters that consists of letters,
digits, underscores (_), and dollar signs ($).
Cannot start with a digit.
Cannot be a reserved word.
Cannot be true, false, or null.
An identifier can be of any length.
2008 © Department of Information Systems - University of Information Technology
4
Variables & Constants
Variables: used to store data in a program
datatype variableName;
datatype variable1, variable2, , variablen;
Constants
Represents permanent data that never changes
final datatype CONSTANTNAME = VALUE;
What is benefits of using constants ???
• Don't have to repeatedly type the same value;
• Can be changed in a single location if necessary;
• Makes the program easy to read.
2008 © Department of Information Systems - University of Information Technology
5
Data Types and Operations (1)
2008 © Department of Information Systems - University of Information Technology
6
Data Types and Operations (2)
2008 © Department of Information Systems - University of Information Technology
7
Numeric Type Conversions
Conversions rules
If one of the operands is double, the
other is converted into double.
Otherwise, if one of the operands is float,
the other is converted into float.
Otherwise, if one of the operands is long,
the other is converted into long.
Otherwise, both operands are converted
into int.
2008 © Department of Information Systems - University of Information Technology
8
Operand Evaluation Order
The rule of evaluating an expression:
Rule 1: Evaluate whatever sub-expressions
you can possibly evaluate from left to right.
Rule 2: The operators are applied according
to their precedence
Rule 3: The associativity rule applies for two
operators next to each other with the same
precedence
You can use parentheses to force an
evaluation order
Does parentheses slow down the
execution of the expression???
2008 © Department of Information Systems - University of Information Technology
9
Operator Precedence (1)
2008 © Department of Information Systems - University of Information Technology
10
Operator Precedence (2)
Example:
int a = 0;
int x = a + (++a);
Îx=?
int a = 0;
int x = ++a + a;
Îx=?
2008 © Department of Information Systems - University of Information Technology
11
boolean Data Type & Operations
Java also provides
& and | operators (unconditional)
A little bit different from && and || operators
(conditional)
(x != 0) & (100 / x) ???
2008 © Department of Information Systems - University of Information Technology
12
Programming Errors (1)
Syntax errors (compilation errors)
Runtime Errors
public class ShowSyntaxErrors {
public static void main(String[] args) {
i = 30;
System.out.println(i + 4);
}
}
public class ShowRuntimeErrors {
public static void main(String[] args)
{
int i = 1 / 0;
}
}
2008 © Department of Information Systems - University of Information Technology
13
Programming Errors (2)
Logic Errors
public class ShowLogicErrors {
public static void main(String[] args) {
// Add number1 to number2
int number1 = 3;
int number2 = 3;
number2 += number1 + number2;
System.out.println("number2 is " + number2);
}
}
2008 © Department of Information Systems - University of Information Technology
14
Control Structures
Selection Statements
if Statements
switch Statements
Conditional Expressions
Loops
while Loop
do-while Loop
for Loop
break and continue
2008 © Department of Information Systems - University of Information Technology
15
Outline
Primitive Data Types and Operations
Control Structures
Selection Statements
Loop Statements
Methods
Arrays
Q&A
2008 © Department of Information Systems - University of Information Technology
16
if Statements
Simple if Statements
if else Statements
if (booleanExpression) {
statement(s);
}
if (booleanExpression) {
stat(s)-for-the-true-case;
}
else {
stat(s)-for-the-false-case;
}
2008 © Department of Information Systems - University of Information Technology
17
if Statements - notes
Common mistake
else matching
2008 © Department of Information Systems - University of Information Technology
18
switch Statements
switch (switch-expression) {
case value1: statement(s)1;
break;
case value2: statement(s)2;
break;
…
case valueN: statement(s)N;
break;
default: statement(s)-for-default;
}
switch-expression must yield a value of char, byte, short, or int
The value1, . . ., and valueN must have the same data type as the value
of the switch-expression and be constant expressions
2008 © Department of Information Systems - University of Information Technology
19
Conditional Expressions
Syntax:
• booleanExpression ? expression1 : expression2;
Example
Why we need this operator???
y = (x > 0) ? 1 : -1;
if (x > 0) y = 1
else y = -1;
2008 © Department of Information Systems - University of Information Technology
20
Control Structures
Selection Statements
if Statements
switch Statements
Conditional Expressions
Loops
while Loop
do-while Loop
for Loop
break and continue
2008 © Department of Information Systems - University of Information Technology
21
while Loop
while (loop-continuation-condition) {
// Loop body
Statement(s);
}
2008 © Department of Information Systems - University of Information Technology
22
do-while Loop
do {
// Loop body;
Statement(s);
} while (loop-continuation-condition);
2008 © Department of Information Systems - University of Information Technology
23
for Loop
for (initial-action; loop-continuation-condition; action-after-each-iteration) {
// Loop body;
Statement(s);
}
2008 © Department of Information Systems - University of Information Technology
24
Loops
Which Loop to Use?
Common errors:
2008 © Department of Information Systems - University of Information Technology
25
break and continue
break
Immediately ends the innermost loop that
contains it.
Generally used with an if statement.
continue
Only ends the current iteration.
Generally used with an if statement.