Java

Java is a widely-used and versatile programming language known for its platform independence, robustness, and wide adoption in enterprise systems. It follows the principle of "Write Once, Run Anywhere" (WORA), meaning that Java programs can run on any platform with a Java Virtual Machine (JVM). Java's object-oriented nature and extensive standard library provide developers with a powerful toolkit for building a wide range of applications, from desktop software to web applications and Android mobile apps. Java offers features like automatic memory management through garbage collection, exception handling, multithreading support, and strong type checking.

Module 1: Introduction to Java

Introduction to Java

Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is a general-purpose programming language intended to let application developers write once, run anywhere (WORA), meaning that compiled Java code can run on all platforms that support Java without the need for recompilation.

Java Syntax Basics

Java syntax is the set of rules defining how a Java program is written and interpreted. The syntax is mostly derived from C++ and C. Unlike these languages, which are platform dependent, Java is a write once, run anywhere language.

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

Variables and Types

Java is a statically-typed language, which means that all variables must first be declared before they can be used. This involves stating the variable's type and name.

int myNumber = 5;
String myString = 'Hello';

Operators

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators, Relational Operators, Bitwise Operators, Logical Operators, and Assignment Operators.

int sum = 10 + 20;
boolean isTrue = false;
boolean negate = !isTrue;
int bitwiseComplement = ~10;
boolean isEqual = sum == 30;

Module 2: Control Flow Statements

If-Else

The 'if'-'else' is the most simple decision-making statement. It is used to decide to do something based on a condition.

int testscore = 76;
char grade;

if (testscore >= 90) {
	grade = 'A';
} else if (testscore >= 80) {
	grade = 'B';
} else if (testscore >= 70) {
	grade = 'C';
} else if (testscore >= 60) {
	grade = 'D';
} else {
	grade = 'F';
}

Switch-Case

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.

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

For Loop

The for-loop provides a concise way of writing the loop structure. Unlike a while loop, a for statement consumes the initialization, condition, and increment/decrement in one line, providing a shorter, easy-to-debug structure of looping.

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

While and Do-While Loop

The while loop loops through a block of code as long as a specified condition is true. The do/while loop is a variant of the while loop, which tests the condition at the end of the loop.

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

do {
	System.out.println(i);
	i--;
} while(i > 0);

Module 3: Object-Oriented Programming in Java

Classes and Objects

Classes in Java, the blueprint from which individual objects are created. An object is an instance of a class.

public class Car {
	String brand;
	int year;
	public Car(String brand, int year) {
		this.brand = brand;
		this.year = year;
	}
}

Inheritance

Inheritance in Java is a mechanism where one object acquires all the properties and behaviors of a parent object.

public class Vehicle {
	int maxSpeed;
}

public class Car extends Vehicle {
	int numberOfSeats;
}

Polymorphism

Polymorphism allows us to perform a single action in different ways. So, polymorphism allows you to define one interface and have multiple implementations.

public class Animal {
	public void sound() {
		System.out.println('The animal makes a sound');
	}
}

public class Pig extends Animal {
	public void sound() {
		System.out.println('The pig says: wee wee');
	}
}

Encapsulation

Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit.

public class Employee {
	private String name;
	
	public String getName() {
		return name;
	}
	
	public void setName(String newName) {
		this.name = newName;
	}
}

Abstraction

Abstraction in Java is the process of hiding certain details and showing only essential information to the user. Abstraction can be achieved with either abstract classes or interfaces (which are a more advanced topic).

public abstract class Animal {
	public abstract void sound();
}

public class Dog extends Animal {
	public void sound() {
		System.out.println('The dog says: bow wow');
	}
}

Module 4: Java Collections

Java Collections Framework

The Java Collections Framework is a set of classes and interfaces that implements commonly reusable collection data structures. This framework defines several classes and interfaces to represent a group of objects as a single unit.

Lists

Java List is an ordered collection. Java List allows duplicates while Set doesn't allow duplicate elements. All the elements can be accessed using the index position. Java List provides control over the position where you can insert an element.

List<String> list = new ArrayList<String>();
list.add('Java');
list.add('Python');
list.add('JavaScript');

Sets

Java Set interface is a member of the Java Collections Framework. It is used to store no duplicate and unordered elements. It extends the Collection interface. It is an unordered collection of objects, stored in a random order.

Set<String> set = new HashSet<String>();
set.add('Java');
set.add('Python');
set.add('JavaScript');
set.add('Java');

Maps

Java Map interface is part of collections framework. It provides the operation to store and retrieve the elements on the basis of the keys. The Java Map interface is not a subtype of the Collection interface.

Map<String, Integer> map = new HashMap<String, Integer>();
map.put('Java', 10);
map.put('Python', 20);
map.put('JavaScript', 30);

Module 5: Exception Handling

Java Exceptions

An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions. When an error occurs within a method, the method creates an object and hands it off to the runtime system.

try {
	int[] myNumbers = {1, 2, 3};
	System.out.println(myNumbers[10]);
} catch (Exception e) {
	System.out.println('Something went wrong.');
} finally {
	System.out.println('The 'try catch' is finished.');
}

Throwing Exceptions

The throw keyword in Java is used to explicitly throw an exception from a method or any block of code. We can throw either checked or unchecked exception. The throw keyword is mainly used to throw custom exceptions.

public void checkAge(int age) {
	if (age < 18) {
		throw new ArithmeticException('Access denied - You must be at least 18 years old.');
	}
	else {
		System.out.println('Access granted - You are old enough!');
	}
}

Module 6: Java I/O and File Handling

Java I/O Streams

Java I/O (Input and Output) is used to process the input and produce the output. Java uses the concept of a stream to make I/O operation fast. The java.io package contains all the classes required for input and output operations.

FileInputStream fis = null;
try {
	fis = new FileInputStream('myfile.txt');
	int i = fis.read();
	System.out.println((char)i);
} catch(Exception e) {
	System.out.println(e);
}

File Handling

Java provides several API (also known as Java I/O API) to read and write files since its initial releases. The java.io package contains all the classes required for input and output operations. We can perform file handling in Java by Java I/O API.

File myFile = new File('filename.txt');
if (myFile.createNewFile()) {
	System.out.println('File created: ' + myFile.getName());
} else {
	System.out.println('File already exists.');
}