**Chapter 2: Foundations of Programming**
**Syntax and Structure**
**2.1 Essential Programming Concepts**
Programming requires precise communication through three fundamental aspects:
- **Syntax**: Rules governing code structure
*(e.g., missing semicolon: `int x = 5`)
- **Semantics**: Meaning behind valid code
*(e.g., infinite loop from incorrect termination condition)*
- **Pragmatics**: Writing maintainable, efficient code
*(proper naming, formatting, and documentation)*
**2.2 Anatomy of a Java Program**
The classic Hello World demonstrates core elements:
```java
// Single-line comment
/* Multi-line
comment */
public class HelloWorld { // Class declaration
public static void main(String[] args) { // Entry point
System.out.println("Hello World!"); // Statement
}
}
```
Key components:
- **Class Declaration**: Container for program logic
- **main Method**: Execution starting point
- **System.out**: Standard output stream
- **Statements**: Instructions ending with semicolons
**2.3 Variables and Data Types**
Java uses static typing with primitive and reference types:
| Primitive Type | Size | Range/Description |
|----------------|---------|----------------------------|
| `int` | 4 bytes | -2³¹ to 2³¹-1 |
| `double` | 8 bytes | IEEE 754 floating point |
| `boolean` | 1 bit | true/false |
| `char` | 2 bytes | Unicode characters |
**Declaration and Initialization:**
```java
int count = 10; // Explicit initialization
double pi = 3.14159; // Floating point
boolean isActive = true; // Logical value
char grade = 'A'; // Single character
var message = "Hello"; // Type inference (Java 10+)
```
**2.4 Input/Output Operations**
Using TextIO for robust user interaction:
```java
TextIO.put("Enter temperature (°C): ");
double celsius = TextIO.getlnDouble();
double fahrenheit = celsius * 9/5 + 32;
TextIO.putf("Converted: %.1f°F%n", fahrenheit);
```
**Formatted Output Specifiers:**
| Specifier | Output Type | Example |
|-----------|-------------------|---------------|
| `%d` | Integer | `System.out.printf("%05d", 42)` → 00042 |
| `%f` | Floating point | `%.2f` → 3.14 |
| `%s` | String | `%-10s` → left-aligned 10 chars |
| `%n` | Newline | Platform-independent line break |
**2.5 Operators and Expressions**
Operator precedence table (highest to lowest):
| Operators | Description |
|---------------------------|---------------------------|
| `() [] .` | Parentheses, array access |
| `++ -- ! (type)` | Unary operators |
| `* / %` | Multiplicative |
| `+ -` | Additive |
| `< <= > >= instanceof` | Relational |
| `== !=` | Equality |
| `&&` | Logical AND |
| `||` | Logical OR |
| `= += -= *= /= %=` | Assignment |
**Common Patterns:**
```java
// Compound assignment
int total = 0;
total += 5; // Equivalent to total = total + 5
// Type conversion
int items = 10;
double average = (double) total / items;
// String manipulation
String name = "Alice";
String greeting = "Hello " + name + "!"; // Concatenation
```
**2.6 Control Structures**
Basic decision-making with boolean logic:
```java
TextIO.put("Enter age: ");
int age = TextIO.getlnInt();
String status = (age >= 18)
? "Adult" // If true
: "Minor"; // If false
TextIO.putln("Status: " + status);
```
**2.7 Modern Development Practices**
**IDE Features:**
- **VS Code**: Lightweight editor with Java extensions
- **IntelliJ**: Smart code completion and refactoring
- **Eclipse**: Robust debugging tools
**Command Line Basics:**
```bash
# Compile and run
javac -d bin src/*.java
java -cp bin MainClass
# JShell (REPL - Java 9+)
jshell> var message = "Interactive Java"
jshell> message.length()
```
**Best Practices:**
1. Use `final` for constants: `final double TAX_RATE = 0.07;`
2. Prefer `printf` for formatted output
3. Validate user input with loops (covered in Chapter 3)
4. Avoid magic numbers - use named constants
5. Keep methods short (<15 lines)
---
**Key Improvements:**
1. Added modern Java features (var, JShell)
2. Included operator precedence table
3. Enhanced code examples with comments
4. Structured complex information into tables
5. Added best practices section
6. Mentioned multiple IDE options
7. Used formatted output specifiers table
8. Included type inference examples
9. Added command-line basics for modern development
10. Removed deprecated concepts and applet references
This revision maintains pedagogical flow while incorporating contemporary Java practices
and improving information accessibility through visual organization.