Skip to content

Java Programming · Week 1 notes

Hello World

Introduction to Java

What is Java?

Java is a popular, versatile, and object-oriented programming language used for developing a wide range of applications.

Key Features of Java

  • Platform Independence: Write once, run anywhere (WORA)
  • Object-Oriented: Based on the concept of objects containing data and code
  • Simple: Easy to learn and understand
  • Secure: Developed with built-in security features
  • High Performance: Supports multithreading and is highly efficient

Java Development Kit (JDK)

To start programming in Java, you need to install the Java Development Kit (JDK). The JDK includes:

  • Java Runtime Environment (JRE)
  • Compiler (javac)
  • Debugger
  • Other development tools

In the following lessons, we'll guide you through setting up your development environment and writing your first Java program.

Setting Up Java Environment

Setting Up Your Java Development Environment

Before we can start coding, we need to set up our Java development environment. Follow these steps:

1. Download and Install JDK

  1. Visit the official Oracle website or adopt OpenJDK
  2. Download the latest version of JDK for your operating system
  3. Run the installer and follow the installation wizard

2. Set Up Environment Variables

To use Java from any directory in your command prompt or terminal:

  • Add JAVA_HOME to your system variables
  • Add %JAVA_HOME%\bin to your PATH variable

3. Verify Installation

Open a command prompt or terminal and type:

java -version javac -version

If you see version information, your Java environment is set up correctly!

4. Choose an Integrated Development Environment (IDE)

While you can write Java code in any text editor, an IDE can make development easier. Popular choices include:

  • IntelliJ IDEA
  • Eclipse
  • NetBeans

In the next lesson, we'll write our first Java program!

Writing Your First Java Program

Hello, World! in Java

Now that we have our environment set up, let's write our first Java program - the classic "Hello, World!"

The Code

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

Understanding the Code

  • public class HelloWorld: Defines a public class named HelloWorld
  • public static void main(String[] args): The main method, entry point of our program
  • System.out.println("Hello, World!");: Prints the text to the console

Running the Program

  1. Save the file as HelloWorld.java
  2. Open a command prompt or terminal in the directory containing your file
  3. Compile the program: javac HelloWorld.java
  4. Run the program: java HelloWorld

You should see "Hello, World!" printed in your console.

Key Points

  • Java is case-sensitive
  • Every statement ends with a semicolon
  • The class name should match the file name
  • The main method is required for every Java program

Congratulations! You've written and run your first Java program.

Java Syntax and Structure

Understanding Java Syntax and Structure

Now that we've written our first program, let's dive deeper into Java's syntax and structure.

Basic Syntax Rules

  • Java is case-sensitive
  • Class names should start with an uppercase letter
  • Method names should start with a lowercase letter
  • File name must match the class name (when the class is public)

Program Structure

// Package declaration (optional) package com.example; // Import statements (optional) import java.util.Scanner; // Class declaration public class MyProgram { // Field declarations (optional) private int myField; // Constructor (optional) public MyProgram() { // Constructor code } // Method declarations public void myMethod() { // Method code } // Main method public static void main(String[] args) { // Program execution starts here } }

Comments in Java

Java supports three types of comments:

  • Single-line comments: // This is a single-line comment
  • Multi-line comments: /* This is a multi-line comment */
  • Documentation comments: /** This is a documentation comment */

Understanding these basic syntax rules and program structure will help you write clean and organized Java code.