Java 12 - New Features

Hello there, future Java developers! I'm thrilled to be your guide on this exciting journey through the world of Java 12. As someone who's been teaching programming for many years, I can assure you that Java is a fantastic language to start with, and version 12 brings some cool new features to the table. So, let's dive in and explore these new additions together!

Java 12 - New Features

Java Control Statements

Before we get to the new features, let's quickly review some basic control statements in Java. These are the building blocks of any program, and understanding them is crucial for mastering Java.

If-Else Statement

The if-else statement is like a fork in the road for your program. It allows your code 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, the program checks if the age is 18 or older. If it is, it prints "You can vote!". Otherwise, it prints the "too young" message.

For Loop

The for loop is like a faithful worker that repeats a task a specific number of times.

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

This loop will count from 1 to 5, printing each number. It's like having a little helper that counts for you!

Object Oriented Programming

Now, let's talk about one of Java's superpowers: Object-Oriented Programming (OOP). In Java, everything is an object (well, almost everything).

Classes and Objects

Think of a class as a blueprint for creating objects. Let's create a simple Dog class:

public class Dog {
    String name;
    int age;

    public void bark() {
        System.out.println("Woof! Woof!");
    }
}

Now, we can create Dog objects:

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

Isn't that cool? We've just created a virtual dog that can bark!

Java Built-in Classes

Java comes with many built-in classes that make our lives easier. Let's look at one of the most commonly used ones: the String class.

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

The String class has many useful methods like length() to get the string's length, and toUpperCase() to convert it to uppercase.

New Features in Java 12

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

Switch Expressions (Preview Feature)

Java 12 introduced a preview of switch expressions, which make switch statements more powerful and concise.

String day = "MONDAY";
String result = switch (day) {
    case "MONDAY", "FRIDAY", "SUNDAY" -> "Relax";
    case "TUESDAY" -> "Work hard";
    case "THURSDAY", "SATURDAY" -> "Play sports";
    case "WEDNESDAY" -> "Go shopping";
    default -> "Unknown day";
};
System.out.println(result);

This new syntax allows us to use arrows (->) instead of colons and break statements, making the code cleaner and less error-prone.

String Class Enhancements

Java 12 added some new methods to the String class. One of them is indent(), which allows you to add or remove indentation from every line in a string.

String text = "Hello\nWorld";
System.out.println(text.indent(4));

This will print:

    Hello
    World

Each line is indented by 4 spaces. Neat, right?

Collectors.teeing()

Java 12 introduced a new collector called teeing(). It's like having two separate collectors and then combining their results.

import java.util.stream.Collectors;

double mean = List.of(1, 2, 3, 4, 5)
    .stream()
    .collect(Collectors.teeing(
        Collectors.summingDouble(i -> i),
        Collectors.counting(),
        (sum, count) -> sum / count));

System.out.println("Mean: " + mean);  // Prints "Mean: 3.0"

This code calculates the mean of a list of numbers using the new teeing() collector. It's a bit advanced, but it shows how powerful Java's stream API can be!

Conclusion

We've just scratched the surface of Java 12's new features, but I hope this gives you a taste of what's possible. Remember, learning to code is like learning a new language - it takes time and practice, but it's incredibly rewarding.

As we wrap up, I'm reminded of a student who once told me, "Java is like a Swiss Army knife - it has a tool for every job!" And you know what? He was absolutely right. With each new version, Java adds more tools to that knife, making it even more versatile and powerful.

Keep coding, keep learning, and most importantly, have fun! The world of Java is vast and exciting, and you're just at the beginning of your journey. Who knows? Maybe you'll be the one creating the next big feature in Java 13!

Credits: Image by storyset