Documentos de Académico
Documentos de Profesional
Documentos de Cultura
The Java interpreter starts up a new Virtual Machine The VM executes the defined class by running its main() method
2
public class HelloWorld { public static void main(String args[]) { System.out.println("Hello World"); } }
HelloWorld.java
source code
Run
C:\ Hello World
HelloWorld.class
output
Flow Control
Classes and Objects
Arrays
Readability and Style
5
short
int long float double char boolean
Short integer
Integer Long integer (real numbers)
Single-precision floating point
(other types)
A single character A boolean value (true or false) 16-bit Unicode character true or false
6
NOTE: A variable of a primitive type always holds a value of that exact type
1.3 Operators
Assignment: = += -= *= Numeric: Relational: Boolean: + - * / % ++ -== != < && || ! > <= >=
1.3 Operators
Assignment Statement
Assignment operator is =, E. g. i = 7;
Expressions
x = 3 + y;
Format
if( Boolean_Statement ) statements if true else statements if false
Example
if( value >= 0 ) goCompute(value); else { System.out.println(Negative!); }
10
It you leave out break, the switch will evaluate all cases.
NOTE: The case statements fall through. A break statement prevents this from happening
11
Note that condition is only evaluated at the top The loop is executed as long as the condition is satisfied Example
i=1; while( i<10 ) { System.out.println(i); i=i+1; }
13
Format
while(true) { statements }
14
Format
do {
15
Format
for(initial; condition; adjustment) { statements Must evaluate to a } boolean
System.out.println(Loop);
16
Which numbers are printed? Continue interrupts the execution of the current iteration within the loop but maintains the loop execution
for(int i = 0; i < 10; i++) {
Classes
Methods
NOTE: All Java statements must appear within methods. All methods are defined within classes.
18
19
y P(x, y)
But this doesnt create the object! You have to use new to create a new object:
Point p = new Point(3.1,2.4);
22
Invoke a method:
objectname.methodname()
Example
Point p = new Point(3.0,2.0); double dist = p.distanceFromOrigin(); p.x = 7.0;
23
1.6 Arrays
Arrays are supported as a kind of reference type. Declaring an array:
double marks[];
Array elements:
0 1 2 3 4 1.2 6.1 0.2
-1.1
1.9
marks[2]
1.6 Arrays
Notes on Arrays
Arrays index starts at position 0
Arrays have a .length (a very useful attribute) An array of strings is declared and created as follows:
String name[] = new String[10]; name[0] = rob;
1.6 Arrays
Example Code
int[] values = {1,2,3,4,5}; int total = 0; ... for (int i=0; i<values.length; i++) { total = total + values[i]; } System.out.println("total is " +total);
26
Use meaningful names, capitalise the first letter in every word method and variable identifiers start with lowercase letter
setName, getAge, numberOfStudents,
27
Indent statement blocks. (tab or 2-4 spaces) Use comments that add meaning to the program and explain ambiguous or confusing constructs.
28
- Example
/* Block comments are good for explaining the purpose of a method (statement block) and complex code sections. */ MORE CODE HERE } catch (Exception e) { // DEAL WITH THE EXCEPTION BLOCK HERE } } else { // STATEMENTS CODE HERE } } }
29
30
31
32
Describe your solution in broad terms before going into detail Break the problem down into a sequence of simple operations
E.g. Input number, Division, Addition, Print result
33