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.
Basics — Java Fundamentals
Section: Java Basics. Tokens, keywords, program structure, identifiers, and the javac / java compilation pipeline.
Typical .java layout
- Optional
package→importstatements → class declaration → fields → constructors → methods →mainif this class is the entry point. java.langis 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, andnull. - 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:
PascalCasetypes,camelCasemethods/fields,UPPER_SNAKEconstants,lowercasepackages. - 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 kind | Examples | Notes |
|---|---|---|
| Keyword | class, static, new | Reserved; cannot be identifiers |
| Identifier | score, HelloWorld | Names you choose |
| Literal | 17, "Java", true | Fixed values in source |
| Operator | +, &&, ?: | Act on operands |
| Separator | ; { } ( ) , . | Structure the program |
Compilation: javac and java
javac Hello.javareads source, type-checks it, and writesHello.classbytecode.java Hellostarts a JVM, loads the class, and invokespublic static void main.- Errors at compile time (missing
;, type mismatch) never reach the JVM. Runtime errors (NPE,/ by zero) happen after launch.
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+).
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.
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);
}
} Problem Solving Tricks
- If
javacis not found, the JDKbinfolder 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.
Practice Programs List
- List five token kinds and give one example of each from a 5-line program.
- Try naming a variable
staticand record the compiler message. - Write a class with a constant, a local variable, and a print statement, then compile it.
- Draw the steps: source → javac → bytecode → JVM → output.
- Create two classes in two files where only one has
mainand call a method of the other.