Skip to main content

Java Basics

Java Fundamentals

Complete lesson for Java Fundamentals using LEARNCODEGURU’s 6-level learning model — from basics to advanced practice, with Java OOP and Collections where relevant.

L1

Basics — Java Fundamentals

Section: Java Basics. Tokens, keywords, program structure, identifiers, and the javac / java compilation pipeline.

Typical .java layout

  • Optional package → import statements → class declaration → fields → constructors → methods → main if this class is the entry point.
  • java.lang is imported automatically (String, System, Object, Math).
public class FundamentalsDemo {
    public static void main(String[] args) {
        int count = 10;
        System.out.println("count = " + count);
    }
}

Tokens

  • The compiler breaks source into tokens: keywords, identifiers, literals, operators, and separators (;, ,, ., (), {}, []).
  • Literals include integers (42), longs (42L), doubles (3.14), floats (3.14f), chars ('A'), strings ("hi"), booleans, and null.
  • Java is free-form whitespace; statements end with ; (except class/method/block declarations).

Keywords and identifiers

  • Identifiers start with a letter, $, or _, then letters, digits, $, or _. They cannot be keywords.
  • Convention: PascalCase types, camelCase methods/fields, UPPER_SNAKE constants, lowercase packages.
  • Java 17 keywords/reserved words you will meet early: class, public, static, void, int, if, else, for, while, new, return, this, super, extends, implements, try, catch, finally, throw, throws, import, package, boolean, char, byte, short, long, float, double, null/true/false (literals, not keywords in the same way, but reserved).
Token kindExamplesNotes
Keywordclass, static, newReserved; cannot be identifiers
Identifierscore, HelloWorldNames you choose
Literal17, "Java", trueFixed values in source
Operator+, &&, ?:Act on operands
Separator; { } ( ) , .Structure the program

Compilation: javac and java

  • javac Hello.java reads source, type-checks it, and writes Hello.class bytecode.
  • java Hello starts a JVM, loads the class, and invokes public static void main.
  • Errors at compile time (missing ;, type mismatch) never reach the JVM. Runtime errors (NPE, / by zero) happen after launch.
L2

Advanced — Java Fundamentals

Comments, Unicode, and source rules

  • // line comments, /* */ block comments, and /** */ Javadoc. Nested /* */ is not allowed.
  • A file may contain at most one public top-level type; that type’s name is the file name.
  • var (Java 10+) is not a reserved keyword in all contexts — it is a reserved type name for local-variable type inference.
  • Underscores in numeric literals (1_000_000) are allowed since Java 7; a lone _ is no longer a valid identifier (Java 9+).
L3

Tricky Interview Points

Q1) What are tokens in Java?

Answer The smallest elements the compiler recognizes: keywords, identifiers, literals, operators, and separators.

Q2) Can you use a keyword as a variable name?

Answer No. Keywords are reserved. `int class = 1;` does not compile.

Q3) What is the difference between javac and java?

Answer javac compiles .java source to .class bytecode. java launches the JVM and runs a class that has main.

Q4) Which package is imported by default?

Answer java.lang — that is why String and System need no import.

Q5) What is a legal identifier?

Answer It starts with a letter, $ or _, then letters, digits, $ or _. It must not be a keyword. Names like 1name or class are illegal.

Q6) Is main required in every class?

Answer No. Only the entry-point class needs main. Libraries and other types have no main.

L4

Simple Programs

Tokens and a simple class structure

public class TokensDemo {
    static final int MAX = 3; // constant identifier

    public static void main(String[] args) {
        int score = 10; // identifier + literal
        score += MAX;   // operator
        System.out.println("score = " + score);
    }
}
L5

Problem Solving Tricks

  • If javac is not found, the JDK bin folder is not on PATH — you installed a runtime-only image or the wrong JDK.
  • Keep one public class per file until you know why you would do otherwise.
  • Use // comments while learning; add Javadoc when you write APIs.
  • Compile with matching versions: javac --release 17 File.java.
L6

Practice Programs List

  1. List five token kinds and give one example of each from a 5-line program.
  2. Try naming a variable static and record the compiler message.
  3. Write a class with a constant, a local variable, and a print statement, then compile it.
  4. Draw the steps: source → javac → bytecode → JVM → output.
  5. Create two classes in two files where only one has main and call a method of the other.