Java Basics
Introduction to Java
Complete lesson for Introduction to Java using LEARNCODEGURU’s 6-level learning model — from basics to advanced practice, with Java OOP and Collections where relevant.
Basics — Introduction to Java
Section: Java Basics. Start Java with history, features, JDK vs JRE vs JVM, compilation, and your first Hello World.
What is Java?
- Java is a general-purpose, class-based, object-oriented language created by James Gosling at Sun Microsystems (first public release 1995; now stewarded by Oracle and the OpenJDK community).
- Source files (
.java) compile to bytecode (.class) that the Java Virtual Machine executes — the “write once, run anywhere” idea. - Used for backend services, Android (historically), enterprise apps, big data tools, and many coding interviews.
JDK vs JRE vs JVM
- These three names are the most common Java-platform interview question. Do not mix them up.
- Since Java 11, Oracle no longer ships a separate consumer JRE the old way — you typically install a JDK. The *concepts* still matter.
| Component | Contains | Used for |
|---|---|---|
| JVM | Runtime engine: class loader, bytecode verifier, interpreter / JIT, GC | Execute .class bytecode on a host OS/CPU |
| JRE | JVM + core class libraries (java.lang, java.util, …) | Run already-compiled Java programs |
| JDK | JRE + development tools (javac, jar, javadoc, debugger) | Write, compile, package, and run Java code |
First program — Hello World
- The public class name must match the file name: save this as
HelloWorld.java. - Compile with
javac HelloWorld.java(createsHelloWorld.class), then runjava HelloWorld(no.classsuffix). mainmust bepublic static void main(String[] args)so the launcher can start the program without an instance.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
} Why Java is still taught first for interviews
- Strong static typing, a huge standard library, and a predictable memory model (GC, no raw pointers).
- Checked exceptions, collections, and OOP vocabulary show up constantly in campus and service-company interviews.
- Java 17 is a current LTS: records, text blocks, switch expressions, and sealed classes are fair game alongside classic Java 8 streams/lambdas.
Advanced — Introduction to Java
Compilation, bytecode, and the launcher
javacis a compiler, not an interpreter. The JVM then interprets and/or JIT-compiles hot methods to native code.java HelloWorlduses the application class loader; apublicclass in the default package is found on the current directory classpath (.).- If you use a package (
package com.demo;), the file must live in a matching folder and you runjava com.demo.HelloWorldfrom the package root. - One public top-level class per
.javafile. Additional package-private classes in the same file are allowed but uncommon in tutorials. maincan also bepublic static void main(String... args)(varargs). A non-staticmainwill not start the program.
Tricky Interview Points
Q1) What is Java?
Answer Java is a class-based, object-oriented language that compiles to bytecode executed by the JVM, which gives platform independence.
Q2) Who created Java, and what was its original name?
Answer James Gosling (Sun Microsystems). The project was originally called Oak, then renamed Java.
Q3) What is the difference between JDK, JRE, and JVM?
Answer JVM executes bytecode. JRE = JVM + standard libraries (needed to run programs). JDK = JRE + tools like javac (needed to develop). You install a JDK to compile.
Q4) Is Java compiled or interpreted?
Answer Both. javac compiles source to bytecode; the JVM interprets bytecode and JIT-compiles hot code to native instructions.
Q5) Why is Java called platform independent?
Answer Bytecode is portable. You compile once; any compatible JVM on Windows, Linux, or macOS can run the same .class files.
Q6) What is the signature of the entry-point method?
Answer public static void main(String[] args). It must be public and static so the launcher can invoke it without constructing an object.
Q7) What happens if the public class name does not match the file name?
Answer javac fails. The public top-level class name must match the .java file name.
Q8) Do you still need a separate JRE in Java 17?
Answer Conceptually JRE still means “runtime only”, but modern distributions ship JDKs. You can create a custom runtime with jlink. Interviews still expect JDK > JRE > JVM.
Simple Programs
Hello World
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
} Hello with command-line args
public class HelloArgs {
public static void main(String[] args) {
String name = args.length > 0 ? args[0] : "learner";
System.out.println("Welcome to Java, " + name);
}
} Problem Solving Tricks
- File name = public class name.
HelloWorld.java→public class HelloWorld. - Run
java HelloWorld, notjava HelloWorld.class. - If compilation succeeds but
javasays “could not find or load main class”, you are in the wrong directory or used a package without matching folders. System.outis aPrintStream;printlnadds a platform line separator.- Print the Java version you are using:
java -versionandjavac -version— they must match if you have several JDKs installed. - For Java 11+ you can run a single-file program with
java HelloWorld.java(no separatejavacstep).
Practice Programs List
- Write
HelloWorld.java, compile withjavac, and run withjava. - Change the class name without renaming the file and read the compiler error.
- Print your name and the value of
args.lengthwhen you pass 0, 1, and 3 arguments. - Explain JDK, JRE, and JVM in four sentences from memory, then check the table.
- Run
java -versionand write down whether you have an LTS (11, 17, 21). - Move Hello World into package
demoand run it from the correct working directory.