The Basic Parts of Java: Data Types
The Basic Parts of Java: Data Types
Data Types
Primitive Composite
Control structures
Natural numbers
Floating points
Also called built-in types All numbers are signed Have fixed size on all platforms
OOP: Basic Parts of Java 2
Declarations
A declaration is the introduction of a new name in a program. All variables must declared in advance. The is no dedicated variable declaration part of a Java
program. General forms type variableName1, variableName2, variableName3; type variableName1 = value1, variableName2 = value2, variableName3 = value3;
Elements are all of type boolean The index type is always integer Index limits from 0 to MAXSIZE-1
Bound-check at run-time. Arrays are first class objects (not pointers like in C) There are no struct/record or enumaration types in Java.
structs/records are replaced by classes Type safe enumeration will be added in Java 1.5 (2005)
Lexical Rules
A name in Java consists of [0-9][a-z][A-Z][_$]
cannot start with a number national language letters can be used, e.g., , , and . no maximum length thisIsAVeryLongVariableName
All resevered word in Java are lower case, e.g., if. Case matters myVariable, myvariable
Naming Conventions
Words run together, no underscore Intermediate words capitalized.
Name of method or variable: first letter lower case Name of constants: all upper case, separated by underscore,
e.g., java.math.E and java.math.PI Part of JavaSoft programming standard Java's Naming convention (link)
OOP: Basic Parts of Java
Commands in Java
Very similar to the C programming language Assignment
variable = <expression>
Method call
Control Structures
Block Statement
Several statements can be grouped together into a block
statement. A block is delimited by braces { <statement list> } Variables can be declared in a block. A block statement can be used wherever a statement is called for in the Java syntax.
For example, in an if-else statement, the if portion, or the else portion, or both, could be block statements
Arithmetic operators
Relational Operators
Equality == (two '=' symbols) Inequality != Greater-than >, >= Less-than <, <=
i i i i
Bitwise operators
5 5 8 2 2
= = = = =
5 255 11 64 4
128 = 0 2 = 10 31 = 15 3 = 56 2 = 1
11
can be combined with other binary operators +=, -=, *=, /=, %=, >>=, <<=, &=, ^=, !=
Conditional Operator
Precendence rules similar to C for Java operators Associtivity rules similar to C for Java operators
12
Methods in Java
All procedures and functions in Java are methods on classes. The difference between a procedure and a function is the
return type
Implicit: When the last command is executed (for procedures). Explicit: By using the return command.
13
double set(int i), double set(int i, int j) Cannot overload on return value!
14
}
15
Parameter Mechanism
All parameters in Java are pass-by-value.
The value of the actual parameter is copied to the formal parameter. public static void main (String[] args) Will be supported in Java 1.5
Passing Objects
Via a formal parameter it is possible to modify the object directly. The reference to the object cannot be modified.
18
String calc(int num1, int num2, String message){ int sum = num1 + num2; String result = message + sum return result; }
19
Are shared between all the instances of a class public static int i; public static ArrayList = new ArrayList(); public static final char DOT = '.';
For methods
Can be access without using an object public static void main(String args[]){} public static int getCount(){}
21
Control Structures
Basic parts of the programs Borrowed from C programming language Branching
if-else switch (case statement) break, continue, goto while-do do-while for
Looping
23
The if Statement
The if statement has the following syntax:
The condition must be a boolean expression. It must evaluate to either true or false. if (condition){ statement; }
If the condition is true, the statement is executed. If it is false, the statement is skipped.
24
Logic of an if Statement
// example 1 if (weight < 20000) doStuffMethod(); // same thing if (weight < 20000){ doStuffMethod(); } // example 2 if (weight < 20000) doStuffMethod(); doMoreStuff(); false // NOT the same thing if (weight < 20000){ doStuffMethod(); doMoreStuff(); } // What is ugly here? boolean b = getChoice(); if (b == true){ // do stuff }
condition
true statement
next statement
25
condition is false, statement2 is executed One or the other will be executed, but not both An else clause is matched to the last unmatched if (no matter what the indentation implies)
26
true statement1
next statement
27
enumerables can appear in any order enumerables do not need to be consecutive several case constant may select the same substatement enumerables must be distinct enumerable cannot use case 1..9
28
expression
value1
value 2
value3
statement-list1
statement-list2
statement-list3
next statement
other values
30
31
32
switch(salary/20000) { case 0: System.out.println("poor"); break; case 1: System.out.println("not so poor"); break; case 2: System.out.println("rich"); break; case 3: System.out.println("really rich"); break; default: System.out.println("Hi, Bill Gates"); }
OOP: Basic Parts of Java
33
Therefore, the body of a while loop will execute zero or more times
34
condition
next statement
35
This is a common type of logical error. You should always double check to ensure that your loops will terminate normally. 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
36
The do Statement
The do statement has the following syntax
Uses both the do and while reserved words do { statement; } while (condition)
The statement is executed once initially, then the condition is evaluated. The statement is executed until the condition becomes false.
38
for (initialization ; condition ; increment) statement; The statement is executed until the condition becomes false The increment portion is executed at the end of each iteration
39
initialization
next statement
40
41
Branching
break
Can be used in any control structure Exits from the innermost enclosing loop break <label> Cycles a loop, e.g., jump to the condition checking Only from methods Jumps out of the current method an returns to where the method was called from return <expression> Reserved word, not implemented
continue
return
goto
42
next statement
OOP: Basic Parts of Java 43
next statement
OOP: Basic Parts of Java 44
false
next statement
OOP: Basic Parts of Java
continue Example
public void skipPrinting(int x, int y){ for(int num = 1; num <= 100; num++){ if((num % x) == 0){ continue; } if((num % y) == 0){ continue; } // This num is not divisible by x or y System.out.println(num); } }
46
47
Summary
Set of built-in data types Array are supported
No support records or enumarated type Procedure Functions Always by-value in Java Actual and formal parameters. if, if-else, if-else-if-else, if-else-if-else-if-else, etc. while-do, do-while for switch
48
Methods
Argument passing
Control structures