2.DataTypes ControlStatements Operators
2.DataTypes ControlStatements Operators
Data Types
Built-in Data Types
Simple form
<datatype> <identifier>;
Example
int total;
Optional initialization at declaration
<data type> <identifier> = <init value>;
Example
int total = 0;
Examples
int counter;
int numStudents = 583;
float gpa;
double batAvg = .406;
char gender;
char gender = ‘f’;
boolean isSafe;
boolean isEmpty = true;
String personName;
String streetName = “North Avenue”;
Primitive Type Facts
Type Size Min Max Default
boolean 1bit false* true* false
char 2 '\u0000' (null)
byte 1 -128 127 (byte) 0
short 2 -32,768 32,767 (short) 0
int 4 -2,147,483,648 2,147,483,647 0
long 8 -9,223,372,036,854,775,808 9,223,372,036,854,775,807 0L
float 4 Approx ±3.4E+38 with 7 significant digits 0.0F
double 8 Approx ±1.7E+308 with 15 significant digits 0.0D
void
* Not truly min and max. Note: Size is in Bytes
Conditionals & Iteration
Decision Statements
if (condition)
statement1;
if (condition) {
statements;
} // if
Decision Statements
For safety and good programming practice, always include a 'default' case.
How many days?
if (month == 4 || month == 6 ||
month == 9 || month == 11)
numdays = 30;
else if (month = 2)
{
numdays = 28;
if (leap)
numdays = 29;
}
else
numdays = 31;
Switch
switch (month)
{
case 4:
case 6:
case 9:
case 11:
numdays = 30;
break;
case 2:
numdays = 28;
if(leap)
numdays = 29;
break;
default: /* Good idea? */
numdays = 31;
}
Java Iteration Constructs: "While Loops"
Java example:
Note: Check is made before entering loop thus it is possible that loop may not
execute
Java Iteration Constructs: "Do While Loops"
Java example:
do
{
statement 1;
...
statement N;
} while (condition);
Java syntax:
for (<initialization>; <continue if>;<increment>)
Java example:
int count;
for (count = 1; count <= 50; count ++) {
<loop-body>
}
Secret!
A for Loop can always be converted to a while loop
Unary Operators
Binary Operators
Ternary Operators
Arithmetic Operators
Equal to (==)
Bitwise Operators
Short-circuit operators
Bitwise Logical Operators
AND (&)
OR (|)
XOR (^)
=
+=
-=
*=
/=
%=
&=
|=
^=
Advanced Operators
new
instanceof
Equality
An array lets you associate one name with a fixed (but possibly
large) number of values
0 1 2 3 4 5 6 7 8 9
myArray 12 43 6 83 14 -57 109 12 0 6
Indexing into arrays
To reference a single array element, use
array-name [ index ]
Indexed elements can be used just like simple variables
You can access their values
You can modify their values
An array index is sometimes called a subscript
0 1 2 3 4 5 6 7 8 9
myArray 12 43 6 83 14 -57 109 12 0 6
0 1 2 3 4 5 6 7 8 9
myArray 12 43 6 83 14 -57 109 12 0 6
Examples:
x = myArray[1]; // sets x to 43
myArray[4] = 99; // replaces 14 with 99
m = 5;
y = myArray[m]; // sets y to -57
z = myArray[myArray[9]];// sets z to 109
Array values
An array may hold any type of value
Another syntax:
int [ ] a, b, c, d; // notice position of brackets
a, b, c and d are int arrays
When the brackets come before the first variable, they apply to all
variables in the list
Different ways of Creating the array
int myarray [ ]= {1,2,3}; Only Values
Here length
indicates the size of an array
Arrays of objects