Day 1: Introduction to Java and Environment Setup
Java is an object-oriented programming language created by Sun Microsystems in 1995. Under the philosophy of “Write Once, Run Anywhere,” it runs on the JVM (Java Virtual Machine), allowing the same code to execute regardless of the operating system. It is currently one of the most widely used languages in the world, utilized in web servers, Android apps, large-scale enterprise systems, and more.
Verifying JDK Installation
After installing the JDK (Java Development Kit), verify the version in the terminal.
// Run in the terminal
// java -version
// javac -version
// JDK 17 or higher recommended
// If you see output like java version "17.0.x", you're good to go
Your First Java Program
Every Java program must have a main method inside a class. The file name must match the class name exactly.
// HelloJava.java
public class HelloJava {
public static void main(String[] args) {
System.out.println("Hello, welcome to the world of Java!");
System.out.println("Java version: " + System.getProperty("java.version"));
System.out.println("Operating system: " + System.getProperty("os.name"));
}
}
Compilation and Execution Process
Java is a compiled language. The source code (.java) is compiled into bytecode (.class), which is then executed by the JVM.
// Step 1: Compile (in the terminal)
// javac HelloJava.java
// -> HelloJava.class file is generated
// Step 2: Run
// java HelloJava
// -> Outputs "Hello, welcome to the world of Java!"
// Java execution flow:
// .java (source code) -> javac compile -> .class (bytecode) -> JVM execution
Comment Styles
Let’s explore three ways to add comments to your code.
public class CommentExample {
public static void main(String[] args) {
// Single-line comment: starts with two slashes
/*
* Multi-line comment:
* Used when a longer explanation is needed.
*/
/**
* Javadoc comment: Used for generating API documentation.
* @param args command-line arguments
*/
System.out.println("Comments are not executed.");
}
}
Today’s Exercises
-
Self-Introduction Program: Write a program that prints your name, age, and favorite programming language using
System.out.println(). -
System Information Output: Write a program that uses
System.getProperty()to print the Java version, user name (user.name), and current working directory (user.dir). -
ASCII Art: Write a program that uses multiple
System.out.println()calls to print simple ASCII art (e.g., a triangle or heart shape made of stars).