Wednesday, July 10, 2024

A Java Programmer's Journey from Java 7 to Java 11

A Java Programmer's Journey from Java 7 to Java 11

As Java has evolved, it has introduced numerous features and enhancements that have significantly improved the language. This article highlights the key differences and improvements from Java 7 to Java 11 with examples and explanations.

1. Lambda Expressions and Functional Interfaces (Java 8)

Lambda expressions provide a concise way to represent anonymous functions. They are particularly useful for implementing simple event listeners or callbacks.
// Java 7: Anonymous inner class
Runnable runnable = new Runnable() {
    @Override
    public void run() {
        System.out.println("Hello, World!");
    }
};
new Thread(runnable).start();

// Java 11: Lambda expression
Runnable runnable = () -> System.out.println("Hello, World!");
new Thread(runnable).start();

2. Stream API (Java 8)

The Stream API allows for functional-style operations on collections of objects, such as map, filter, and reduce operations.
// Java 7
List<String> list = Arrays.asList("a", "b", "c", "d");
for (String s : list) {
    System.out.println(s.toUpperCase());
}

// Java 11
List<String> list = Arrays.asList("a", "b", "c", "d");
list.stream()
    .map(String::toUpperCase)
    .forEach(System.out::println);

3. Optional (Java 8)

Optional is a container object that may or may not contain a non-null value. It helps avoid null checks and potential NullPointerException.
// Java 7
public String getName(Person person) {
    if (person != null) {
        return person.getName();
    } else {
        return "Unknown";
    }
}

// Java 11
public String getName(Person person) {
    return Optional.ofNullable(person)
                   .map(Person::getName)
                   .orElse("Unknown");
}

4. Local-Variable Type Inference (Java 10)

The var keyword allows local variables to have their types inferred by the compiler, making code less verbose.
// Java 7
List<String> list = new ArrayList<>();
list.add("Hello");

// Java 11
var list = new ArrayList<String>();
list.add("Hello");

5. Module System (Java 9)

The module system introduced in Java 9 allows for better encapsulation and separation of code, improving maintainability and security.
// Java 7: No module system
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

// Java 11: With module system
// module-info.java
module mymodule {
    exports com.example;
}

// Main.java
package com.example;

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

6. API Changes and Enhancements

Standard libraries have been enhanced with new methods to make common tasks easier and more efficient.
// Java 7: String.join not available
List<String> list = Arrays.asList("a", "b", "c");
String result = "";
for (String s : list) {
    result += s + ",";
}
result = result.substring(0, result.length() - 1);

// Java 11
List<String> list = Arrays.asList("a", "b", "c");
String result = String.join(",", list);

7. New Methods in java.util.Collection (Java 8)

Java 8 introduced new methods like forEach, removeIf, and replaceAll to simplify collection operations.
// Java 7
List<String> list = new ArrayList<>(Arrays.asList("a", "b", "c"));
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
    if ("b".equals(iterator.next())) {
        iterator.remove();
    }
}

// Java 11
List<String> list = new ArrayList<>(Arrays.asList("a", "b", "c"));
list.removeIf("b"::equals);

8. HTTP Client (Java 11)

Java 11 introduced a new HttpClient API to replace the old HttpURLConnection and third-party libraries for HTTP requests.
// Java 7: Using third-party libraries like Apache HttpClient
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet("https://api.example.com/data");
HttpResponse response = client.execute(request);

// Java 11
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
    .uri(URI.create("https://api.example.com/data"))
    .build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

9. Default and Static Methods in Interfaces (Java 8)

Java 8 allows interfaces to have default and static methods, enabling interfaces to provide method implementations.
// Java 7
public interface MyInterface {
    void abstractMethod();
}

public class MyClass implements MyInterface {
    @Override
    public void abstractMethod() {
        System.out.println("Abstract method implementation");
    }
}

// Java 11
public interface MyInterface {
    void abstractMethod();
    
    default void defaultMethod() {
        System.out.println("Default method implementation");
    }

    static void staticMethod() {
        System.out.println("Static method implementation");
    }
}

