Java Fundamentals

Chapter 1: Introduction to Java

Java is one of the most widely used programming languages in the world. It powers Android apps, enterprise software, web applications, and much more.

Why Java?

Java follows the principle of "write once, run anywhere." This means code written in Java can run on any device that has the Java Virtual Machine (JVM) installed. Java is strongly typed, object-oriented, and has a vast ecosystem of libraries and tools.

Setting Up Java

To start programming in Java, you need to install the Java Development Kit (JDK). Download it from the official Oracle website or use an open-source alternative like OpenJDK.

After installation, verify it by opening your terminal and typing:

java --version javac --version

The first command checks the Java runtime, and the second checks the Java compiler.

Your First Java Program

Create a file named HelloWorld.java and type the following:

public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }

To compile and run this program, open your terminal and type:

javac HelloWorld.java java HelloWorld

Let us break down what each part means. The public class HelloWorld line defines a class named HelloWorld. Every Java program needs at least one class. The public static void main line defines the main method, which is the entry point of every Java program. System.out.println prints text to the console.

Variables and Data Types

Java requires you to declare the type of each variable:

String name = "Saurabh"; int age = 25; double height = 5.8; boolean isStudent = true;

Unlike Python, you must specify the type before the variable name.

Key Takeaways

Java is a strongly typed, object-oriented language. It runs on the JVM, making it platform-independent. Every Java program has a main method as its entry point. Variables must have their types declared explicitly.