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

JAVA Hand Book

JAVA Hand book
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

JAVA Hand Book

JAVA Hand book
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Java Handbook

1. Introduction to Java
Java is a versatile, object-oriented programming language known for its platform
independence, reliability, and extensive libraries. Java programs run on the Java Virtual
Machine (JVM), making them highly portable across different systems.

Core Java Features

 Platform-independent: "Write Once, Run Anywhere" capability due to the JVM.


 Object-Oriented: Follows principles like inheritance, encapsulation, polymorphism,
and abstraction.
 Robust and Secure: Automatic memory management and strong type-checking.
 Multithreading: Supports concurrent execution of code, improving performance.

2. Java Program Structure


A basic Java program consists of classes and a main method, which serves as the program's
entry point.

java

public class Main {


public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}

3. Data Types and Variables


Java is a statically-typed language, meaning variables must be declared with a specific type.

Primitive Data Types

Type Size Description Example


byte 1 byte Integer (-128 to 127) byte b = 100;
short 2 bytes Integer (-32,768 to 32,767) short s = 1000;
int 4 bytes Integer int i = 12345;
long 8 bytes Large integer long l = 123456L;
float 4 bytes Single-precision decimal float f = 3.14f;
Type Size Description Example
double 8 bytes Double-precision decimal double d = 3.14159;
char 2 bytes Single character char c = 'A';
boolean 1 bit True or false boolean b = true;

Non-Primitive Data Types

 String: A sequence of characters. String greeting = "Hello";


 Arrays: A collection of similar types. int[] numbers = {1, 2, 3};

4. Operators in Java
Arithmetic Operators

 + (Addition), - (Subtraction), * (Multiplication), / (Division), % (Modulus)

Relational Operators

 ==, !=, >, <, >=, <=

Logical Operators

 && (Logical AND), || (Logical OR), ! (Logical NOT)

Assignment Operators

 =, +=, -=, *=, /=, %=

5. Control Flow Statements


If-Else Statement
java

int number = 10;


if (number > 5) {
System.out.println("Number is greater than 5");
} else {
System.out.println("Number is 5 or less");
}

Switch Statement
java
Copy code
int day = 3;
switch (day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
default: System.out.println("Other day");
}

Loops

 For Loop:

java
Copy code
for (int i = 0; i < 5; i++) {
System.out.println(i);
}

 While Loop:

java
Copy code
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}

 Do-While Loop:

java
Copy code
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 5);

6. Methods
Methods are reusable blocks of code in Java that perform specific tasks.

java
Copy code
public static int add(int a, int b) {
return a + b;
}

 Method Call:

java
Copy code
int sum = add(5, 10);

Method Overloading
Allows multiple methods with the same name but different parameters.

java
Copy code
public int add(int a, int b) { return a + b; }
public double add(double a, double b) { return a + b; }

7. Object-Oriented Programming (OOP)


Classes and Objects

A class defines the blueprint for an object.

java
Copy code
public class Car {
String model;
int year;

public Car(String model, int year) {


this.model = model;
this.year = year;
}

public void displayInfo() {


System.out.println("Model: " + model + ", Year: " + year);
}
}

public class Main {


public static void main(String[] args) {
Car myCar = new Car("Tesla", 2022);
myCar.displayInfo();
}
}

Inheritance

Inheriting properties from one class to another.

java
Copy code
public class Animal {
public void eat() {
System.out.println("Eating...");
}
}

public class Dog extends Animal {


public void bark() {
System.out.println("Barking...");
}
}

Polymorphism
 Method Overriding: Allows a subclass to provide a specific implementation of a
method in the superclass.

Encapsulation

Encapsulation protects the data within a class by marking fields as private and providing
getters and setters.

java
Copy code
public class Person {
private String name;

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}
}

Abstraction

Abstract classes and interfaces hide implementation details.

java

public abstract class Shape {


public abstract void draw();
}

public class Circle extends Shape {


public void draw() {
System.out.println("Drawing a Circle");
}
}

8. Exception Handling
java

try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
} finally {
System.out.println("This block always executes");
}

 Common Exceptions: ArithmeticException, NullPointerException,


ArrayIndexOutOfBoundsException
9. Java Collections Framework
Java collections provide data structures like List, Set, and Map.

 List (ordered, allows duplicates): ArrayList, LinkedList

java
Copy code
List<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");

 Set (unique, unordered): HashSet, TreeSet

java
Copy code
Set<String> uniqueNames = new HashSet<>();
uniqueNames.add("Alice");
uniqueNames.add("Bob");

 Map (key-value pairs): HashMap, TreeMap

java
Copy code
Map<Integer, String> idToName = new HashMap<>();
idToName.put(1, "Alice");
idToName.put(2, "Bob");

10. File Handling


Java offers classes for reading and writing files.

java
Copy code
try {
File file = new File("example.txt");
Scanner reader = new Scanner(file);
while (reader.hasNextLine()) {
String data = reader.nextLine();
System.out.println(data);
}
reader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
}

11. Multithreading
Java allows concurrent execution through threads.

java
Copy code
public class MyThread extends Thread {
public void run() {
System.out.println("Thread running");
}
}

public class Main {


public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
}
}

Runnable Interface
java
Copy code
public class MyRunnable implements Runnable {
public void run() {
System.out.println("Runnable running");
}
}

public class Main {


public static void main(String[] args) {
Thread thread = new Thread(new MyRunnable());
thread.start();
}
}

This handbook provides a summary of Java’s foundational elements, from syntax and OOP
principles to advanced concepts like collections, file handling, and multithreading.
Experimenting with these concepts will reinforce your understanding and proficiency in Java
programming.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy