Head First Java Chapter 1
Tom Henricksen
The Way Java Works
● Source
● Compiler
● Output(code)
● Virtual Machine
Code Structure in Java
● Source
● Class
● Method
Eclipse
Eclipse
Eclipse
Eclipse
Eclipse
Eclipse
HelloWorld
public class HelloWorld {
public static void main (String[] args) {
System.out.println(“Hello World”);
}
}
Run your application
Run your application
Statements
//declaration
String name;
//assignment
name = “Tom”;
//method call
double d = Math.Random();
Loops
while (score > 10) {
score = score + 1;
}
For (int v = 0; v < 5; v++) {
System.out.println(v);
}
Branching
if (x = 10) {
System.out.println(“x is ten”);
} else {
System.out.println(“x is NOT ten”);
}
Boolean tests
< (less than)
> (greater than)
== (equality)
while (x < 10) {
x = x + 1;
}
NameLoop.java
public class NameLoop {
public static void main (String[] args) {
int x = 1;
while (x < 10) {
System.out.println(“My name is Tom!”)
}
}
Java Basics
● Statements end with a semicolon;
● Code blocks are defined by curly braces {}
● Declare an int variable with a name and a type: int x;
● The assignment operator is one equals sign =
● The equals operator uses two equals signs ==
Java Basics
● A while loop runs everything within its block as long as
conditional test is true
● If the conditional is false, the while loop code block won’t
run, and execution will move down
● Put your boolean test within parentheses
while (x==4) {}
Exercise

Head First Java Chapter 1