📘 Table of Contents
- 1. Introduction to Java
- 2. Setting Up Java
- 3. Basic Syntax
- 4. Variables and Data Types
- 5. Operators
- 6. Conditionals and Loops
- 7. Methods (Functions)
- 8. Classes and Objects
- 9. Inheritance and Polymorphism
- 10. Arrays and Collections
- 11. Exception Handling
- 12. Best Practices
- 13. Mini Project
- 14. Conclusion
1. Introduction to Java
Java is one of the most popular and widely used programming languages. It’s platform-independent, object-oriented, and known for its stability and scalability.
- Write once, run anywhere (WORA).
- Strongly typed and class-based.
- Used for Android, web, and enterprise applications.
Note: Java runs on the JVM (Java Virtual Machine) and compiles
.java files into .class bytecode.2. Setting Up Java
To start coding in Java:
- Install JDK (Java Development Kit).
- Use an IDE like IntelliJ IDEA, Eclipse, or VS Code.
public class Main {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
Tip: Every Java program must have a
main method — it’s the entry point.3. Basic Syntax
- Class names should start with a capital letter.
- Statements end with a semicolon (
;). - Code blocks use curly braces
{ }.
4. Variables and Data Types
Variables must have a type before use:
int age = 25;
double price = 19.99;
char grade = 'A';
String name = "Alice";
boolean isJavaFun = true;
Note: Java is statically typed — you must declare variable types explicitly.
5. Operators
int a = 10, b = 5;
System.out.println(a + b); // 15
System.out.println(a > b); // true
System.out.println(a == b); // false
System.out.println(a % b); // 0
6. Conditionals and Loops
if (a > b) {
System.out.println("a is greater");
} else {
System.out.println("b is greater");
}
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
7. Methods (Functions)
public class Main {
static int add(int x, int y) {
return x + y;
}
public static void main(String[] args) {
System.out.println(add(5, 3));
}
}
8. Classes and Objects
class Person {
String name;
int age;
Person(String n, int a) {
name = n;
age = a;
}
void introduce() {
System.out.println("Hi, I'm " + name + " and I'm " + age);
}
}
public class Main {
public static void main(String[] args) {
Person p = new Person("Bob", 25);
p.introduce();
}
}
9. Inheritance and Polymorphism
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Woof!");
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog();
a.sound(); // Woof!
}
}
10. Arrays and Collections
String[] fruits = {"Apple", "Banana", "Cherry"};
System.out.println(fruits[0]);
import java.util.ArrayList;
ArrayList list = new ArrayList<>();
list.add("Java");
list.add("Kotlin");
System.out.println(list.size());
11. Exception Handling
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
} finally {
System.out.println("End of program");
}
12. Best Practices
- Follow CamelCase naming for classes and methods.
- Keep methods short and focused.
- Use meaningful variable names.
- Close resources like files or scanners properly.
13. Mini Project: Simple Calculator
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter two numbers: ");
int a = input.nextInt();
int b = input.nextInt();
System.out.println("Sum: " + (a + b));
}
}
14. Conclusion
Java is a robust, portable, and versatile language that powers millions of apps worldwide. Once you master the basics, move on to topics like Streams, Multithreading, and Spring Framework.
Practice regularly and write small projects to strengthen your understanding!
Comments
Post a Comment