0% found this document useful (0 votes)
5 views8 pages

Complete Java Tutorial

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
5 views8 pages

Complete Java Tutorial

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

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

- Secure and Robust

- Multithreaded and Concurrent

Applications of Java:

- Web applications

- Mobile development (Android apps)

- Enterprise systems

- Scientific applications

Setting Up the Environment


To get started with Java, follow these steps:

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.

3. Verify installation by opening a terminal/command prompt and typing:

```

java -version

javac -version
```

4. Write and run your first program:

```java

public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello, World!");

```

Java Basics
Java programs are structured into classes and methods. Below are the basics:

Data Types:

- Primitive: int, double, char, boolean, etc.

- Non-Primitive: Strings, Arrays, Classes.

Variables:

- Declaration and initialization example:

```java

int age = 25;

double salary = 50000.75;

boolean isActive = true;

String name = "John";

```

Operators:
- Arithmetic, Logical, Relational, Assignment, Bitwise, etc.

Example:

```java

int a = 5, b = 10;

System.out.println(a + b); // Output: 15

```

Control Statements:

- Conditional: if, else, switch

- Loops: for, while, do-while

Example:

```java

for (int i = 0; i < 5; i++) {

System.out.println(i);

```

Object-Oriented Programming (OOP)


Java is based on OOP principles:

1. **Encapsulation**:

- Wrapping data and methods in a single unit.

```java

class Person {

private String name;

public String getName() {


return name;

public void setName(String name) {

this.name = name;

```

2. **Inheritance**:

- One class inherits properties of another.

```java

class Animal {

void eat() {

System.out.println("This animal eats food.");

class Dog extends Animal {

void bark() {

System.out.println("Dog barks.");

```

3. **Polymorphism**:

- One name, multiple implementations.


```java

class Animal {

void sound() {

System.out.println("This is an animal sound.");

class Dog extends Animal {

@Override

void sound() {

System.out.println("Bark!");

```

4. **Abstraction**:

- Hiding implementation details.

```java

abstract class Shape {

abstract void draw();

class Circle extends Shape {

void draw() {

System.out.println("Drawing a Circle.");

}
```

Exception Handling
Java provides robust mechanisms to handle runtime errors:

Using try-catch-finally:

```java

try {

int result = 10 / 0; // This will throw ArithmeticException

} catch (ArithmeticException e) {

System.out.println("Cannot divide by zero.");

} finally {

System.out.println("Execution finished.");

```

Advanced Topics
1. **Collections Framework**:

- Provides data structures such as ArrayList, HashMap, and HashSet.

Example:

```java

import java.util.ArrayList;

public class Example {

public static void main(String[] args) {

ArrayList<String> list = new ArrayList<>();

list.add("Java");

list.add("Python");
System.out.println(list);

```

2. **Multithreading**:

- Enables simultaneous execution of threads.

Example:

```java

class MyThread extends Thread {

public void run() {

System.out.println("Thread is running.");

public class Main {

public static void main(String[] args) {

MyThread t1 = new MyThread();

t1.start();

```

3. **File Handling**:

```java

import java.io.FileWriter;
public class FileExample {

public static void main(String[] args) {

try {

FileWriter writer = new FileWriter("example.txt");

writer.write("Hello, File Handling!");

writer.close();

} catch (Exception e) {

e.printStackTrace();

```

You might also like