Java tutorials  /  Introduction to Java Programming
Chapter 1 · Java

Introduction to Java Programming

Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It follows the 'Write Once, Run Anywhere' (WORA) philosophy, meaning compiled Java code can run on all platforms that support Java without the need for recompilation.

Think of Java as a universal translator for computers. You write your code once in Java, and instead of the computer running it directly, a special program called the Java Virtual Machine (JVM) translates it into instructions that any computer (Windows, Mac, Linux, or even a mobile phone) can understand. This is why Java is used everywhere, from Android apps to bank servers.

Consider a large e-commerce company like Amazon or a banking application. These systems need to run reliably on thousands of different servers, potentially with different operating systems, without crashing due to platform-specific issues. Java is chosen for such systems because: 1) The JVM abstracts away the underlying OS, so the same compiled '.class' files run identically on a Linux server in a data center and a developer's Windows laptop. 2) Java's strong memory management (Garbage Collection) prevents common memory leaks that could crash a server handling millions of transactions. 3) Android, the world's most popular mobile OS, uses Java (and Kotlin, which is JVM-compatible) as its primary application language, meaning skills learned here directly transfer to mobile app development.

Before Java, languages like C and C++ required code to be recompiled for every different operating system and hardware architecture, which was time-consuming and error-prone. Java solved this portability problem with the JVM. Additionally, Java introduced automatic memory management (garbage collection), reducing memory leaks and pointer-related crashes that were common in C/C++. Its strong typing, extensive standard library, and built-in security features made it a preferred choice for enterprise-grade, mission-critical applications like banking systems, e-commerce platforms, and large-scale distributed systems.

  • Java SE (Standard Edition): The core platform providing the foundational APIs of the Java language, including basic data types, objects, collections, I/O, networking, and security. Used for building desktop applications and as the base for other editions.
  • Java EE / Jakarta EE (Enterprise Edition): Built on top of Java SE, this edition provides APIs and runtime environments for developing large-scale, distributed, multi-tiered enterprise applications, such as web services, servlets, and REST APIs.
  • Java ME (Micro Edition): A subset of Java SE designed specifically for developing applications for embedded and mobile devices with limited resources, such as older feature phones and IoT devices.
  • JavaFX: A software platform used for creating rich, modern desktop applications with graphical user interfaces (GUIs), supporting features like CSS styling, animations, and hardware-accelerated graphics.
public class ClassName { public static void main(String[] args) { // Your code statements go here } }
Before running any Java logic, a beginner must understand the mandatory structure Java enforces: every piece of executable code must live inside a class, and program execution must start from a special method called 'main'. Let's write a simple program that prints a welcome message to the console to understand this structure.
Your First Java Program: Hello World
This example demonstrates the minimal, mandatory structure required to write and run any Java program, including the class declaration and the main method entry point.
Java
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); System.out.println("Welcome to Java programming with CompileX."); } }
Hello, World! Welcome to Java programming with CompileX.
The 'public class HelloWorld' line declares a class named HelloWorld; the filename must match this class name exactly (HelloWorld.java). The 'public static void main(String[] args)' line is the entry point of every Java application — the JVM looks for this exact method signature to start execution. 'System.out.println()' is a built-in method used to print text to the console, automatically adding a new line after each call.
Compiling and Running Java from the Command Line
This example shows the two-step process of converting human-readable Java source code into bytecode, and then executing that bytecode using the JVM.
Java (Terminal Commands)
javac HelloWorld.java java HelloWorld
Hello, World! Welcome to Java programming with CompileX.
The 'javac' command is the Java compiler; it reads HelloWorld.java and produces a platform-independent file called HelloWorld.class containing bytecode. The 'java' command then launches the JVM, which reads HelloWorld.class and executes it, producing the program's output. This two-step compile-then-run process is central to Java's 'Write Once, Run Anywhere' capability.
  • Filename and Public Class Name Mismatch: Beginners often name their file 'Main.java' but declare 'public class HelloWorld'. Java requires that the filename exactly match the name of the public class it contains (case-sensitive), otherwise the compiler throws an error: 'class HelloWorld is public, should be declared in a file named HelloWorld.java'.
  • Forgetting the Exact 'main' Method Signature: Writing 'public void main(String[] args)' instead of 'public static void main(String[] args)' is a common error. Omitting 'static' means the JVM cannot call the method without first creating an object, causing a runtime error: 'Main method not found in class'.
  • Case Sensitivity Errors: Java is case-sensitive, so writing 'String[] Args' or 'system.out.println' (lowercase 's') instead of 'System.out.println' will result in compilation errors. Beginners coming from case-insensitive environments often overlook this.
  • Missing Semicolons: Every statement in Java must end with a semicolon. Forgetting it, such as writing 'System.out.println("Hello")' without the trailing semicolon, results in a compile-time error like 'error: ';' expected'.
  • Follow Standard Naming Conventions: Class names should use PascalCase (e.g., 'StudentRecord'), while method and variable names should use camelCase (e.g., 'calculateTotal'). This improves code readability and aligns with conventions used across the Java ecosystem, making collaboration easier.
  • One Public Class Per File: Keep only one public class per .java file and name the file after that class. This keeps the codebase organized and avoids compilation errors, especially as projects grow larger with multiple interconnected classes.
  • Use Meaningful Comments Sparingly: Add comments to explain 'why' a piece of logic exists rather than 'what' it does (the code itself should be self-explanatory for the 'what'). Over-commenting obvious code adds clutter and maintenance overhead.
  • Set Up a Proper IDE Early: Use an Integrated Development Environment like IntelliJ IDEA, Eclipse, or VS Code with Java extensions from the start. These tools provide real-time error detection, auto-completion, and debugging support that significantly speed up the learning curve.
