×

What’s new in Java 12

On March 19th, 2019, the Java 12th edition was released. After releasing this edition, they have decided to release every new edition every six months. This version is the advanced or updated version of JDK 11. Here, we will learn about the high-level performance of Java 12 and its new features.

Features and Language changes

A lot of new language features are available in Java 12 which are more advanced than Java 11. Some of the features are listed below.

  • String class new methods
  • File:: Mismatch method
  • Teeing collector

Now let us discuss each of them in brief with the code as well as output.

String class new methods

In the updated version that is in the Java 12 version, two new methods are there in the Java String class. The two new methods are

  • indent ()
  • transform ()

Let us see about each method in brief.

indent () Method

This method takes the parameters and gives indentation for each line depending upon the parameter or number we give in the method. If we give numbers or parameters greater than zero or a positive number, then at the beginning of each line spaces will be added. Or else, if we give a negative number that is a number less than zero then this method will remove spaces from the beginning of the line. If the text does not have sufficient white spaces, then all the leading white characters are removed.

Now let us take an example first we will try to add spaces and then we will try to remove the spaces.

IndentExpl.java

// importing the packages
import java.util.*; 
import java.lang.*;
import java.io.*;
class IndentExpl
{
public static void main (String[] args) throws java.lang.Exception
{
String txt = " Hello JavaTpointers !\n This is an updated version of Java. Welcome to Java 12.";
txt = txt.indent(4); // using the indent() method. Giving a positive number means spaces will be added
System.out.println(txt); // printing the txt


txt = txt.indent(-10); // using the indent() method. Giving a negative number means spaces will be reduced.
System.out.println(txt); // printing the txt
}
}

Output:

What’s new in Java 12

Code Explanation:

In the above code firstly, we have imported the packages they are util.*, lang. * and io.*. Next, we created a class called IndentExpl. Inside the main method, we have declared a string txt. Next, we used the indent () method. First, we have given a positive number 4, so the spaces are added to the txt. In the next method, we have given the negative number. Which means spaces will be deleted. Here in the program, we have given -10 which exceeds our spaces, but only the spaces present there are removed. Text is not affected by the number exceeding.

transform () method:

As the name of this method indicates that we can transform a string. Here this new function of Java 12 accepts only one parameter as input and returns the transformed form as the output.

Let’s understand more about this function using a program along with output.

import java.util.*;
import java.lang.*;
import java.io.*;


class TransformExpl
{
public static void main (String[] args) throws java.lang.Exception
{
String s = "Welcome, To, JavaTpoint";
List l = s.transform(s1 -> {return Arrays.asList(s1.split(","));});
System.out.println(l);
}
}

Output:

What’s new in Java 12

Code Explanation:

In the above code firstly, we have imported the packages that are the package util.*, lang.* and io.*.  Next, we have created a new class that is TransformExpl. Inside the main method, we have declared a string s. Next, we have transformed the string to the list l. And we have used the split() function to split the txt. Hence, we got the output as displayed in the above pic.

File:: Mismatch method

In the “ nio.file.Files utility ” class java 12 version came with a new mismatch method.

public static long mismatch(Path path, Path path2) throws IOException

Generally, this method is used to compare two files and the first mismatched byte is returned as output. If the files are the same means, we will get -1L as output. If there is any mismatch in the file, then the output will be in the range of 0L to the byte size of the smaller size.

let us understand more about this method using an example syntax. In the first program, we will create two same files to get the output as -1L. And next, we will

        Path path1 = Files.createTempFile("file1", ".txt");
        Path path2 = Files.createTempFile("file2", ".txt");
        Files.writeString(path1, " Hello ! Welcome to JavaTpoint ");
        Files.writeString(path2, " Hello ! Welcome to JavaTpoint ");
        long mismatch1 = Files.mismatch(path1, path2); // using mismatch method
        System.out.println(
            " Below the mismatch position in a file is returned. If the files are the same, then -1 is returned. ");
        System.out.println(
            " In file 1 and file 2 the mismatch position is at : "
            + mismatch1);
        Path path3 = Files.createTempFile("file3", ".txt");
        Path path4 = Files.createTempFile("file4", ".txt");
        Files.writeString(path3, " Hello ! Welcome to JavaTpoint ");
        Files.writeString(path4, " Hello ! Welcome to Tutorial and Example ");
        long mismatch2 = Files.mismatch(path3, path4); // using mismatch method 
        System.out.println(
            " In file 3 and file 4 the mismatch position is at : "
            + mismatch2);

Explanation:

We have created two strings and those strings are the same. Next, we used the method mismatch to check whether the texts have any mismatches or not. Since these are the same files, we will not get any mismatch and we get -1L as output. Next also we have created different texts and used this mismatch method since they are different texts, we will get the position of the mismatched place.

Teeing Collector

To the Collectors, class teeing collector is added in the new version of Java which is Java 12.

