Definition

A class is a blueprint for creating objects in Java.

Purpose

Define the structure and behavior of objects.

General Syntax

class ClassName { // fields and methods }

Example

class Student { String name; int age; }

Declares a Student class with name and age fields.

Definition

The main method is the entry point of any Java application.

Purpose

JVM calls the main method to start program execution.

General Syntax

public static void main(String[] args) { // code }

Example

public static void main(String[] args) { System.out.println("Hello"); }

The main method prints 'Hello' to the console.

Definition

Conditional statement that executes different code blocks based on a boolean condition.

Purpose

Make decisions in program flow.

General Syntax

if (condition) { // code } else { // code }

Example

int x = 10; if (x > 5) { System.out.println("Greater"); } else { System.out.println("Smaller"); }

Since x is 10 which is greater than 5, it prints 'Greater'.

Definition

A for loop repeats a block of code a fixed number of times.

Purpose

Iterate over a range of values or array elements.

General Syntax

for (initialization; condition; update) { // loop body }

Example

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

Prints numbers 0 through 4, each on a new line.

Definition

An array stores multiple values of the same type in a single variable.

Purpose

Store and access collections of data efficiently.

General Syntax

dataType[] arrayName = new dataType[size];

Example

int[] numbers = new int[5]; numbers[0] = 10;

Creates an integer array of size 5 and assigns 10 to the first element.

Definition

A method is a block of code that performs a specific task and can return a value.

Purpose

Encapsulate reusable functionality.

General Syntax

returnType methodName(parameters) { // method body return value; }

Example

int add(int a, int b) { return a + b; }

Defines a method that takes two integers and returns their sum.

Definition

A constructor initializes objects when they are created.

Purpose

Set initial values for object attributes.

General Syntax

class ClassName { ClassName() { // initialization } }

Example

class Student { String name; Student(String n) { name = n; } }

Constructor takes a name parameter and assigns it to the name field.

Definition

Inheritance allows a class to inherit properties and methods from another class.

Purpose

Reuse code and establish hierarchical relationships.

General Syntax

class ChildClass extends ParentClass { // additional fields and methods }

Example

class Animal { void eat() { System.out.println("Eating"); } } class Dog extends Animal { void bark() { System.out.println("Barking"); } }

Dog inherits the eat() method from Animal and adds its own bark() method.

Definition

Try-catch handles exceptions that may occur during program execution.

Purpose

Prevent program crashes by gracefully handling errors.

General Syntax

try { // risky code } catch (ExceptionType e) { // handle exception }

Example

try { int result = 10 / 0; } catch (ArithmeticException e) { System.out.println("Cannot divide by zero"); }

The try block attempts division by zero, which throws an exception caught by the catch block.

Definition

ArrayList is a resizable array implementation of the List interface.

Purpose

Store dynamic collections of elements that can grow or shrink.

General Syntax

ArrayList<Type> list = new ArrayList<>(); list.add(element);

Example

ArrayList<String> names = new ArrayList<>(); names.add("Alice"); names.add("Bob"); System.out.println(names.get(0));

Creates an ArrayList, adds two names, and prints the first one.

Definition

A while loop repeatedly executes a block of code as long as a condition is true.

Purpose

Repeat code when the number of iterations is not known in advance.

General Syntax

while (condition) { // loop body }

Example

int i = 0; while (i < 3) { System.out.println(i); i++; }

Prints 0, 1, 2. The loop stops when i becomes 3.

Definition

Switch executes one of many code blocks based on the value of an expression.

Purpose

Replace multiple if-else statements for cleaner code.

General Syntax

switch (expression) { case value1: // code; break; case value2: // code; break; default: // code; }

Example

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

Since day is 2, it prints 'Tuesday'.

Definition

An interface defines abstract methods that implementing classes must define.

Purpose

Achieve abstraction and multiple inheritance in Java.

General Syntax

interface InterfaceName { void methodName(); } class MyClass implements InterfaceName { public void methodName() { } }

Example

interface Drawable { void draw(); } class Circle implements Drawable { public void draw() { System.out.println("Drawing circle"); } }

Circle implements the Drawable interface and provides the draw() method.

Definition

String class provides methods like length(), charAt(), substring() for string manipulation.

Purpose

Manipulate and analyze text data.

General Syntax

string.length(); string.charAt(index); string.substring(start, end);

Example

String s = "Hello"; System.out.println(s.length()); System.out.println(s.charAt(0));

Prints 5 (length) and 'H' (first character).

Definition

File handling in Java uses classes like FileReader, BufferedReader to read and write files.

Purpose

Read from and write to external files for data persistence.

General Syntax

FileReader fr = new FileReader("file.txt"); BufferedReader br = new BufferedReader(fr);

Example

BufferedReader br = new BufferedReader(new FileReader("data.txt")); String line = br.readLine(); br.close();

Opens data.txt, reads the first line, and closes the file.

← Back to Java