Getting Started
Programming is the process of giving a computer a set of precise instructions to solve a problem. To do this, we first develop an algorithm, which is a step-by-step plan. We then translate that algorithm into a language the computer can understand. This chapter introduces the fundamental concepts of how your ideas, written in the Java programming language, are translated into an executable program that a computer can run.
What You Should Be Able to Do
Describe the process of translating human-readable code into machine-executable instructions.
Differentiate between high-level and low-level programming languages.
Identify and categorize common programming errors as compile-time, run-time, or logic errors.
Write a simple Java program that produces console output using the
mainmethod.Explain the purpose of a compiler and the Java Virtual Machine (JVM).
Key Concepts & Java Implementation
The Core Idea: From Human to Machine
Computers operate on a very low level, using electrical signals that represent ones and zeros. Writing instructions directly in this format, known as a low-level language (e.g., machine code), is extremely difficult and tedious for humans.
To bridge this gap, we use high-level languages like Java. These languages use syntax that is closer to human language, making it easier to write, read, and maintain complex programs. However, a computer cannot directly understand a high-level language. It must be translated into a low-level language first. This translation process is central to all programming.
The Java Compilation Process
Java uses a special two-step process to run its programs, which allows it to be platform-independent (meaning the same code can run on Windows, macOS, or Linux without changes).
Writing Source Code: You write your program in a file ending with
.java. This human-readable file is called source code.Compilation: You use a compiler, a special program that translates your source code. The Java compiler (
javac) checks your code for syntax errors. If there are no errors, it produces a new file ending in.class. This file does not contain machine code for a specific processor; instead, it contains an intermediate language called Java bytecode.Execution: The Java Virtual Machine (JVM) is used to run the program. The JVM is an interpreter that reads the bytecode from the
.classfile and translates it into native machine code for the specific computer it is running on. This "just-in-time" translation allows Java to be so versatile.
This can be visualized as:
MyProgram.java (Source Code) -> Compiler -> MyProgram.class (Bytecode) -> JVM -> Program Runs
Your First Java Program
Every executable Java program needs an entry point—a specific place where the JVM knows to begin execution. This entry point is a method called main.
A method is a named block of code that performs a specific task. The main method has a very specific signature that you must use every time.
Annotated Java Example: Hello, World!
// public class HelloWorld defines a new class named HelloWorld.
// All Java code lives inside classes.
public class HelloWorld {
// This is the main method, the entry point of the program.
// The JVM looks for this exact signature to start execution.
public static void main(String[] args) {
// System.out.println() is a command that prints a line of text
// to the console and then moves the cursor to the next line.
System.out.println("Hello, World!");
}
}
When this program is compiled and run, it will display the text Hello, World! in the console.
Types of Programming Errors
As you write code, you will inevitably encounter errors. Understanding the different types of errors is crucial for debugging (the process of finding and fixing them).
| Error Type | When It's Found | Description |
|---|---|---|
| Compile-time Error | During compilation | The code violates the grammar rules of Java (e.g., a missing semicolon or a misspelled keyword). The compiler cannot create the .class file. |
| Run-time Error | During execution | The code is syntactically correct, but an unexpected situation occurs while the program is running (e.g., dividing by zero or trying to access a file that doesn't exist). The program will crash. |
| Logic Error | After execution | The program compiles and runs without crashing, but it produces an incorrect result. The programmer's reasoning was flawed. These are often the hardest to find. |
Java Syntax Quick-Reference
A compact reference for the syntax introduced in this topic.
| Syntax | Purpose |
|---|---|
public class ClassName { ... } | Defines a new class, which acts as a blueprint for objects and a container for your code. |
public static void main(String[] args) { ... } | The required entry point for any executable Java program. Execution starts here. |
System.out.print(value); | Prints the value to the console without moving to a new line. |
System.out.println(value); | Prints the value to the console and then moves the cursor to the next line. |
// comment | A single-line comment. The compiler ignores everything from // to the end of the line. |
; | The semicolon character, used to terminate most Java statements. |
Core Code Examples & Terminology
Algorithm: A finite, unambiguous set of instructions designed to solve a specific problem.
Compiler: A program that translates an entire source code file from a high-level language into a lower-level language (like bytecode) before execution begins.
Interpreter: A program that reads and executes code line by line, translating as it goes. The JVM acts as an interpreter for Java bytecode.
Source Code: The human-readable code written by a programmer in a high-level language, typically stored in a
.javafile.Bytecode: The platform-independent, intermediate code generated by the Java compiler, stored in a
.classfile and executed by the JVM.Compile-time Error: An error in the syntax of the code, such as a typo or missing semicolon, that prevents the compiler from creating bytecode.
Run-time Error: An error that occurs during the execution of a program, causing it to terminate unexpectedly. Also known as an exception.
Core Snippet 1: Basic Program Structure
public class MyProgram { public static void main(String[] args) { // Your program's instructions go here. } }This snippet shows the minimal structure required for any runnable Java application.
Core Snippet 2: Console Output
System.out.println("First line."); System.out.print("Part one, "); System.out.print("part two.");This demonstrates the difference between
println(which adds a newline) andprint(which does not).
Core Skill Check
Code Tracing: What is the exact console output of this Java code:
System.out.print("Java"); System.out.println(" is fun."); System.out.print("Indeed!");?Debugging: Identify the compile-time error in this Java code:
public class Test { public static void main(String[] args) { System.out.println("Test") } }.Application: Write a single line of Java code that prints the message "Ready to code!" to the console and moves the cursor to the next line.
Common Misconceptions & Errors
Confusing
printandprintln: Remember thatprintlnstands for "print line" and automatically adds a newline character after its output. Multipleprintstatements will result in output on the same line.Forgetting Semicolons (
;): Nearly every Java statement must end with a semicolon. Forgetting one is the most common compile-time error for beginners.Case Sensitivity: Java is case-sensitive.
system,System, andSYSTEMare all treated as different things. Keywords likepublicandclassmust be lowercase.Incorrect
mainMethod Signature: Themainmethod signature,public static void main(String[] args), must be typed exactly. Any deviation (e.g.,Main,string[], or forgettingstatic) will prevent the JVM from finding and running your program.
Summary
Programming begins with an algorithm, which is then expressed in a high-level language like Java. To run a Java program, a compiler first translates the human-readable source code into platform-independent bytecode. This bytecode is then executed by the Java Virtual Machine (JVM), an interpreter that makes Java portable across different operating systems. Every Java program begins execution in the main method. As a programmer, you will encounter compile-time, run-time, and logic errors, and understanding the difference is key to effective debugging. Using System.out.println() is the fundamental way to display information and see the results of your code.