Java Basics
Java is a high-level, object-oriented programming language known for its
portability, security, and robustness. It follows the “Write Once, Run
Anywhere” (WORA) principle, meaning Java programs can run on any system
with a Java Virtual Machine (JVM).
### **1. Java Features**
- **Object-Oriented** – Follows OOP principles like Encapsulation,
Inheritance, Polymorphism, and Abstraction.
- **Platform-Independent** – Runs on any OS with JVM.
- **Simple & Secure** – Eliminates memory management issues with
automatic garbage collection.
- **Multi-threaded** – Supports concurrent execution for better performance.
### **2. Basic Structure of a Java Program**
Every Java application must have a `main` method as the entry point.
```java
Public class HelloWorld
Public static void main(String[] args) {
System.out.println(“Hello, World!”);
```
**Explanation:**
- `public class HelloWorld` → Defines a class named `HelloWorld`.
- `public static void main(String[] args)` → Main method where execution
starts.
- `System.out.println(“Hello, World!”);` → Prints outputs
### **3. Java Syntax and Data Types**
#### **a. Variables & Data Types**
Java is a statically-typed language, meaning variables must be declared with
a specific type.
Int age = 25; // Integer type
Double price = 99.99; // Decimal type
Char grade = ‘A’; // Character type
Boolean isJavaFun = true; // Boolean type
String name = “John”; // String type
```
#### **b. Operators**
Java supports arithmetic, relational, logical, and bitwise operators.
Int sum = 5 + 3; // Addition
Boolean check = 5 > 3; // Relational Operator
Boolean result = true && false; // Logical AND
```
### **4. Control Flow Statements**
#### **a. Conditional Statements**
```java
Int num = 10;
If (num > 0) {
System.out.println(“Positive number”);
} else {
System.out.println(“Negative number”);
```
#### **b. Looping Statements**
**For Loop:**
```java
For (int I = 1; I <= 5; i++) {
System.out.println(i);
**While Loop:**
```java
Int I = 1;
While (I <= 5) {
System.out.println(i);
I++;
```
### **5. Object-Oriented Programming (OOP) in Java**
#### **a. Classes and Objects**
Java uses **classes** as blueprints for creating **objects**.
```java
Class Car {
String brand = “Toyota”; // Attribute
Void honk() { // Method
System.out.println(“Beep Beep!”);
Public class Main {
Public static void main(String[] args) {
Car myCar = new Car(); // Create an object
System.out.println(myCar.brand);
myCar.honk();
```
#### **b. Inheritance**
Allows a class to inherit properties from another class.
```java
Class Animal {
Void sound() {
System.out.println(“Animals make sounds”);
Class Dog extends Animal {
Void bark() {
System.out.println(“Dog barks”);
Public class Main {
Public static void main(String[] args) {
Dog myDog = new Dog();
myDog.sound(); // Inherited method
myDog.bark();
```
### **6. Exception Handling**
Java uses `try-catch` blocks to handle runtime errors.
```java
Try {
Int result = 10 / 0; // This will cause an error
} catch (ArithmeticException e) {
System.out.println(“Cannot divide by zero!”);
```
### **7. Input and Output in Java**
Reading user input using `Scanner` class.
```java
Import java.util.Scanner;
Public class UserInput {
Public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print(“Enter your name: “);
String name = scanner.nextLine();
System.out.println(“Hello, “ + name);
```
### **8. Java Collections Framework (Basic)**
Java provides built-in data structures like Lists, Sets, and Maps.
```java
Import java.util.ArrayList;
Public class ListExample {
Public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
Names.add(“Alice”);
Names.add(“Bob”);
System.out.println(names);
```
### **9. Compiling and Running Java Programs**
1. **Write Code** in `HelloWorld.java`
2. **Compile** using:
```
Javac HelloWorld.java
```
3. **Run** using:
```
Java HelloWorld
```
### **10. Java Development Tools**
- **JDK (Java Development Kit)** → Includes compiler (`javac`), libraries, and
JVM.
- **JRE (Java Runtime Environment)** → Runs Java applications.
- **IDEs (Integrated Development Environments)** → Eclipse, IntelliJ IDEA,
NetBeans.
### **Next Steps**
If you’re new to Java, practice small programs, explore Object-Oriented
concepts, and try building simple applications like a calculator or a to-do list.
Would you like me to suggest a learning path or beginner projects?