What does 'Write Once, Run Anywhere' (WORA) mean in the context of Java?
WORA refers to Java's platform independence. When Java source code is compiled using 'javac', it is converted into an intermediate format called bytecode (.class files), not native machine code. This bytecode is not tied to any specific operating system or hardware. Instead, it is executed by the Java Virtual Machine (JVM), and since JVMs exist for Windows, Linux, macOS, and other platforms, the same bytecode file can run unmodified on any device that has a compatible JVM installed.
What is the difference between JDK, JRE, and JVM?
JVM (Java Virtual Machine) is the runtime engine that executes Java bytecode and provides platform independence. JRE (Java Runtime Environment) includes the JVM plus the standard class libraries needed to run Java applications, but it does not include development tools. JDK (Java Development Kit) is a superset of the JRE that additionally includes development tools like the compiler ('javac'), debugger, and other utilities needed to write and compile Java programs. In short: JDK is for developers, JRE is for running apps, and JVM is the core execution engine inside JRE.
Why is the main method declared as 'public static void main(String[] args)'?
Each keyword serves a specific purpose: 'public' allows the JVM (which is external to the class) to access the method from outside the class. 'static' allows the JVM to call the method without creating an instance of the class first, since no object exists yet when the program starts. 'void' indicates the method does not return any value back to the JVM. 'String[] args' allows command-line arguments to be passed into the program at runtime. Changing any part of this exact signature will prevent the JVM from recognizing it as the valid entry point.
Is Java purely object-oriented? Justify your answer.
No, Java is not purely object-oriented because it supports eight primitive data types (int, char, boolean, byte, short, long, float, double) that are not objects, for performance reasons. A purely object-oriented language, like Smalltalk, treats everything, including primitives, as an object. Java compensates for this using wrapper classes (like Integer for int) and a feature called autoboxing/unboxing, which automatically converts between primitives and their corresponding object wrapper types when needed, such as when using Collections that only accept objects.
Write a Java program that declares a class named 'StudentInfo' and prints your name, age, and course on three separate lines using the 'main' method.
public class StudentInfo { public static void main(String[] args) { System.out.println("Name: John Doe"); System.out.println("Age: 21"); System.out.println("Course: Computer Science"); } }
Identify and fix the error in the following code: public class Demo { public void main(String[] args) { System.out.println("Testing") } }
There are two errors: 1) The 'main' method is missing the 'static' keyword, which is required so the JVM can invoke it without creating an object. 2) The println statement is missing a semicolon. Corrected code: public class Demo { public static void main(String[] args) { System.out.println("Testing"); } }
Write a program that accepts a command-line argument and prints it back to the console, along with a message stating how many arguments were passed.
public class ArgsDemo { public static void main(String[] args) { System.out.println("Number of arguments passed: " + args.length); if (args.length > 0) { System.out.println("First argument: " + args[0]); } else { System.out.println("No arguments were passed."); } } }

Java is a robust, platform-independent, object-oriented programming language built around the JVM, which enables the 'Write Once, Run Anywhere' capability. Every Java program requires a class and a properly declared 'main' method as its entry point. Understanding the JDK, JRE, and JVM relationship, along with correct syntax fundamentals like case sensitivity, semicolons, and naming conventions, forms the essential foundation for all further Java learning, from object-oriented programming concepts to enterprise application development.

© 2026 CompileX. Maintained by Aditya Kumar Sharma.