Java Basics

Installation, variables, operators, and basic syntax

1. Introduction & Features

Java is a platform-independent, object-oriented programming language. Write once, run anywhere.

Platform Independent

Runs on any OS with JVM

Object-Oriented

Classes, objects, inheritance

Secure

Built-in security features

Memory Management

Automatic garbage collection

2. Installation & Setup

Download JDK

Get the latest JDK from Oracle or OpenJDK

Set JAVA_HOME

Point to JDK installation directory

Update PATH

Add JDK/bin to system PATH

Verify Installation

Run: java -version

# Check Java version
java -version

# Check compiler version
javac -version

# Expected output:
java version "17.0.2" 2022-01-18
Java(TM) SE Runtime Environment...

3. Your First Java Program

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

Compile

javac HelloWorld.java

Run

java HelloWorld

Key Points

  • Class name must match filename
  • main() method is the entry point
  • Java is case-sensitive
  • Statements end with semicolon

4. Variables & Data Types

byte

8-bit

-128 to 127

short

16-bit

-32,768 to 32,767

int

32-bit

-2^31 to 2^31-1

long

64-bit

-2^63 to 2^63-1

float

32-bit

IEEE 754

double

64-bit

IEEE 754

char

16-bit

Unicode

boolean

1-bit

true/false

// Integer types
int age = 25;
long population = 8000000000L;

// Floating point
double price = 99.99;
float discount = 0.15f;

// Character and boolean
char grade = 'A';
boolean isActive = true;

// String (reference type)
String name = "Java";

Variable Rules

Names must start with letter, $, or _. Cannot use Java keywords. Use camelCase convention.

5. Operators

Arithmetic

  • (add)
  • (sub)
  • (mul)

/ (div)

% (mod)

++

—

Comparison

==

!=

<

=

<=

Logical

&& (AND)

|| (OR)

! (NOT)

Assignment

=

+=

-=

*=

/=

int a = 10, b = 5;

// Arithmetic
int sum = a + b;        // 15
int remainder = a % b;  // 0

// Comparison
boolean isGreater = a > b;  // true

// Logical
boolean result = (a > 5) && (b < 10);  // true

// Assignment
a += 5;  // a = a + 5, now a = 15

6. Quick Examples

Calculator

int num1 = 15, num2 = 4;
System.out.println(num1 + num2);  // 19
System.out.println(num1 - num2);  // 11
System.out.println(num1 * num2);  // 60
System.out.println(num1 / num2);  // 3

String Operations

String firstName = "John";
String lastName = "Doe";
String fullName = firstName + " " + lastName;
System.out.println(fullName);  // John Doe
System.out.println(fullName.length());  // 8

Type Conversion

// Implicit (widening)
int i = 100;
double d = i;  // 100.0

// Explicit (narrowing)
double price = 99.99;
int rounded = (int) price;  // 99

User Input

import java.util.Scanner;

Scanner input = new Scanner(System.in);
System.out.print("Enter name: ");
String name = input.nextLine();
System.out.println("Hello " + name);

Next Steps

Learn Control Structures (if, loops)

Understand Arrays & Collections

Master Object-Oriented Programming

Practice Error Handling