public class MyClass implements MyInterface {
    @Override
    public void abstractMethod() {
        System.out.println("Abstract method implementation");
    }
}

public class Main {
    public static void main(String[] args) {
        MyClass myClass = new MyClass();
        myClass.abstractMethod();
        myClass.defaultMethod();
        MyInterface.staticMethod();
    }
}

10. New Date and Time API (Java 8)

The new date and time API introduced in Java 8 (in the java.time package) provides a more modern and comprehensive way to handle dates and times.
// Java 7
import java.util.Date;
import java.text.SimpleDateFormat;

public class Main {
    public static void main(String[] args) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(sdf.format(date));
    }
}

// Java 11
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        LocalDate date = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        System.out.println(date.format(formatter));
    }
}

11. CompletableFuture (Java 8)

CompletableFuture is part of the java.util.concurrent package and provides a way to handle asynchronous computations more effectively.
// Java 7
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Main {
    public static void main(String[] args) throws Exception {
        ExecutorService executor = Executors.newFixedThreadPool(1);
        Future<String> future = executor.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                return "Hello, World!";
            }
        });
        System.out.println(future.get());
        executor.shutdown();
    }
}

// Java 11
import java.util.concurrent.CompletableFuture;

public class Main {
    public static void main(String[] args) {
        CompletableFuture.supplyAsync(() -> "Hello, World!")
                         .thenAccept(System.out::println);
    }
}

12. VarHandle and VarHandles (Java 9)

VarHandle is a flexible mechanism for variable manipulation, providing a safer alternative to the sun.misc.Unsafe class.
// Java 7: No direct equivalent, often Unsafe was used with reflection

// Java 11
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;

public class Main {
    private static int value;

    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        VarHandle handle = MethodHandles.lookup().findStaticVarHandle(Main.class, "value", int.class);
        handle.set(42);
        System.out.println(value);
    }
}

13. Private Methods in Interfaces (Java 9)

Java 9 introduced private methods in interfaces to share code between default methods.
// Java 7: No direct equivalent

// Java 11
public interface MyInterface {
    default void method1() {
        common();
    }

    default void method2() {
        common();
    }

    private void common() {
        System.out.println("Common method implementation");
    }
}

public class MyClass implements MyInterface {}

public class Main {
    public static void main(String[] args) {
        MyClass myClass = new MyClass();
        myClass.method1();
        myClass.method2();
    }
}

14. Collection Factory Methods (Java 9)

Java 9 introduced factory methods for creating immutable collections, making it easier to create and initialize collections.
// Java 7
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");

Set<String> set = new HashSet<>();
set.add("a");
set.add("b");
set.add("c");

Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);

// Java 11
List<String> list = List.of("a", "b", "c");
Set<String> set = Set.of("a", "b", "c");
Map<String, Integer> map = Map.of("a", 1, "b", 2, "c", 3);

15. String API Enhancements (Java 11)

Java 11 introduced several new methods in the String class to simplify common operations.
// Java 7
String str = " ";
boolean isBlank = str.trim().isEmpty();
String stripped = str.replaceAll("^\\s+|\\s+$", "");
String repeated = new String(new char[3]).replace("\0", "abc");

// Java 11
String str = " ";
boolean isBlank = str.isBlank(); // true
String stripped = str.strip();   // ""
String repeated = "abc".repeat(3); // "abcabcabc"

16. Files API Enhancements (Java 11)

Java 11 introduced new methods in the Files class to make file operations more straightforward.
// Java 7
Path path = Paths.get("test.txt");
List<String> lines = Files.readAllLines(path);
String content = "";
for (String line : lines) {
    content += line + System.lineSeparator();
}
Files.write(path, content.getBytes());

// Java 11
Path path = Paths.get("test.txt");
String content = Files.readString(path);
Files.writeString(path, content);
By leveraging these features introduced from Java 8 to Java 11, you can make your Java code more concise, readable, maintainable, and efficient. Moving from Java 7 to Java 11 involves adopting new paradigms and best practices that modernize your development process and take advantage of the latest improvements in the Java language and libraries.

0 comments:

Post a Comment

Contact

Get in touch with me


Adress/Street

Bangalore, India