Oracle Java Foundations study materials
Oracle Java Foundations study resources

Oracle Java Foundations: Java Basics

Oracle Java Foundations · Work through your exam, one topic at a time.

All guide sections

Java Basics

Source file rules

A Java source file ends in .java and may declare at most one public top-level type. If it does, the file name must match that type exactly, including case. Any number of non-public top-level classes may share the file. The package statement, if present, is the single first statement; import statements follow it. Only java.lang is imported implicitly, and wildcard imports like java.util.* do not recurse into subpackages.

Compile and run

javac turns source into bytecode .class files; java starts the JVM and takes the fully qualified class name without the .class extension. Classes are found via the classpath, which defaults to the current directory. A class with no package declaration sits in the default package and cannot be imported from a named package.

The entry point

Execution begins at public static void main(String[] args). All four pieces are required, but modifier order is irrelevant, so static public void main(String[] args) is fine and String... args also works. Traps: main missing static compiles but fails at runtime (no valid entry point); public static void main() compiles but is only an overload; public static int main(String[] args) is also just an overload; a private main prevents launch.

Identifiers and comments

Identifiers are case-sensitive, may contain letters, digits, _ and $, and cannot start with a digit or be a keyword (true, false, null are literals, not keywords). Three comment styles: //, /* */, and /** */ (Javadoc).

Worked example

public class App { static public void main(String[] args) { } } in a file named App.java compiles and runs correctly, because modifier order does not matter and the public class name matches the file name.