💻

Java Basics Overview

Jun 22, 2025

Overview

This lecture introduces Java programming from installation to basic syntax, variables, user input, expressions, GUI basics, useful math methods, and generating random values.

Getting Started with Java

  • Java is a top programming language used in enterprise, Android apps, and more.
  • Computers read machine code (binary), but humans write source code.
  • Java source files end with .java; they are compiled to bytecode (.class), making them cross-platform.
  • The JVM (Java Virtual Machine) translates bytecode to machine code.
  • Download the JDK (Java Development Kit) which includes the JVM and JRE (Java Runtime Environment).
  • Use an IDE (recommended: Eclipse or IntelliJ IDEA) for easier coding, compiling, and running.

Writing and Running Programs

  • Create a Java project, then add a class (e.g., Main) containing the main method.
  • The main method: public static void main(String[] args) { } is the entry point for every Java program.
  • Use System.out.print() and System.out.println() to output text to the console.
  • println adds a new line after printing, while print does not.
  • Escape sequences (e.g., \n for a new line, \t for tab, \" for double quotes, \\ for backslash) format output.
  • Comments: // for single-line, /* ... */ for multi-line.

Variables and Data Types

  • Variables store values; their type must be declared.
  • Eight primitive types: boolean, byte, short, int, long, float, double, char.
  • String is a reference type for text.
  • Declaration: int x; Assignment: x = 5; Initialization: int x = 5;
  • Use long (add L at the end) for very large integers, float (add f at the end) for decimals, double for more precision.
  • Characters use single quotes, e.g., char letter = 'A';, strings use double quotes.

Swapping Variables

  • Swapping variable values requires a temporary variable:
    • temp = x; x = y; y = temp;

User Input with Scanner

  • Import the Scanner: import java.util.Scanner;
  • Create a Scanner: Scanner scanner = new Scanner(System.in);
  • Read input: scanner.nextLine() for strings, scanner.nextInt() for integers, scanner.nextDouble() for decimals.
  • After reading numbers, use an extra scanner.nextLine() to consume the leftover newline.

Expressions and Operators

  • Expressions combine operands (variables/values) and operators (+, -, *, /, %).
  • Increment: x++; Decrement: x--;
  • Integer division truncates decimals (e.g., 10/3 gives 3).
  • Cast to double to get precise results (e.g., double result = (double)x / y;).*

Basic GUI Applications with JOptionPane

  • Import: import javax.swing.JOptionPane;
  • Show input dialog: JOptionPane.showInputDialog("Enter your name");
  • Show message dialog: JOptionPane.showMessageDialog(null, "Hello " + name);
  • Convert input for numbers: Integer.parseInt(inputStr) or Double.parseDouble(inputStr).

Useful Math Methods

  • Use Math.max(a, b), Math.min(a, b), Math.abs(x), Math.sqrt(x), Math.round(x), Math.ceil(x), Math.floor(x).
  • Example: Find hypotenuse: z = Math.sqrt(x*x + y*y);

Generating Random Values

  • Import: import java.util.Random;
  • Create a Random object: Random random = new Random();
  • Generate random int: random.nextInt(n) (returns 0 to n-1).
  • Generate random double: random.nextDouble() (returns 0 to 1).
  • Generate random boolean: random.nextBoolean().

Key Terms & Definitions

  • Source Code — Human-readable program text (.java).
  • Bytecode — Compiled cross-platform code (.class) for the JVM.
  • JDK — Java Development Kit: tools for coding, compiling, running Java.
  • JVM — Java Virtual Machine: runs bytecode on any machine.
  • IDE — Integrated Development Environment: coding tool (e.g., Eclipse).
  • Primitive Data Type — Basic data type (int, double, boolean, etc.).
  • Reference Data Type — Stores addresses; e.g., Strings and objects.
  • Scanner — Class used to accept user input.
  • Expression — Combination of operands and operators.
  • JOptionPane — GUI class for dialog boxes.
  • Random — Class for generating pseudo-random numbers.
  • Escape Sequence — Special character in strings (e.g., \n, \t).

Action Items / Next Steps

  • Download and install the JDK and an IDE (Eclipse or IntelliJ).
  • Practice creating Java projects, classes, and basic output.
  • Experiment with variables, user input, and simple expressions.
  • Try swapping variables and using math/random classes.
  • Attempt to build a basic GUI using JOptionPane.
  • Complete assignment: code to swap two variables and post in the comments.