Java Basics

Table of Contents

Basic Syntax

Program Structure:


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

Data Types and Variables

Primitive Types


// Numeric types
byte b = 127;
short s = 32767;
int i = 2147483647;
long l = 9223372036854775807L;
float f = 3.14f;
double d = 3.14159;

// Character type
char c = 'A';

// Boolean type
boolean bool = true;
    

Reference Types


// String
String text = "Hello, World!";

// Arrays
int[] numbers = new int[5];

// Objects
Integer number = new Integer(42);
StringBuilder builder = new StringBuilder();

// Null
String nullString = null;
    

Control Flow

Conditional Statements


// If statement
if (age >= 18) {
    System.out.println("Adult");
} else if (age >= 13) {
    System.out.println("Teenager");
} else {
    System.out.println("Child");
}

// Switch statement
switch (day) {
    case 1:
        System.out.println("Monday");
        break;
    case 2:
        System.out.println("Tuesday");
        break;
    default:
        System.out.println("Other day");
}
    

Loops


// For loop
for (int i = 0; i < 5; i++) {
    System.out.println(i);
}

// Enhanced for loop
for (String item : items) {
    System.out.println(item);
}

// While loop
while (condition) {
    // Loop body
}

// Do-while loop
do {
    // Loop body
} while (condition);
    

Arrays and Collections

Array Operations:


// Array declaration and initialization
int[] numbers = new int[5];
int[] primes = {2, 3, 5, 7, 11};

// Multi-dimensional arrays
int[][] matrix = new int[3][3];
matrix[0][0] = 1;

// Array methods
Arrays.sort(numbers);
int index = Arrays.binarySearch(numbers, 5);
int[] copy = Arrays.copyOf(numbers, numbers.length);
        

Collections Framework


// ArrayList
List<String> list = new ArrayList<>();
list.add("First");
list.add("Second");

// HashSet
Set<Integer> set = new HashSet<>();
set.add(1);
set.add(2);

// HashMap
Map<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);
    

Methods

Method Declaration:


// Basic method
public void greet() {
    System.out.println("Hello!");
}

// Method with parameters
public int add(int a, int b) {
    return a + b;
}

// Method overloading
public void print(String text) {
    System.out.println(text);
}

public void print(int number) {
    System.out.println(number);
}
        

Classes and Objects

Class Definition


public class Person {
    // Fields
    private String name;
    private int age;

    // Constructor
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Getters and Setters
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    // Method
    public void introduce() {
        System.out.println("Hi, I'm " + name);
    }
}
    

Object Creation and Usage


// Creating objects
Person person = new Person("John", 30);

// Using objects
person.introduce();
String name = person.getName();
person.setName("Jane");
    

Packages and Imports

Package Structure:


// Package declaration
package com.example.myapp;

// Imports
import java.util.List;
import java.util.ArrayList;
import java.time.LocalDate;

// Static imports
import static java.lang.Math.PI;
import static java.lang.System.out;
        

Common Packages:

Related Resources