Head First Java
incomplete
Chapter 1 Breaking the Surface
The Way Java Works
Source: Create a source document (filename.java)
Compiler: Run the document through a source code compiler.
this will check for errors and won't let you compile until it is satisfied
Output (code): Compiler creates new file containing bytecode
Java will be able to translate/interpret this file into something it can run
The compiled bytecode is platform independent
Virtual Machines: A java virtual machine is used to run java bytecode code
This is what makes java code machine independent
What you'll do in Java
Type your source code
Compile your source file
javac <sourcefile.java>
This compiles the code and creates a
sourcefile.class
file
this is the bytecode
Run the program
java sourcefile
This starts the Java Virtual Machine (JVM)
The JVM translates the bytecode into something the underlying platform understands
Code structure in Java
Put a class in a source file
Put methods in a class
Put statements in a method
What goes in a source file?
source code file holds one class definition
What goes in a class?
a class has one or more methods
the method holds instructions on what that method should do
the method must be declared inside a class
What goes in a method?
within the curly braces of a method, write the instructions for how that method should be performed
method code is basically a set of statements
Anatomy of a class
When the JVM starts running, it looks for the class you gave it at the command line. Then is starts looking for a specially written method that looks exactly like:
Next, the JVM runs everything between the curly braces of your main method. Every Java application has to have at least one class, and at least one main method (main per application not per class)

Writing a class with a main
everything goes into a class
running a program tells the JVM to load class, then start executing the main() method
keep running till all the code in the main method is finished
main() method is the entry point of the whole app
Remember:
Syntax Fun
each statement must end in a semicolon
x = x + 1;
a single-line comment begins with two forward slashes
// single-line comment
most white space does not matter
x = 3 ;
variables are declared with a name and a type
int weight
classes and methods must be defined within a pair of curly braces
Looping and looping and...
Java jas three main looping constructs:
while
do-while
for
Concentrate on the while loop for now
while some condition is true, do everything inside the loop block
key to a loop is the conditional test
Simple boolean tests
do a simple boolean test by checking the value of a variable, using a comparison operator including:
<
(less than)>
(greater than)==
(equality)
while loop example: Loopy.java
output
Conditional branching
An if test is basically the same as the boolean test in a while loop - except instead of saying " while this..." you'll say, " if there is..."
IfTest.java
In the example above, if the statement is true, then two statements will print out, but regardless, at least one statement will always print.
output
Now, we can add an else to the condition, so that we can say something like " if this then do this else do this..."
IfTest2.java
output
BeerSong.java
Glossary
A definition list or a glossary:
- First Term
This is the definition of the first term.
- Second Term
This is the definition of the second term.