Java is a popular, high-level, object-oriented programming language created in 1995 by James Gosling at Sun Microsystems (which was later acquired by Oracle).
Java is now owned by Oracle, and it's estimated that more than 3 billion devices run Java.
- Mobile applications (especially Android apps)
- Desktop applications
- Web applications
- Web servers and application servers
- Games
- Database connections
- And much, much more!
- Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
- It is one of the most popular programming languages in the world
- It has a large demand in the current job market
- It is easy to learn and simple to use
- It is open-source and free
- It is secure, fast, and powerful
- It has huge community support (tens of millions of developers)
- Java is an object-oriented language which gives a clear structure to programs and allows code to be reused, lowering development costs
- As Java is close to C++ and C#, it makes it easy for programmers to switch to Java or vice versa
When you are finished with this tutorial, you will be able to:
- Write basic Java programs
- Create real-life examples
💡 No prior programming experience is necessary to begin learning Java!
Java is a powerful, platform-independent programming language. Here's how to get started writing and running Java code on your machine.
To compile and run Java programs, you need to install the JDK (Java Development Kit).
- Visit: https://www.oracle.com/java/technologies/javase-downloads.html
- Download and install the JDK for your operating system.
- After installation, verify it by running in your terminal or command prompt:
java -version
javac -versionThe Java compilation process converts human-readable Java source code into machine-executable bytecode. Below is a step-by-step explanation of how Java code is compiled and executed.
- Java code is written in plain text files with a
.javaextension (e.g.,MyProgram.java). - The code contains classes, methods, and other Java constructs following the language's syntax.
- The Java compiler,
javac, processes the.javafile. - It checks for syntax errors and translates the source code into bytecode, producing a
.classfile (e.g.,MyProgram.class). - Bytecode is a platform-independent intermediate representation of the program.
- The
.classfile contains bytecode, which is a set of instructions for the Java Virtual Machine (JVM). - Bytecode is portable, meaning it can run on any device with a compatible JVM, regardless of the underlying hardware or operating system.
- When the program is executed (using the
javacommand), the JVM loads the.classfile into memory. - The Class Loader subsystem handles loading, linking, and initializing the classes.
- The JVM verifies the bytecode to ensure it is valid and safe (e.g., no invalid operations or security violations).
- This step prevents corrupted or malicious code from running.
- The JVM interprets or compiles the bytecode into native machine code using the Just-In-Time (JIT) compiler.
- The JIT compiler optimizes the bytecode for the specific hardware, improving performance.
- The program is then executed, producing the desired output.
- Java Compiler (javac): Converts source code to bytecode.
- Java Virtual Machine (JVM): Executes bytecode, providing platform independence.
- Just-In-Time (JIT) Compiler: Optimizes bytecode to native code at runtime.
- Write a Java program:
public class MyProgram { public static void main(String[] args) { System.out.println("Hello, World!"); } }
This guide introduces the basic syntax of Java, using a simple "Hello World" program as an example. It explains the structure of a Java program, key components, and common pitfalls for beginners. Example: Hello World Program Below is a basic Java program that prints "Hello World" to the screen:
public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}Hello World- Class Declaration
Every Java program requires a class: All executable code must reside inside a class. Class name rules: The class name (e.g., Main) should start with an uppercase letter (convention). The file name must match the class name exactly and end with .java (e.g., Main.java).
Case sensitivity: Java is case-sensitive, so Main and main are treated as different names.
Example:
public class Main { // Class name matches file name: Main.java
// Code goes here
}Note: Save the file with the exact class name (e.g., Main.java) to avoid compilation errors. 2. The main Method
The main method is the entry point of every Java program. It must have the following signature:public static void main(String[] args)
Explanation of keywords: public: The method is accessible from outside the class. static: The method belongs to the class, not an instance of it. void: The method does not return a value. String[] args: An array of String arguments passed to the program (useful for command-line inputs).
Any code inside the main method's curly braces {} is executed when the program runs.
Example:
public static void main(String[] args) {
// Code here runs when the program starts
}- Printing Output with System.out.println()
The System.out.println() method prints text to the console, followed by a new line. Breakdown: System: A built-in Java class that provides access to system resources. out: A static member of System, representing the output stream (usually the console). println(): A method that prints a line of text and adds a newline character.
Syntax:System.out.println("Text to print");Each statement in Java must end with a semicolon (;).
Example:
System.out.println("Hello World"); // Prints "Hello World" and moves to the next lineNote: Use System.out.print() if you want to print without a newline. 4. Code Blocks and Syntax Rules
Curly braces {}: Define the start and end of a block of code (e.g., class or method body).
Semicolons (;): End each statement to separate it from the next.
Indentation: While not required, indenting code (e.g., 4 spaces or 1 tab) improves readability.
Running the Program
Save the file: Ensure the file name matches the class name (e.g., Main.java). Compile the code: Use the command javac Main.java to generate a Main.class file. Run the program: Use the command java Main to execute the program. Prerequisite: Ensure Java is installed (see Java Installation Guide).
Common Errors to Avoid
Mismatched file and class names: The file name must match the public class name (e.g., Main.java for public class Main). Missing semicolon: Forgetting ; after a statement causes a compilation error. Case sensitivity issues: Ensure correct capitalization (e.g., Main vs. main). Incorrect main method signature: The main method must be exactly public static void main(String[] args).
Notes
Java is a case-sensitive language, so always check capitalization. The main method is mandatory for standalone programs. Use meaningful class names that reflect the program's purpose. Practice proper indentation to make your code easier to read.