Back to Module
0

Introduction to Java

Learn the basics of the Java programming language and its ecosystem

Introduction to Java

Welcome to your first lesson in Java programming! In this lesson, we'll explore what Java is, its history, features, and how to set up your development environment.

What is Java?

Java is a high-level, class-based, object-oriented programming language that was developed by Sun Microsystems (now owned by Oracle) in the mid-1990s. It was designed with the goal of allowing developers to "write once, run anywhere" (WORA), meaning that compiled Java code can run on all platforms that support Java without the need for recompilation.

Key Features of Java

  • Platform Independence: Java programs are compiled into bytecode that can run on any device with a Java Virtual Machine (JVM).
  • Object-Oriented: Java follows the object-oriented programming paradigm, focusing on creating objects that contain data and methods.
  • Simple and Familiar: Java was designed to be easy to learn, especially for programmers familiar with C and C++.
  • Robust and Secure: Java provides automatic memory management, exception handling, and strict compile-time type checking.
  • Multithreaded: Java supports concurrent programming, allowing you to write programs that can perform multiple tasks simultaneously.

Setting Up Your Development Environment

To start coding in Java, you'll need to set up a development environment:

  1. Install the Java Development Kit (JDK):
    • Visit Oracle's JDK download page or use OpenJDK
    • Download and install the version appropriate for your operating system
    • Verify installation by opening a terminal or command prompt and typing: java -version
  2. Choose an Integrated Development Environment (IDE):
    • IntelliJ IDEA: A powerful IDE with a robust set of features
    • Eclipse: A popular open-source IDE with extensive plugin support
    • VS Code: A lightweight editor with Java extensions
    • NetBeans: An IDE that simplifies Java development with drag-and-drop features
  3. Create Your First Java Program:
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, Java World!");
    }
}

Save this code in a file named HelloWorld.java, compile it with javac HelloWorld.java, and run it with java HelloWorld.

Java Syntax Basics

Variables and Data Types

Java has two categories of data types:

  1. Primitive Types:
    • byte: 8-bit integer
    • short: 16-bit integer
    • int: 32-bit integer
    • long: 64-bit integer
    • float: 32-bit floating-point
    • double: 64-bit floating-point
    • char: 16-bit Unicode character
    • boolean: true or false
  2. Reference Types:
    • Objects
    • Arrays
    • Strings

Example:

int age = 25;
double salary = 50000.50;
char grade = 'A';
boolean isEmployed = true;
String name = "John Doe";

Basic Operators

  • Arithmetic: +, -, *, /, %
  • Assignment: =, +=, -=, *=, /=
  • Comparison: ==, !=, >, <, >=, <=
  • Logical: &&, ||, !

Control Flow

  • If-Else Statements:
    if (condition) {
        // code block
    } else if (anotherCondition) {
        // code block
    } else {
        // code block
    }
    
  • Switch Statements:
    switch (variable) {
        case value1:
            // code block
            break;
        case value2:
            // code block
            break;
        default:
            // code block
    }
    
  • Loops:
    // For loop
    for (int i = 0; i < 5; i++) {
        System.out.println(i);
    }
    
    // While loop
    int j = 0;
    while (j < 5) {
        System.out.println(j);
        j++;
    }
    
    // Do-while loop
    int k = 0;
    do {
        System.out.println(k);
        k++;
    } while (k < 5);
    

Practice Exercise

Create a simple Java program that:

  1. Declares variables of different data types
  2. Performs basic arithmetic operations
  3. Uses if-else statements to make decisions
  4. Implements a for loop to iterate through numbers

Summary

In this lesson, you've learned:

  • What Java is and its key features
  • How to set up a Java development environment
  • Basic Java syntax, including variables, operators, and control flow statements

In the next module, we'll dive into Object-Oriented Programming concepts in Java, starting with classes and objects.

© 2025 Noruj.com. All rights reserved.

Made with
by the Noruj Team