The Basic Parts of Java: Data Types
The Basic Parts of Java: Data Types
• Data Types
Primitive
int, float, double, etc.
Composite
array (will also be covered in the lecture on Collections)
• Lexical Rules
• Expressions and operators
• Methods
Parameter list
Argument parsing
• Control Structures
• Branching
• Examples in http://www.cs.auc.dk/~torp/Teaching/E03/OOP/Examples/
OOP: Basic Parts of Java 1
Primitive Data Types
• Boolean {true, false}
• byte 8-bit
• short 16-bit
Natural numbers
• int 32-bit
• long 64-bit
• float 32-bit
Floating points
• double 64-bit
• char 16-bit Uni-code
// or similar
double v = 3.14e-23,
w = 5.5;
// A well-known constant
final static double PI = 3.14;
OOP: Basic Parts of Java 4
Array: A Composite Data Type
• An array is an indexed sequence of values of the same type.
• Arrays are defined as classes in Java.
• Example:
boolean[] boolTable = new boolean[MAXSIZE]
• Bound-check at run-time.
• Arrays are first class objects (not pointers like in C)
• Method call
Various parameter mechanisms
• Control Structures
sequential
selective
iterative
• Arithmetic operators
Additive +, -, ++, -- i = i + 1, i++, --i
Multicative *, /, % (mod operator) 9%2 = 1, 7%4 = 3
• Relational Operators
Equality == (two '=' symbols) i = i, i == i
Inequality != i != j
Greater-than >, >= i > j, i >= j
Less-than <, <= i < j, i <= j
OOP: Basic Parts of Java 10
Expresions and Operators, cont.
• Logical operators
and && bool1 && bool2
or || bool1 || bool2 || bool3
not ! !(bool1)
All are short-circuit
• Bitwise operators
and & 255 & 5 = 5 15 & 128 = 0
or | 255 | 5 = 255 8 & 2 = 10
xor ^ 3 ^ 8 = 11 16 ^ 31 = 15
shift left << 16 << 2 = 64 7 << 3 = 56
shift right >> 16 >> 2 = 4 7 >> 2 = 1
• Conditional Operator
Ternary operator
?:
int max = n > m ? n : m;
• Returning
Implicit: When the last command is executed (for procedures).
Explicit: By using the return command.
Good design: only to have one return command each method
• Passing Objects
Objects are accessed via a reference.
References are pass-by-value.
The refernce is copied
The object itself is not copied
Via a formal parameter it is possible to modify the object "directly".
The reference to the object can however not be modified.
• For method
Can be access without using an object
public static void main(String args[]){}
public static int getCount(){}
if (condition){
statement;
}
// same thing
if (weight < 20000){
condition doStuffMethod();
}
true
false // example 2
statement if (weight < 20000)
doStuffMethod();
doMoreStuff();
next statement // NOT the same thing
if (weight < 20000){
doStuffMethod();
doMoreStuff();
}
OOP: Basic Parts of Java 23
The if-else Statement
• An else clause can be added to an if statement to make it an if-
else statement
if (condition){
statement1;
}
else{
statement2;
}
statement1 statement2
next statement
If expression
matches value2, ● enumerables can appear in any order
● enumerables do not need to be consecutive
control jumps
● several case constant may select the same substatement
to here
● enumerables must be distinct
switch (expression){
case value1 :
expression statement-list1
break;
case value2 :
statement-list2
value1 value 2 value3 break;
statement-list1 statement-list2 statement-list3 case value3 :
statement-list3
break;
}
// next statement
other values
next statement
switch(salary/20000) {
case 0:
System.out.println("pour");
break;
case 1:
System.out.println("not so pour");
break;
case 2:
System.out.println("rich");
break;
case 3:
System.out.println("really rich");
break;
default:
System.out.println("Hi, Bill Gates");
}
while (condition)
while is a
statement;
reserved word
// Count from 1 to 10
int n = 10;
int i = 1;
while (i <= n) {
condition
System.out.println(i);
i = i + 1;
}
true // next statement
false
statement
next statement
// what is wrong here?
int i = 0;
while(i < 10){
System.out.println(i);
// do stuff
}
OOP: Basic Parts of Java 33
The while Statement, cont.
• The body of a while loop must eventually make the condition
false.
• If not, it is an infinite loop, which will execute until the user
interrupts the program.
This is a common type of logical error.
You should always double check to ensure that your loops will
terminate normally.
• The while statement can be nested
That is, the body of a while could contain another loop
Each time through the outer while, the inner while will go through its
entire set of iterations
// Count from 1 to 10
int n = 10;
statement int i = 1;
do {
true System.out.println(i)
i = i + 1;
condition } while (i <= 10);
// next statement
false
next statement
condition
while (condition1) {
condition1 stmt1;
if (condition2)
true break;
stmt2;
stmt1 }
// next statement
false
condition2
false
true
stmt2
next statement
OOP: Basic Parts of Java 41
Logic of the break Statement, cont
while (condition1) {
condition1 stmt1;
while (true){
break;
true }
stmt2;
stmt1 }
// next statement
false
condition2
break
stmt2
next statement
OOP: Basic Parts of Java 42
Logic of the continue Statement
while (condition1) {
stmt1;
if (condition2)
condition1
continue;
stmt2;
true }
// next statement
true stmt1
false
condition2
false
// what is wrong here?
stmt2 while (condition){
// many more statements
continue;
next statement }
OOP: Basic Parts of Java 43
continue Example