Java is based on the principles of object - oriented programming (OOP). Key concepts include classes, objects, inheritance, polymorphism, and encapsulation.
class Car {
String color;
int speed;
public Car(String color, int speed) {
this.color = color;
this.speed = speed;
}
public void accelerate(int increment) {
this.speed += increment;
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car("Red", 60);
myCar.accelerate(10);
System.out.println("The car's speed is now: " + myCar.speed);
}
}
Java provides a robust exception - handling mechanism. Exceptions are events that disrupt the normal flow of a program. There are two types of exceptions: checked and unchecked.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ExceptionExample {
public static void main(String[] args) {
try {
File myFile = new File("nonexistent.txt");
Scanner scanner = new Scanner(myFile);
} catch (FileNotFoundException e) {
System.out.println("The file was not found: " + e.getMessage());
}
}
}
JAVA_HOME
environment variable to the installation directory.bin
directory of the JDK to the PATH
environment variable.Java uses automatic garbage collection to manage memory. However, it’s important to understand how objects are created and destroyed to avoid memory leaks.
class MemoryLeakExample {
private static java.util.List<Object> list = new java.util.ArrayList<>();
public static void main(String[] args) {
for (int i = 0; i < 100000; i++) {
Object obj = new Object();
list.add(obj);
// If we don't remove the object from the list, it may cause a memory leak
}
}
}
Java supports multithreading, allowing multiple threads to execute concurrently. This can improve the performance of an application but also introduces challenges such as race conditions.
class Counter {
private int count = 0;
public synchronized void increment() {
count++;
}
public int getCount() {
return count;
}
}
class MyThread extends Thread {
private Counter counter;
public MyThread(Counter counter) {
this.counter = counter;
}
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
counter.increment();
}
}
}
public class MultithreadingExample {
public static void main(String[] args) throws InterruptedException {
Counter counter = new Counter();
MyThread thread1 = new MyThread(counter);
MyThread thread2 = new MyThread(counter);
thread1.start();
thread2.start();
thread1.join();
thread2.join();
System.out.println("The final count is: " + counter.getCount());
}
}
import java.util.Scanner;
public class CommandLineApp {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your name: ");
String name = scanner.nextLine();
System.out.println("Hello, " + name + "!");
scanner.close();
}
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class WebAppExample {
@GetMapping("/")
public String hello() {
return "Hello, World!";
}
public static void main(String[] args) {
SpringApplication.run(WebAppExample.class, args);
}
}
// This method calculates the sum of two integers
public int add(int a, int b) {
return a + b;
}
class StringUtils {
public static boolean isNullOrEmpty(String str) {
return str == null || str.isEmpty();
}
}
class Singleton {
private static Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class CalculatorTest {
@Test
public void testAdd() {
Calculator calculator = new Calculator();
int result = calculator.add(2, 3);
assertEquals(5, result);
}
}
class Calculator {
public int add(int a, int b) {
return a + b;
}
}
Building robust applications with Java requires a solid understanding of fundamental concepts, proper setup of the development environment, and adherence to common and best practices. By following the steps and examples in this guide, you can create Java applications that are reliable, efficient, and easy to maintain. Remember to continuously learn and stay updated with the latest Java features and trends to build even better applications.