Java Fundamentals
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:
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.
```java
System.out.println("Hello, World!");
```
1. Primitive Types: byte, short, int, long, float, double, char, boolean.
Example:
```java
```
Control Statements
Control the flow of a program using:
Example:
```java
} else {
```
Example:
```java
class Animal {
void sound() {
@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
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;
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
System.out.println("Thread is running.");
thread.start();
}
}
```