Here is the syntax of teeing collector. Through this syntax, we can easily understand about teeing collector.

Collector<T, ?, R> teeing(Collector<? super T, ?, R1> downstream1,
  Collector<? super T, ?, R2> downstream2, BiFunction<? super R1, ? super R2, R> merger)

Here in the syntax of teeing collector, we use two downstream collectors. So, each element by using both the downstream collectors they are processed. Now the processed elements are sent to the merge function. Inside the merge function, the elements are merged, and the final output is produced.

The teeing collector contains three components they are two collectors and one bi function. So input is given to the collectors, and we will receive the output through the bifunction.

Let us understand more about this teeing collector using a syntax example.

double meanofthedigits = Stream.of(10,5,2,6,1)
                .collect(Collectors.teeing(
                        summingDouble(i -> i),
                        counting(),
                        (sum, n) -> sum / n));
System.out.println(meanofthedigits);

Here we have given numbers to the collector. Next, we want to find the mean of the digits. We have calculated the mean and the output will be 4.8.


Related Topics

Java String Methods

Java String Methods Java String class is the most important class of the java.lang package. It is used to handle the String related operations. It contains a lot of built-in Java...

2 minutes read.

InputMismatchException in Java

What is InputMismatchException? One of the most frequent errors in Java is the InputMismatchException. Because the InputMismatchException is a subtype of the java.lang, it is an unchecked exception. RuntimeException.Because it is...

4 minutes read.

Java FileOutputStream

What is FileOutputStream?When we need raw stream data written into a file, we need to look for another option: FileOutputStream. It is used when the file's data is byte-oriented. It comes under...

4 minutes read.

Java Calculate Average of List

The list is a linear data structure used in Java to store ordered data collections. Additionally, it accepts duplicate values while maintaining insertion order. It is sometimes necessary to find...

3 minutes read.

Nth node from the end of the Linked list in Java

In talks with leading IT organizations like Google, Amazon, TCS, Accenture, etc., this extremely intriguing subject is constantly brought up. The goal of the problem-solving exercise is to evaluate the...

6 minutes read.

Bean class in Java

The web applications that are created with the help of the JSP or Java Server Pages generally have fairly more functionality than the web pages that specifically are created with...

5 minutes read.

Uses of Java

Java is used in many real-world Java applications, including technologies and tools. This Java programming language has become the backbone for developing many applications. In areas like embedded systems and...

3 minutes read.

Classes and Objects in Java Example Programs

Classes and Objects in Java Example Programs Java is an Object-Oriented programming language, i.e., everything in Java is associated with objects and objects are associated with classes. The classes and objects...

5 minutes read.

Getting Synchronized Set from Java HashSet

The synchronizedSet() technique for java.util.Collections class is utilized to return a synchronized (string safe) set supported by the predetermined set. To ensure sequential access, it is important that everything admittance...

4 minutes read.

ArrayDeque in Java

ArrayDeque The ArrayDeque is one of the essential concepts in java to implement the deque interface. It will allow us to apply a resizable array to implement the Deque interface. This...

8 minutes read.

Prime Number Program in Java Using a Scanner

In Java, a prime number is one that can only be divided by one or by itself and is greater than one. In other words, only one or itself can...

3 minutes read.

Java Class Name

How to write a class name? The following considerations should be made while writing class names. The name of the current class shouldn't be based on a preset or existing class. Java keywords...

3 minutes read.

Difference between JDK, JRE and JVM In Java

In the Java ecosystem, three primary components are often mentioned: JDK, JRE, and JVM. Here’s a breakdown of each: Java Development Kit (JDK) The Java Development Kit (JDK) is a comprehensive software...

2 minutes read.

Java List Implementations

ArrayList and LinkedList are the two implementations of List that are for general-purpose. You'll probably utilise an array list the majority of the time because it's quick and provides constant-time...

3 minutes read.

Java Transient Keyword

An object in Java can be turned into a stream of bytes using serialization. The data of the instance and the kind of data saved in that instance are both...

3 minutes read.

String Declaration in Java

A string is a group of characters. In Java, the string can be treated as both class and datatype. In Java programming, the String class have many advantages. Everything in...

3 minutes read.

This Operator Using in Java

this keyword in Java has a wide range of applications. this reference variable in Programming language refers to the object of interest. This keyword is used in Java. this keyword is...

5 minutes read.

Java If Keyword

Definition: The if statement specifies a section of Java code that will run if an if statement's condition is false. The following conditional statements can be used in Java: To provide a block...

3 minutes read.

Reserved Keywords in Java

In Java, a reserved term is used as a code key called a keyword. Because they are predefined, these terms cannot be used for anything else. They cannot serve as...

3 minutes read.

Java class class

Java Class class instances are an executing Java application's implementation of the classes and interfaces. As well as, every Array is indeed an object that is common for all Arrays...

6 minutes read.