⚡0 XP🔥0

Introduction to Java

Lesson 1 of 15 ⏱ 4 min read ✎ 2 exercises ❓ 5-question quiz ▶ Runs Java

What Java is, why "write once, run anywhere" works, JDK vs JRE vs JVM, your first program, and how javac and java compile and run code.

What is Java?#

Java is a general-purpose, object-oriented programming language released by Sun Microsystems in 1995 (created by James Gosling's team; Oracle owns it today). It is one of the most widely used languages in the world:

  • Enterprise and banking software - most large Indian IT services projects (TCS, Infosys, Wipro...) use Java with Spring Boot.
  • Android apps - Android was built around Java (Kotlin is now preferred, but it runs on the same platform).
  • Big data tools - Hadoop, Kafka, Spark and Elasticsearch run on the JVM.
  • Desktop and server applications, trading systems, and a huge open-source ecosystem.

Key features: object-oriented, platform independent, strongly typed, automatic memory management (garbage collection), multithreading built in, and a very large standard library.

Write once, run anywhere#

C and C++ compile to machine code for one specific CPU and operating system. Java compiles to bytecode - instructions for a virtual computer, the Java Virtual Machine (JVM). Any machine with a JVM (Windows, Linux, macOS, Android) can run the same .class file.

Main.java  --javac-->  Main.class (bytecode)  --java (JVM)-->  runs on Windows / Linux / Mac
Term What it is
JVM (Java Virtual Machine) runs bytecode; translates it to machine code (using a JIT compiler for speed)
JRE (Java Runtime Environment) JVM + standard libraries - enough to run Java programs
JDK (Java Development Kit) JRE + compiler (javac) + tools - needed to develop Java programs
हिंदी में समझें

Java का code पहले bytecode में compile होता है, किसी खास computer के machine code में नहीं। यह bytecode JVM पर चलता है, और JVM हर operating system के लिए उपलब्ध है। इसलिए एक बार compile किया हुआ program Windows, Linux, Mac - कहीं भी चल जाता है: "Write Once, Run Anywhere"।

Your first Java program#

▶ Try it YourselfJava
Ctrl + Enter to run
Output
Hello, Java!
2 + 3 = 5

Press Run - the code is compiled with javac and run on a real JVM.

Line by line#

Code Meaning
public class Main Every Java program lives inside a class. public means visible everywhere.
public static void main(String[] args) The entry point. The JVM calls this method first.
public the JVM must be able to call it from outside
static it belongs to the class, so no object needs to be created first
void it returns nothing
String[] args command-line arguments as an array of strings
System.out.println(...) print, then move to a new line
Common Mistake

Java is case-sensitive: System is not system, and main is not Main. Every statement ends with ;, and every { needs a matching }.

▶ Try it YourselfJava
Ctrl + Enter to run
Output
No new line... now a new line.
Tab:	after tab
Quote: "Java" and backslash: \
Name: Isha, Age: 20, CGPA: 8.76

printf works like C's printf: %s string, %d integer, %.2f decimal with 2 places, %n new line.

Comments#

// single-line comment

/* multi-line
   comment */

/**
 * Javadoc comment - used to generate API documentation.
 */

Installing Java and running from the terminal#

  1. Install a JDK - for example Eclipse Temurin (Adoptium) JDK 21 or Oracle JDK 21 (21 is a long-term-support version).
  2. Use an IDE: IntelliJ IDEA Community (most popular), Eclipse, VS Code with the Java extension pack, or BlueJ for beginners.
  3. From a terminal:
javac Main.java     # compile → Main.class
java Main           # run (no .class extension)
java Main.java      # Java 11+: compile and run a single file in one step
Tip

In the browser editor here, always keep the class with main named Main. In your own projects, the file name must match the public class name.

Summary#

  • Java compiles to bytecode that runs on the JVM on any operating system.
  • JDK (develop) ⊃ JRE (run) ⊃ JVM (execute).
  • Programs start at public static void main(String[] args) inside a class.
  • println adds a new line, print does not, printf formats.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Your first Java program

Print exactly Welcome to Java at Vindhyaedu from main.

Expected output:
Welcome to Java at Vindhyaedu
2

Print a box

Using print and println, print:

+------+
| Java |
+------+
Expected output:
+------+
| Java |
+------+

Quick Quiz

Q1.

What does the Java compiler (javac) produce?

Q2.

Which is the correct signature of the entry point?

Q3.

If a file declares public class Hello, the file must be named...

Q4.

Which one do you need to DEVELOP Java programs?

Q5.

What is the difference between print and println?