No history yet

Java Syntax and Structure

The Rules of the Road

Coming from another programming language, the first thing you'll notice about Java is that it's a bit strict. It follows a set of rules called static typing. This means you have to declare the type of a variable before you use it, and you can't change it later.

Think of it like organizing a kitchen. You have a jar labeled "Flour" and a bottle labeled "Water." You can only put flour in the flour jar and water in the water bottle. Java works the same way with its data, which helps prevent mixing things up and causing errors.

Once you declare a variable as an integer, for example, it can only ever hold integers. This helps catch potential bugs early, during compilation, rather than when the program is running.

// This is perfectly fine in Java
int myNumber = 42;
String myText = "Hello, Java!";

// But this will cause an error!
// You can't put a string into a variable meant for an integer.
myNumber = "Some other text";

Everything Lives in a Class

In Java, all code—every variable, every function—must reside inside a class. A class is like a blueprint or a container for your program's logic. You can't just have loose code sitting in a file.

Every Java application needs an entry point, a place where the program knows to begin. This is a special method called main. It has a very specific signature that you'll memorize quickly.

public class MyFirstApp {
  // The main method is the entry point of the application
  public static void main(String[] args) {
    // Code inside here will run when the program starts
    System.out.println("Hello, World!");
  }
}

Let's quickly break down that main method declaration:

  • public: Anyone can access it.
  • static: It can be run without creating an instance of MyFirstApp.
  • void: This method doesn't return any value.
  • main: The name of the method.
  • (String[] args): It accepts a list of text strings as arguments, which can be passed from the command line.

Getting Your Tools Ready

To write and run Java code, you need two key things: the Java Development Kit (JDK) and an Integrated Development Environment (IDE).

The JDK is a software package that contains the tools needed to compile your Java code into bytecode—a format the machine can understand—and run it. It includes the compiler (javac) and the Java Runtime Environment (JRE), which actually executes the program.

Lesson image

An IDE is a text editor with superpowers. It helps you write code more efficiently with features like syntax highlighting, code completion, and debugging tools. Popular choices for Java include IntelliJ IDEA, Eclipse, and Visual Studio Code with Java extensions.

Organizing Code with Packages

As programs grow, you need a way to keep them organized. Java uses packages for this. A package is a namespace that organizes a set of related classes, similar to how you use folders to organize files on your computer. Package names are typically written in reverse domain name order to ensure they are unique, like com.company.project.

To use a class from another package, you must import it. This tells the compiler where to find the code you're referencing.

// This file is part of the 'com.example.greetings' package
package com.example.greetings;

// We want to use the 'Scanner' class, which is in another package
import java.util.Scanner;

public class Greeter {
  public static void main(String[] args) {
    // Now we can use the Scanner class because we imported it
    Scanner input = new Scanner(System.in);
    System.out.println("Enter your name: ");
    String name = input.nextLine();
    System.out.println("Hello, " + name + "!");
  }
}

Now you have the basic building blocks: a statically typed language, a class-based structure, and a way to organize your code. Let's test what you've learned.

Quiz Questions 1/5

What is a key characteristic of Java's static typing system?

Quiz Questions 2/5

In a Java application, where must all executable code, including variables and methods, be placed?