๐Ÿ’ป

Java Programming Basics

Aug 23, 2025

Overview

This lecture provides a comprehensive introduction to Java programming, covering installation, programming basics, data types, operators, control structures, arrays, functions, exceptions, and concludes with a simple mini-project.

Setting Up Java

  • Download and install the Java Development Kit (JDK) from the official Oracle website for your operating system.
  • Download and install an IDE (e.g., IntelliJ IDEA Community Edition) for writing code.
  • Create a new Java project and set up your package and class structure as per standard Java conventions.

Java Program Structure

  • Java code is organized into classes; every program must have a class and a main method: public static void main(String[] args).
  • The file name must match the public class name.
  • Comments in Java are written with // for single-line and /* ... */ for multi-line.

Data Types and Variables

  • Primitive data types: byte, short, int, long, float, double, char, boolean.
  • Non-primitive data types: String, arrays, and objects.
  • Use final keyword to declare constants.
  • Type casting allows conversion between compatible data types (implicit and explicit casting).

Operators

  • Arithmetic operators: +, -, *, /, %.
  • Assignment operator: =, and compound assignments like +=, -=.
  • Increment/Decrement operators: ++, -- (prefix and postfix).
  • Comparison operators: ==, !=, >, <, >=, <=.
  • Logical operators: && (AND), || (OR), ! (NOT).

Input and Output

  • Output: Use System.out.println() and System.out.print().
  • Input: Use Scanner class (Scanner sc = new Scanner(System.in);).

Control Structures

  • Conditional statements: if, else if, else, switch.
  • Loops: for, while, do-while.
  • Use break to exit loops and continue to skip iteration.

Arrays

  • Arrays store multiple values of the same type: int[] arr = new int[3];.
  • Access elements via indices, starting at 0.
  • Two-dimensional arrays represent matrices: int[][] matrix = new int[2][3];.

String Methods

  • Strings are immutable.
  • Common methods: .length(), .charAt(index), .substring(), .replace(), .concat().

Exception Handling

  • Handle exceptions using try-catch blocks to prevent program crash due to runtime errors.

Functions (Methods)

  • Define reusable blocks of code called methods.
  • Syntax: public static void methodName(parameters) { // code }.
  • Methods can return values or be void (no return).

Mini-Project Example

  • Create a random number guessing game using loops, conditionals, and user input.

Key Terms & Definitions

  • JDK (Java Development Kit) โ€” Software package for developing Java applications.
  • IDE (Integrated Development Environment) โ€” Application for writing and testing code efficiently.
  • Main Method โ€” Entry point of a Java application.
  • Primitive Data Type โ€” Basic type such as int, boolean, etc.
  • Array โ€” Data structure to store a fixed-size sequence of elements of the same type.
  • Type Casting โ€” Changing a variable from one type to another.
  • Exception โ€” An error that disrupts program flow, handled with try-catch.
  • Method โ€” Reusable code block that performs a specific task.

Action Items / Next Steps

  • Practice writing and running simple Java programs.
  • Complete the random number guessing mini-project.
  • Review array operations, loops, and control structures.
  • Prepare questions on any unclear topics for the next lecture.