Difference Between Java 7 and Java 8 With Examples

First, let's have a look at some common, practical questions that people have when trying to choose the right Java version for their project.

Go to the Adoptium site, chose the latest Java version, download and install it. Then come back to this guide to maybe still learn a thing or two about Java versions.

What is the latest Java version?

As of September 2021, Java 17 is the latest released Java version. It is also the next long-term support version (LTS) after Java 11.

What Java version should I use?

Newer Java versions now follow every 6 months. Hence, Java 18 is scheduled for March 2022, Java 19 for September 2022 and so on. In the past, Java release cycles were much longer, up to 3-5 years. This graphic demonstrates that:

javaversions 5

With that many new versions coming out, there's basically these real-world(™) usage scenarios:

  • Legacy projects in companies are often stuck with using Java 8 (see Why are companies still stuck with Java 8?) . Hence you will be forced to use Java 8 as well.

  • Some legacy projects are even stuck on Java 1.5 (released 2004) or 1.6 (released 2006) - sorry, pals!

  • If you are making sure to use the very latest IDEs, frameworks and build tools and starting a greenfield project, you can, without hesitation, use Java 11 (LTS) or even the latest Java 17 LTS.

  • There's the special field of Android development, where the Java version is basically stuck at Java 7, with a specific set of Java 8 features available. Or you switch to using the Kotlin programming language.

Why are companies still stuck with Java 8?

There's a mix of different reasons companies are still stuck with Java 8. To name a few:

  • Build tools (Maven, Gradle etc.) and some libraries initially had bugs with versions Java versions > 8 and needed updates. Even today, with e.g. Java >=9, certain build tools print out "reflective access"-warnings when building Java projects, which simply "feels not ready", even though the builds are fine.

  • Up until Java 8 you were pretty much using Oracle's JDK builds and you did not have to care about licensing. Oracle changed the licensing scheme In 2019, though, which led the internet go crazy with a ton of articles saying "Java is not free anymore" - and a fair amount of confusion followed. This is however not really an issue, which you'll learn about in the Java Distributions section of this guide.

  • Some companies have policies to only use LTS versions and rely on their OS vendors to provide them these builds, which takes time.

To sum up: you have a mix of practical issues (upgrading your tools, libraries, frameworks) and political issues.

Why are some Java versions, like 8 also called 1.8?

Java versions before 9 simply had a different naming scheme. So, Java 8 can also be called 1.8, Java 5 can be called 1.5 etc. When you issued the 'java -version' command, with these versions you got output like this:

                              c:\Program Files\Java\jdk1.8.0_191\bin>java                -version                java version "1.8.0_191"                  (1)                  Java(TM) SE Runtime Environment (build 1.8.0_191-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)                          

Which simply means Java 8. With the switch to time-based releases with Java 9 the naming scheme also changed, and Java versions aren't prefixed with 1.x anymore. Now the version number looks like this:

                              c:\Program Files\Java\jdk11\bin>java                -version                openjdk version "11" 2018-09-25                  (1)                  OpenJDK Runtime Environment 18.9 (build 11+28) OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)                          

What is the difference between the Java versions? Should I learn a specific one?

Coming from other programming languages with major breakages between releases, like say Python 2 to 3, you might be wondering if the same applies to Java.

Java is special in this regard, as it is extremely backwards compatible. This means that your Java 5 or 8 program is guaranteed to run with a Java 8-17 virtual machine - with a few exceptions you don't need to worry about for now.

It obviously does not work the other way around, say your program relies on Java 17 features, that are simply not available under a Java 8 JVM.

This means a couple of things:

  • You do not just "learn" a specific Java version, like 12.

  • Rather, you'll get a good foundation in all language features up until Java 8. This serves as a good base.

  • And then learn, from a guide like this, what additional features came in Java 9-17 and use them whenever you can.

What are examples of these new features between Java versions?

Have a look at the Java Features 8-17 section.

But as a rule of thumb: The older, longer release-cycles (3-5 years, up until Java 8) meant a lot of new features per release.

The 6-month release cycle means a lot less features, per release, so you can catch up quickly on Java 9-17 language features.

What is the difference between a JRE and a JDK?

Up until now, we have only been talking about "Java". But what is Java exactly?

First, you need to differentiate between a JRE (Java Runtime Environment) and a JDK (Java Development Kit).

Historically, you downloaded just a JRE if you were only interested in running Java programs. A JRE includes, among other things, the Java Virtual Machine (JVM) and the "java" command line tool.

To develop new Java programs, you needed to download a JDK. A JDK includes everything the JRE has, as well as the compiler javac and a couple of other tools like javadoc (Java documentation generator) and jdb (Java Debugger).

Now why am I talking in past tense?

Up until Java 8, the Oracle website offered JREs and JDKs as separate downloads - even though the JDK also always included a JRE in a separate folder. With Java 9 that distinction was basically gone, and you are always downloading a JDK. The directory structure of JDKs also changed, with not having an explicit JRE folder anymore.

So, even though some distributions (see Java Distributions section) still offer a separate JRE download, there seems to be the trend of offering just a JDK. Hence, we are going to use Java and JDK interchangeably from now on.

How do I install Java or a JDK then?

Ignore the Java-Docker images, .msi wrappers or platform-specific packages for the moment. In the end, Java is just a .zip file, nothing more, nothing less.

Therefore, all you need to do to install Java onto your machine, is to unzip your jdk-{5-17}.zip file. You don't even need administrator rights for that.

Your unzipped Java file will look like this:

                              Directory C:\dev\jdk-11                                12.11.2019  19:24    <DIR>                                                .                12.11.2019  19:24    <DIR>                                ..                12.11.2019  19:23    <DIR>                                bin                12.11.2019  19:23    <DIR>                                conf                12.11.2019  19:24    <DIR>                                include                12.11.2019  19:24    <DIR>                                jmods                22.08.2018  19:18    <DIR>                                legal                12.11.2019  19:24    <DIR>                                lib                12.11.2019  19:23             1.238 release                          

The magic happens in the /bin directory, which under Windows looks like this:

                              Directory C:\dev\jdk-11\bin                                ...                                12.11.2019  19:23           272.736 java.exe                                ...                                12.11.2019  19:23            20.832 javac.exe                                ...                          

So all you need to do is unzip that file and put the /bin directory in your PATH variable, so you can call the 'java' command from anywhere.

(In case you are wondering, GUI installers like the one from Oracle or Adoptium will do the unzipping and modifying the PATH variable for you, that's about it.)

To verify you installed Java correctly, you can then simply run 'java -version'. If the output looks like the one below, you are good to go.

                              openjdk version "11" 2018-09-25 OpenJDK Runtime Environment 18.9 (build 11+28) OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)                          

Now there's one question left: Where do you get that Java .zip file from? Which brings us to the topic of distributions.

moonwhough.blogspot.com

Source: https://www.marcobehler.com/guides/a-guide-to-java-versions-and-features

0 Response to "Difference Between Java 7 and Java 8 With Examples"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel