# Ch 01: Breaking the Surface

**Source**: Head First Java, Second Edition | **Pages**: 35-60

## 🎯 Learning Objectives

Java basics and first program

## 📚 Key Concepts

* Java platform basics
* Compiling and running Java
* main() method structure
* System.out.println()
* Basic syntax and structure
* while loops and conditionals
* Variables and types intro

***

## 📖 Detailed Notes

### 1. Java platform basics

You’ll type a source code file, compile it using thejavac compiler, then run the compiled bytecodeon a Java virtual machine.

**source —** create a source document party.java

**compiler —** checks for errors and won't let us compile till it is satisfied that everything will run correctly. ex— patry.class(made up of bytecode)

**output —** compiler creates a new doc coded into java byte code, this is platform independent, the bytecode is then used to run the code into jvm

**virtual machine —** any machine which has jvm installed can run the compiled byte code.

### 2. Compiling and running Java

*Essential concept for mastering Java and OOP.*

**Example**:

```java
import java.awt.*;
import java.awt.event.*;
class Party {
   public void buildInvite() {
     Frame f = new Frame();
     Label l = new Label(“Party at Tim’s”);
     Button b = new Button(“You bet”);
     Button c = new Button(“Shoot me”);
     Panel p = new Panel();
     p.add(l);    
    }   // more code here... 
}
```

### 3. main() method structure

*Essential concept for mastering Java and OOP.*

**Example**:

```java
Party.class file is made up 
```

### 4. System.out.println()

*Essential concept for mastering Java and OOP.*

**Example**:

```java
Method void buildInvite()
```

### 5. Basic syntax and structure

*Essential concept for mastering Java and OOP.*

**Example**:

```java
Party.class file. The JVM 
```

### 6. while loops and conditionals

*Essential concept for mastering Java and OOP.*

**Example**:

```java
int size = 27;
String name = “Fido”;
Dog myDog = new Dog(name, size);
x = size - 5;
if (x < 15) myDog.bark(8);
while (x > 3) {
   myDog.play();
}
int[] numList = {2,4,6,8};
System.out.print(“Hello”);
System.out.print(“Dog: “ + name);
String num = “8”;
int z = Integer.parseInt(num);
try {
   readTheFile(“myFile.txt”);
} 
catch(FileNotFoundException ex) {
   System.out.print(“File not found.”);
}
```

### 7. Variables and types intro

*Essential concept for mastering Java and OOP.*

**Example**:

```java
int size = 27;
String name = “Fido”;
Dog myDog = new Dog(name, size);
x = size - 5;
if (x < 15) myDog.bark(8);
while (x > 3) {
   myDog.play();
}
int[] numList = {2,4,6,8};
System.out.print(“Hello”);
System.out.print(“Dog: “ + name);
String num = “8”;
int z = Integer.parseInt(num);
try {
   readTheFile(“myFile.txt”);
} 
catch(FileNotFoundException ex) {
   System.out.print(“File not found.”);
}
```

***

## 💡 Important Points to Remember

* that Java is a strongly-typed lan-
* this is not meant to be a tutorial... you’ll be
* when you type this into an editor, let
* Each snippet

***

## ✅ Self-Check Questions

Test your understanding:

1. Can you explain the main concepts covered in this chapter?
2. Can you write code examples demonstrating these concepts?
3. Do you understand when and why to use these features?
4. Can you explain the benefits and tradeoffs?

## 🔄 Quick Revision Points

* [ ] Java platform basics
* [ ] Compiling and running Java
* [ ] main() method structure
* [ ] System.out.println()
* [ ] Basic syntax and structure
* [ ] while loops and conditionals
* [ ] Variables and types intro

***

## 📝 Practice Exercises

1. Write your own code examples for each key concept
2. Modify existing examples to test edge cases
3. Explain concepts to someone else
4. Create a small project using these concepts

## 🔗 Related Chapters

Review related concepts from other chapters to build comprehensive understanding.

***

*For complete details, diagrams, and all examples, refer to Head First Java Second Edition, pages 35-60.*

## Chapter 1: Breaking the Surface — Study Notes

This chapter provides a high-level overview of how Java works, from writing code to running it on the Java Virtual Machine (JVM). It introduces the basic syntax, structure, and control flow needed to start programming.

<a class="button secondary"></a>

### 1. The Java Workflow

The process of creating a Java application follows three main steps:

<a class="button secondary"></a>

1. Source: You write a source file (e.g., `Party.java`).
2. Compiler: You run the compiler (`javac`), which checks for errors and translates the source into Bytecode.
3. Output: The compiler creates a `.class` file (e.g., `Party.class`). This bytecode is platform-independent.
4. Virtual Machine: The JVM translates the bytecode into something the underlying operating system understands and executes the program.

***

### 2. Java Structure

In Java, everything lives in a Class. A class is the blueprint for an object.

<a class="button secondary"></a>

* Classes: Your program is made of one or more classes.
* Methods: Inside a class, you have methods (functions). Methods contain the instructions for what the program should do.
* Statements: Inside methods, you write statements (individual instructions like variable declarations or mathematical operations).

#### The Anatomy of a Class

Java

```
public class MyFirstApp { 
    public static void main (String[] args) {
        System.out.println("I Rule!");
    }
}
```

* `public static void main (String[] args)`: This is the entry point of your program. Every Java application must have at least one class with a `main` method to run.

  <a class="button secondary">+1</a>

***

### 3. Basic Syntax and Rules

* Statements: Each statement must end with a semicolon (`;`).

  <a class="button secondary"></a>
* Comments: Use `//` for single-line comments.
* Variables: Must be declared with a type and a name (e.g., `int x = 5;`).

  <a class="button secondary"></a>
* White Space: The compiler ignores extra spaces, but they are crucial for human readability.

***

### 4. Control Flow (Loops & Conditionals)

Java uses standard C-style syntax for making decisions and repeating actions.

<a class="button secondary"></a>

#### The `while` Loop

Repeats a block of code as long as a boolean condition is true.

Java

```
int x = 1;
while (x < 3) {
    System.out.print("Doo");
    System.out.print("Bee");
    x = x + 1;
}
```

#### The `if/else` Conditional

Executes a block of code only if the condition is met.

Java

```
if (x == 3) {
    System.out.print("Do");
}
```

#### Key Operators

* Comparison: `==` (equals), `!=` (not equal), `<` (less than), `>` (greater than).

  <a class="button secondary"></a>
* Assignment: `=` (sets the value).

  <a class="button secondary"></a>

***

### 5. The Java Virtual Machine (JVM)

The JVM is the "magic" that makes Java portable. Because the compiler creates bytecode instead of native machine code, the same `.class` file can run on any device (Windows, Mac, Linux) that has a JVM installed.

<a class="button secondary"></a>

***

### 6. Summary Checklist for Revision

* Does every statement end with a `;`?
* Is your code wrapped in a `class`?
* Do you have a `main` method to start the program?
* Are you using `System.out.println` to output text?

  <a class="button secondary"></a>
* Is your loop condition eventually becoming `false` to avoid infinite loops?

  <a class="button secondary"></a>
