Java 8 vs Java 12
In this article, we will see a comparison of the Java 8 vs Java 12.
1. What is Java?
Java is a programming language first released by Sun Microsystems in 1995. There are lots of applications and websites that will not work unless you have Java installed, and more are created every day. Java is fast, secure, and reliable. From laptops to datacenters, game consoles to scientific supercomputers, cell phones to the Internet. Java is a high-level, platform-independent, object-oriented, functional programming language and runtime environment. Java applications are typically compiled to bytecode (called class files) that can be executed by a JVM (Java Virtual Machine), independent of computer architecture. The JVM manages memory with the help of a garbage collector (see also garbage-collection) to handle object removal from memory when objects are no longer in use.
2. Java 8 vs Java 12: Head To Head Comparison
Oracle no longer provides a 32-bit Windows download. Java Applet, Java Web Start, Java Plugin, and Java Control Panel are not available in Java 12. JavaFX is no longer included in Java 12. It is now available as a separate download from https://openjfx.io/. JAXB and JAX-WS are no longer bundled with Java 12. Support parallel full garbage collection on G1.
2.1 Switch Expression
The most popular feature introduced in Java 12 is the Switch Expression. Let’s compare the old and new switch statements.
ExampleSwitch.java
package code; public class ExampleSwitch { public static void main(String[] args) { int number = 5; String result; //OLD SWITCH switch (number) { case 1: case 2: result = "one or two"; break; case 3: result = "three"; break; case 4: case 5: case 6: result = "four or five or six"; break; default: result = "unknown"; break; }; System.out.println("Old Syntax : " + result); //NEW SWITCH EXPRESSION result = switch (number) { case 1, 2 -> "one or two"; case 3 -> "three"; case 4, 5, 6 -> "four or five or six"; default -> "unknown"; }; System.out.println("New Syntax : " + result); } }
2.2 New String Methods
Java 12 adds a few new methods to the String class: isBlank()
, lines()
, strip()
, stripLeading()
, stripTrailing()
, and repeat()
. Let’s see how we can make use of the new methods to extract non-blank, stripped lines from a multi-line string:
NewStringMethods.java
package code; import java.util.List; import java.util.stream.Collectors; public class NewStringMethods { public static void main(String[] args) { String multilineString = "I Ate \n \n a \n Big Potato."; List lines = multilineString.lines() .filter(line -> !line.isBlank()) .map(String::strip) .collect(Collectors.toList()); System.out.println(lines); } }
3. What do I have to install, Java 12 or Java 8?
Oracle stopped supporting Java 8 in January 2019. This isn’t out of the ordinary, it regularly does this for major Java versions after five years of public availability. Lots of things have changed since Java 8: we get releases every 6 months Licensing, updates, and support have changed. Where we get our JDK from has changed. On top of that, of course, there are new language features, including the major changes that went into Java 9. But now that Java 11 has replaced Java 8 as the latest LTS, and now that major libraries, frameworks, and build tools have adopted the latest versions of Java, it is a good time to migrate your application to Java 11 or 12.
4. More articles
- Java 8 Features Tutorial
- Download and Install Java Development Kit (JDK) 8
- Download and Install Java Development Kit (JDK) 11
- Download and Install Java Development Kit (JDK) 13
5. Download the Source Code
This was a brief comparison between Java 8 and Java 12.
You can download the full source code of this example here: Java 8 vs Java 12