0% found this document useful (0 votes)
3 views

Java Fundamentals

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Java Fundamentals

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Complete Java Fundamentals

Introduction to Java
Java is a high-level, object-oriented programming language designed to have as few implementation

dependencies as possible.

It is widely used for developing applications for web, mobile, desktop, and more.

Key Features:

- Write Once, Run Anywhere (WORA).

- Platform-independent using JVM (Java Virtual Machine).

- Strong community support.

Steps to Set Up Java:

1. Download and install the Java Development Kit (JDK).

2. Install an Integrated Development Environment (IDE) like IntelliJ IDEA, Eclipse, or VS Code.

3. Verify installation by running `java -version` and `javac -version` in the terminal.

First Program: Hello, World!

```java

public class HelloWorld {

public static void main(String[] args) {

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

```

Data Types and Variables


Java has two main categories of data types:

1. Primitive Types: byte, short, int, long, float, double, char, boolean.

2. Non-Primitive Types: String, Arrays, Classes.

Example:

```java

int age = 25;

double salary = 50000.50;

boolean isJavaFun = true;

```

Control Statements
Control the flow of a program using:

1. Conditional Statements: if, if-else, switch.

2. Loops: for, while, do-while.

3. Jump Statements: break, continue.

Example:

```java

if (age > 18) {

System.out.println("You are an adult.");

} else {

System.out.println("You are a minor.");

```

Object-Oriented Programming (OOP)


Key Principles of OOP:
1. Encapsulation: Wrapping data and methods together.

2. Inheritance: Sharing behavior between classes.

3. Polymorphism: One interface, multiple implementations.

4. Abstraction: Hiding details and showing essentials.

Example:

```java

class Animal {

void sound() {

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

class Dog extends Animal {

@Override

void sound() {

System.out.println("Bark");

```

Exception Handling
Handle runtime errors using try-catch blocks:

```java

try {

int result = 10 / 0;

} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero.");

} finally {

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

```

Data Structures
Arrays, ArrayLists, and LinkedLists are commonly used in Java.

Array Example:

```java

int[] numbers = {1, 2, 3, 4};

for (int num : numbers) {

System.out.println(num);

```

File Handling
Read and write files using the java.io package.

Example:

```java

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

public class FileExample {

public static void main(String[] args) {


try {

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

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

writer.close();

} catch (IOException e) {

e.printStackTrace();

```

Multi-threading
Run multiple threads using the Thread class or Runnable interface.

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 thread = new MyThread();

thread.start();

}
}

```

You might also like