Java Introduction and Setup
Java is a popular programming language used for Android apps, banking systems, online shops and big company software. Java code is compiled once and runs on any computer that has Java installed: "write once, run anywhere".
Java has been one of the world's most used languages for over 25 years. In India especially, a huge number of IT jobs - at service companies, banks, insurance and e-commerce - ask for Java. Android apps were traditionally written in Java, and many large back-end systems still are.
How Java runs: you write code in a .java file. The compiler (javac) checks it and turns it into bytecode (a .class file). The Java Virtual Machine (JVM) then runs that bytecode. Because every operating system has its own JVM, the same bytecode runs on Windows, Mac and Linux - that's the "write once, run anywhere" idea.
Java is strict: you must say the type of every variable, and the compiler catches many mistakes before the program even runs. Beginners sometimes find this slower at first, but it makes big programs safer and easier for teams to maintain.
Java 8 was a big update. It added lambda expressions, the Stream API, Optional and a new Date and Time API. You will learn all of them here.
Your first program
Install the JDK 8 (Java Development Kit). Then save this as Hello.java:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello from Java!");
}
}Every Java program lives inside a class, and starts running from the main method. The words public static void look scary at first: public means anyone can call it, static means it belongs to the class (no object needed), and void means it returns nothing. You'll understand each word properly in later lessons - for now, treat this as the standard starting template.
Compile and run it in a terminal:
javac Hello.java // compiles to Hello.class
java Hello // runs it
// Output: Hello from Java!- The file name must match the public class name:
Hello.javaholdsclass Hello. mainis where the program starts.- Every statement ends with a semicolon
;.
- Java code is compiled to bytecode and runs on the JVM, on any system.
- Every program has a class and a
mainmethod where it starts. - Statements end with
;and the file name must match the public class. - Java 8 added lambdas, streams, Optional and a new date/time API.
