Java is one of the world’s most widely used programming languages. It is object-oriented, platform-independent, and the backbone of many enterprise applications, Android apps, and server systems.
Why Learn Java?
- Cross-platform → Runs on any OS with a JVM (Write Once, Run Anywhere).
- Object-Oriented → Organize code into reusable classes.
- Popular → Used in Android, backend, finance, and more.
- Stable & Secure → Trusted for enterprise-level software.
Hello World
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
Variables
int age = 25; // Integer
double pi = 3.14; // Decimal
String name = "Alice"; // Text
boolean isJavaFun = true; // Boolean
Data Types
- Primitive Types: int, double, char, boolean
- Non-Primitive Types: String, Arrays, Objects
Operators
+
(addition),-
(subtraction)*
(multiplication),/
(division)==
(equal to),!=
(not equal)&&
(AND),||
(OR)
Conditionals
int age = 18;
if (age >= 18) {
System.out.println("Adult");
} else {
System.out.println("Minor");
}
Switch Statement
int day = 3;
switch (day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
case 3: System.out.println("Wednesday"); break;
default: System.out.println("Other day");
}
Loops
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
int x = 0;
while (x < 3) {
System.out.println(x);
x++;
}
Methods
public static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
System.out.println(add(5, 3));
}
Classes and Objects
class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
void greet() {
System.out.println("Hi, I'm " + name + " and I'm " + age + " years old.");
}
}
public class Main {
public static void main(String[] args) {
Person p = new Person("Alice", 25);
p.greet();
}
}
Inheritance
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Bark");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.sound();
}
}
Arrays
int[] numbers = {1, 2, 3, 4};
for (int n : numbers) {
System.out.println(n);
}
Mini Project: Simple Calculator
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = sc.nextInt();
System.out.print("Enter second number: ");
int b = sc.nextInt();
System.out.println("Sum: " + (a + b));
System.out.println("Difference: " + (a - b));
}
}
Best Practices
- Follow naming conventions (
camelCase
for variables,PascalCase
for classes). - Keep classes small and focused.
- Use comments to explain complex logic.
- Avoid unnecessary object creation.
Next Steps
- Java Collections → Learn about lists, sets, and maps.
- OOP Principles → Deep dive into inheritance, polymorphism, encapsulation.
- Frameworks → Spring Boot for backend development.
- Android → Learn Java for mobile apps (though Kotlin is now preferred).
Conclusion
Java is a powerful, stable, and versatile programming language that has shaped modern software development. Mastering Java will give you a solid foundation for Android apps, enterprise applications, and backend systems.
Keep practicing, build projects, and Java will open countless opportunities 🚀🔥
No comments:
Post a Comment