Java 15 - New Features

Hello there, future Java developers! I'm thrilled to be your guide on this exciting journey through the new features of Java 15. As someone who's been teaching Java for over a decade, I can confidently say that this version brings some fantastic improvements to the table. So, let's dive in and explore these new features together!

Java 15 - New Features

Java Control Statements

Before we delve into the new features, let's quickly recap some basic control statements in Java. These are fundamental building blocks that you'll use in almost every program you write.

If-Else Statement

The if-else statement is like a fork in the road. It allows your program to make decisions based on certain conditions.

int age = 18;
if (age >= 18) {
    System.out.println("You can vote!");
} else {
    System.out.println("Sorry, you're too young to vote.");
}

In this example, if the age is 18 or above, the program will print "You can vote!". Otherwise, it will print the sorry message.

For Loop

The for loop is like a faithful workhorse in Java. It's great for repeating a block of code a specific number of times.

for (int i = 0; i < 5; i++) {
    System.out.println("Count: " + i);
}

This loop will print the count from 0 to 4. It's like telling Java, "Hey, start at 0, keep going as long as we're less than 5, and add 1 each time."

Object Oriented Programming

Now, let's talk about one of the coolest aspects of Java: Object-Oriented Programming (OOP). It's like building with LEGO bricks, where each brick is an object with its own properties and behaviors.

Classes and Objects

A class is like a blueprint, and an object is what you build from that blueprint. Let's create a simple Dog class:

public class Dog {
    String name;
    int age;

    public void bark() {
        System.out.println(name + " says: Woof!");
    }
}

Now, we can create a Dog object:

Dog myDog = new Dog();
myDog.name = "Buddy";
myDog.age = 3;
myDog.bark(); // This will print: Buddy says: Woof!

Java Built-in Classes

Java comes with a treasure trove of built-in classes that make our lives easier. Let's look at a couple of them.

String Class

The String class is used to work with text. It's so common that Java gives it special treatment.

String greeting = "Hello, World!";
System.out.println(greeting.length()); // Prints: 13
System.out.println(greeting.toUpperCase()); // Prints: HELLO, WORLD!

ArrayList Class

ArrayList is like a super-powered array that can grow or shrink as needed.

import java.util.ArrayList;

ArrayList<String> fruits = new ArrayList<String>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");

System.out.println(fruits); // Prints: [Apple, Banana, Cherry]
System.out.println(fruits.get(1)); // Prints: Banana

New Features in Java 15

Now, let's explore some of the exciting new features in Java 15!

Text Blocks

Text Blocks make it much easier to work with multi-line strings. It's like writing a letter to your code!

String oldWay = "This is how we used to\n" +
                "write multi-line strings.\n" +
                "It was not very convenient.";

String newWay = """
                This is a Text Block.
                It's much easier to read
                and write multi-line strings now!
                """;

System.out.println(newWay);

The newWay string is much cleaner and easier to read, isn't it?

Sealed Classes

Sealed classes allow you to control which other classes can inherit from them. It's like creating an exclusive club for your classes!

public sealed class Shape permits Circle, Square, Triangle {
    // Shape class code
}

final class Circle extends Shape {
    // Circle class code
}

final class Square extends Shape {
    // Square class code
}

final class Triangle extends Shape {
    // Triangle class code
}

// This would cause a compile-time error:
// class Rectangle extends Shape { }

In this example, only Circle, Square, and Triangle are allowed to extend Shape. Any other class trying to extend Shape would cause an error.

Pattern Matching for instanceof

This feature makes it easier to work with different types of objects. It's like having a smart detective in your code!

Object obj = "Hello, Java 15!";

if (obj instanceof String str) {
    System.out.println(str.toUpperCase());
}

In older versions of Java, we'd have to cast obj to String after the instanceof check. Now, we can do it all in one line!

Records

Records are a new kind of class that's perfect for storing data. They're like a Swiss Army knife for simple data objects!

public record Person(String name, int age) { }

Person alice = new Person("Alice", 30);
System.out.println(alice.name()); // Prints: Alice
System.out.println(alice.age()); // Prints: 30

Records automatically generate constructors, getters, equals(), hashCode(), and toString() methods for us. It's like having a personal assistant for your classes!

Conclusion

Wow, we've covered a lot of ground today! From basic control statements to the exciting new features in Java 15, you're now equipped with some powerful tools for your Java programming journey. Remember, programming is like learning a new language - it takes practice, patience, and persistence. But with these new features, Java is becoming more expressive and easier to use than ever before.

As we wrap up, I'm reminded of a quote by the famous computer scientist Alan Kay: "The best way to predict the future is to invent it." With Java 15, you have even more power to invent the future through your code. So go forth, experiment with these new features, and create something amazing!

Happy coding, future Java masters! ??‍??‍?

Credits: Image by storyset