1. Introduction to Java
Java is a high-level, object-oriented programming language designed by Sun Microsystems in 1995. It follows the "Write Once, Run Anywhere" (WORA) principle, meaning Java code compiled once can run on any platform with a Java Virtual Machine (JVM).
Key Features:
- Object-Oriented: Everything is an object
- Platform Independent: Runs on any OS with JVM
- Secure: Built-in security features
- Multithreaded: Supports concurrent programming
2. Setting Up Java
Installation Steps:
- Download JDK from
oracle.com - Install JDK on your system
- Set JAVA_HOME environment variable
- Verify installation:
java -version
We recommend using IDEs like Eclipse, IntelliJ IDEA, or VS Code for development.
3. Variables and Data Types
Variables are containers for storing data values. Java is strongly typed, meaning you must declare the type.
Primitive Data Types:
int- 32-bit integerdouble- 64-bit floating pointboolean- true or falsechar- single characterlong,float,byte,short
Example:
int age = 20; double gpa = 3.85; boolean isStudent = true; String name = "John"; char grade = 'A';
4. Operators
Arithmetic Operators:
int a = 10, b = 5; int sum = a + b; // 15 int diff = a - b; // 5 int product = a * b; // 50 int quotient = a / b; // 2 int remainder = a % b; // 0
Comparison & Logical Operators:
a == b; // false a > b; // true a >= b; // true a != b; // true a > 5 && b < 10; // true (AND) a > 15 || b < 10; // true (OR) !(a == b); // true (NOT)
5. Control Flow
Control flow statements determine the order in which code is executed.
If-Else Statements:
if (age >= 18) {
System.out.println("You are an adult.");
} else {
System.out.println("You are a minor.");
}
Switch Statements:
switch (grade) {
case 'A':
System.out.println("Excellent!");
break;
case 'B':
System.out.println("Good job!");
break;
default:
System.out.println("Keep trying!");
}
6. Loops
For Loop:
for (int i = 1; i <= 5; i++) {
System.out.println("Iteration: " + i);
}
While Loop:
int count = 0;
while (count < 5) {
System.out.println("Count: " + count);
count++;
}
Enhanced For Loop (For-Each):
int[] numbers = {1, 2, 3, 4, 5};
for (int num : numbers) {
System.out.println(num);
}
7. Methods
Methods are reusable blocks of code that perform specific tasks.
Method Syntax:
public static int add(int a, int b) {
return a + b;
}
public static void greet(String name) {
System.out.println("Hello, " + name);
}
// Calling methods
int result = add(5, 3); // 8
greet("Alice"); // Hello, Alice
6. Arrays
An array is a collection of elements of the same type stored in contiguous memory locations.
Declaring and Initializing Arrays:
// Declaration and initialization in one line
int[] numbers = {1, 2, 3, 4, 5};
// Declaration and initialization separately
String[] names = new String[5];
names[0] = "Alice";
names[1] = "Bob";
names[2] = "Charlie";
names[3] = "David";
names[4] = "Eve";
You can access array elements using their index, starting from zero.
7. Object-Oriented Programming Basics
OOP is a programming paradigm based on the concept of objects, which contain data and methods.
- Class: A blueprint for creating objects
- Object: An instance of a class
- Inheritance: A class can inherit properties from another class
- Polyorphism: Objects can take many forms
// Define a class
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public void displayInfo() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
// Create an object of the class
Student student1 = new Student("John", 20);
student1.displayInfo();8. Strings
A string is an object that represents sequences of characters.
Example:// Creating strings
String str1 = "Hello";
String str2 = new String("World");
// Concatenating strings
String result = str1 + " " + str2;
// Using methods on strings
System.out.println(str1.length());
System.out.println(str1.toUpperCase());