Learn Java Coding
1. Introduction to Java
Java is a popular, object-oriented programming language known for its platform independence and strong memory management.
2. Setting Up Java
To run Java programs, install the JDK (Java Development Kit) and use an IDE like IntelliJ IDEA, Eclipse, or VS Code.
3. Java Syntax
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
4. Java Variables
int age = 25; // Integer
double price = 19.99; // Floating point
String name = "John"; // String
boolean isJavaFun = true; // Boolean
5. Java Operators
int sum = 10 + 5; // Addition
int diff = 10 - 5; // Subtraction
int prod = 10 * 5; // Multiplication
int div = 10 / 5; // Division
int mod = 10 % 3; // Modulus
6. Java Loops
// For Loop
for (int i = 0; i < 5; i++) {
System.out.println("Iteration: " + i);
}
// While Loop
int i = 0;
while (i < 5) {
System.out.println("Iteration: " + i);
i++;
}
7. Java Conditional Statements
if (age > 18) {
System.out.println("Adult");
} else {
System.out.println("Minor");
}
8. Java Arrays
int[] numbers = {10, 20, 30, 40, 50};
System.out.println(numbers[0]); // Output: 10
9. Java Methods
public static int add(int a, int b) {
return a + b;
}
10. Object-Oriented Programming (OOP) in Java
class Car {
String model;
Car(String model) {
this.model = model;
}
void showModel() {
System.out.println("Car model: " + model);
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car("Toyota");
myCar.showModel();
}
}
11. Java Exception Handling
try {
int result = 10 / 0; // This will cause an error
} catch (ArithmeticException e) {
System.out.println("Error: Division by zero!");
}
12. Java Multithreading
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running...");
}
}
public class Main {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
}
}
13. Conclusion
Java is a powerful language used for web applications, mobile apps (Android), and enterprise software. Keep practicing to master Java.
