Complete Java Tutorial
Complete Java Tutorial
Introduction to Java
Java is a high-level, object-oriented programming language. It was developed by Sun Microsystems
(now owned by Oracle) in 1995. Java is known for its "Write Once, Run Anywhere" feature, meaning
compiled Java code can run on any platform with a Java Virtual Machine (JVM).
Key Features:
- Platform Independent
- Object-Oriented
Applications of Java:
- Web applications
- Enterprise systems
- Scientific applications
1. Download the Java Development Kit (JDK) from the Oracle website or OpenJDK.
2. Install an Integrated Development Environment (IDE) such as IntelliJ IDEA, Eclipse, or VS Code.
```
java -version
javac -version
```
```java
System.out.println("Hello, World!");
```
Java Basics
Java programs are structured into classes and methods. Below are the basics:
Data Types:
Variables:
```java
```
Operators:
- Arithmetic, Logical, Relational, Assignment, Bitwise, etc.
Example:
```java
int a = 5, b = 10;
```
Control Statements:
Example:
```java
System.out.println(i);
```
1. **Encapsulation**:
```java
class Person {
this.name = name;
```
2. **Inheritance**:
```java
class Animal {
void eat() {
void bark() {
System.out.println("Dog barks.");
```
3. **Polymorphism**:
class Animal {
void sound() {
@Override
void sound() {
System.out.println("Bark!");
```
4. **Abstraction**:
```java
void draw() {
System.out.println("Drawing a Circle.");
}
```
Exception Handling
Java provides robust mechanisms to handle runtime errors:
Using try-catch-finally:
```java
try {
} catch (ArithmeticException e) {
} finally {
System.out.println("Execution finished.");
```
Advanced Topics
1. **Collections Framework**:
Example:
```java
import java.util.ArrayList;
list.add("Java");
list.add("Python");
System.out.println(list);
```
2. **Multithreading**:
Example:
```java
System.out.println("Thread is running.");
t1.start();
```
3. **File Handling**:
```java
import java.io.FileWriter;
public class FileExample {
try {
writer.close();
} catch (Exception e) {
e.printStackTrace();
```