Java Programming Course: A Comprehensive Guide
-By Rahul Chaube
Introduction to Java
Overview
Java is a high-level, object-oriented programming language developed by Sun Microsystems (now Oracle) in 1995. It is designed to be platform-independent and is used for web applications, mobile applications, and enterprise solutions.
Features
- Object-Oriented :Supports encapsulation, inheritance, and polymorphism.
-Platform-Independent: Write Once, Run Anywhere (WORA) — Java programs run on any device with a JVM (Java Virtual Machine).
-Strongly Typed: Enforces strict type checking.
- Automatic Memory Management: Uses garbage collection to manage memory.
2. Setting Up the Environment
Installation
1. Download and Install JDK (Java Development Kit)
— Visit [Oracle’s JDK download page](https://www.oracle.com/java/technologies/javase-downloads.html).
— Download and install the latest JDK version.
2. Set Up Environment Variables
— Windows: Add `JAVA_HOME` to system environment variables and include the `bin` directory in the `PATH`.
— macOS/Linux: Set `JAVA_HOME` in `.bash_profile` or `.bashrc` and update `PATH`.
3. Verify Installation
— Open a terminal or command prompt and type `java -version` and `javac -version`.
3. Java Basics
#### Hello World Program
```java
public class HelloWorld {
public static void main(String[] args) {
System.out.println(“Hello, World!”);
}
}
```
- **`public class HelloWorld`**: Defines a class named `HelloWorld`.
- **`public static void main(String[] args)`**: Entry point of the program.
#### Data Types
- **Primitive Types**: `int`, `float`, `double`, `char`, `boolean`
- **Reference Types**: Arrays, Strings, Objects
**Examples:**
```java
int age = 25;
float height = 5.9f;
double salary = 50000.50;
char grade = ‘A’;
boolean isActive = true;
```
#### Variables and Constants
- **Variable Declaration and Initialization**
```java
int x = 10;
int y = 20;
```
- Constants
```java
final int MAX_VALUE = 100;
```
### 4. Operators
#### Arithmetic Operators
```java
int a = 10, b = 5;
int sum = a + b; // 15
int difference = a — b; // 5
int product = a * b; // 50
int quotient = a / b; // 2
int remainder = a % b; // 0
```
#### Relational Operators
```java
int x = 10, y = 20;
boolean result = (x < y); // true
```
#### Logical Operators
```java
boolean a = true, b = false;
boolean andResult = a && b; // false
boolean orResult = a || b; // true
boolean notResult = !a; // false
```
#### Assignment Operators
```java
int a = 10;
a += 5; // a = 15
a -= 2; // a = 13
a *= 3; // a = 39
a /= 3; // a = 13
```
### 5. Control Flow
#### Conditional Statements
- **If-Else Statement**
```java
int num = 10;
if (num > 0) {
System.out.println(“Positive number”);
} else {
System.out.println(“Non-positive number”);
}
```
- Switch-Case Statement
```java
int day = 3;
switch (day) {
case 1: System.out.println(“Monday”); break;
case 2: System.out.println(“Tuesday”); break;
case 3: System.out.println(“Wednesday”); break;
default: System.out.println(“Invalid day”);
}
```
#### Loops
- **For Loop**
```java
for (int i = 0; i < 5; i++) {
System.out.println(i); // 0 1 2 3 4
}
```
- While Loop
```java
int i = 0;
while (i < 5) {
System.out.println(i); // 0 1 2 3 4
i++;
}
```
- Do-While Loop
```java
int i = 0;
do {
System.out.println(i); // 0 1 2 3 4
i++;
} while (i < 5);
```
### 6. Functions (Methods)
#### Defining Methods
```java
public class Main {
public static void main(String[] args) {
int result = add(10, 20);
System.out.println(“Sum: “ + result);
}
public static int add(int a, int b) {
return a + b;
}
}
```
#### Method Overloading
- Methods with the same name but different parameters.
```java
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
}
```
### 7. Object-Oriented Programming
#### **Classes and Objects**
- **Defining a Class**
```java
public class Person {
String name;
int age;
public void introduce() {
System.out.println(“Hello, my name is “ + name + “ and I am “ + age + “ years old.”);
}
}
```
Creating Objects
- ```java
Person person1 = new Person();
person1.name = “Alice”;
person1.age = 30;
person1.introduce(); // Output: Hello, my name is Alice and I am 30 years old.
```
#### Inheritance
- **Base Class**
```java
public class Animal {
public void eat() {
System.out.println(“This animal eats food.”);
}
}
```
- Derived Class
```java
public class Dog extends Animal {
public void bark() {
System.out.println(“The dog barks.”);
}
}
```
#### Polymorphism
- **Method Overriding**
```java
public class Animal {
public void sound() {
System.out.println(“Animal makes a sound”);
}
}
public class Cat extends Animal {
@Override
public void sound() {
System.out.println(“Cat meows”);
}
}
```
### 8. Interfaces
- **Defining an Interface**
```java
public interface Animal {
void eat();
}
```
- **Implementing an Interface**
```java
public class Dog implements Animal {
public void eat() {
System.out.println(“Dog eats food.”);
}
}
```
### 9. Exception Handling
#### **Try-Catch-Finally**
```java
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println(“Cannot divide by zero.”);
} finally {
System.out.println(“This block always executes.”);
}
```
#### Throwing Exceptions
```java
public class Test {
public static void checkAge(int age) {
if (age < 18) {
throw new ArithmeticException(“Age must be 18 or above.”);
}
}
public static void main(String[] args) {
checkAge(16);
}
}
```
### 10. Collections Framework
#### List Interface
- **ArrayList**
```java
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add(“Apple”);
list.add(“Banana”);
System.out.println(list); // [Apple, Banana]
}
}
```
#### **Map Interface**
- **HashMap**
```java
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put(“Alice”, 25);
map.put(“Bob”, 30);
System.out.println(map); // {Alice=25, Bob=30}
}
}
```
### 11. File I/O
#### Reading from a File
```java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FileReadExample {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader(“example.txt”))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
#### Writing to a File
```java
import java.io.BufferedWriter;
import
java.io.FileWriter;
import java.io.IOException;
public class FileWriteExample {
public static void main(String[] args) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(“example.txt”))) {
bw.write(“Hello, File!”);
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
### 12. Java Standard Libraries
#### Common Libraries
- **`java.lang`**: Core classes (e.g., `String`, `Math`)
- **`java.util`**: Utility classes (e.g., `ArrayList`, `HashMap`)
- **`java.io`**: Input/Output classes (e.g., `File`, `BufferedReader`)
### 13. Advanced Topics
#### Multithreading
- **Creating Threads**
```java
public class MyThread extends Thread {
public void run() {
System.out.println(“Thread is running.”);
}
}
public class Main {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
}
}
```
#### Java Streams API
- **Stream Operations**
```java
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.stream()
.filter(n -> n % 2 == 0)
.forEach(System.out::println); // 2 4
}
}
```
### 14. Best Practices
- **Follow Naming Conventions**: Use meaningful names for classes, methods, and variables.
- **Write Modular Code**: Break down code into methods and classes.
- **Handle Exceptions Gracefully**: Use try-catch blocks to handle errors.
- **Use JavaDocs**: Document code using JavaDoc comments for better maintainability.
### 15. Additional Resources
Official Java Documentation: [Oracle Java Docs](https://docs.oracle.com/en/java/)
- Online Courses: Platforms like Coursera, Udemy, and Codecademy.
- Books: “Effective Java” by Joshua Bloch, “Java: The Complete Reference” by Herbert Schildt.
for more information you can follow me here : https://www.linkedin.com/in/rahulchaube1/
- -RAHUL CHAUBE