What comes to your mind when you hear the word Java?
For some people, they remember a very verbose programming language taught to them in their CS class, for some others, they have heard of it probably because of its popularity.
Some… just hate Java.
Amongst all these, Java is still currently one of the top hot languages today, why is this so?
The Java You Know
A lot of people learnt Java in their early years of programming, some went on to love it while others went on to hate it.
Unfortunately, people use the Java they previously learnt years ago to judge modern Java.
Quoting the words of Venkat Subramanian he said “Java development has been by far the best in the past 5 years compared to the past 20 years, and I am going to put my money on this, the development in the space of Java would be the biggest in the next 3 to 4 years than we have ever seen”
I would try to show you some areas where Java has improved though Java has improved way more than I would show here.
Let us use some examples, in this example, I would use the old Java you probably know and then I would show you how you might solve the problem using Modern Java.
Let’s say your lecturer told you to create a program to create a list of integers from 1 to 10 and then loop through it to print out the even numbers, using old Java your code might look like this:
ArrayList<Integer> myList = new ArrayList<Integer>();
for (int i = 1; i <= 10; i++) {
myList.add(i);
}
for (int num : myList) {
if (num % 2 == 0)
System.out.println(num);
}
Enter fullscreen mode Exit fullscreen mode
If you have used other languages then the code above might make you desire a simpler and less complex solution.
Using modern Java, I could make the code above easier on the eyes by writing it using the style below:
rangeClosed(1, 10).boxed().collect(Collectors.toList())
.stream()
.filter(integer -> integer % 2 == 0)
.forEach(System.out::println);
Enter fullscreen mode Exit fullscreen mode
I think that’s better looking.
Java has been known to be a language where you have to state every single detail before you can get a little thing done, so using the Java you know, creating a Hashmap would look like this:
HashMap<Integer, String> myMap = new HashMap<Integer, String>();
Enter fullscreen mode Exit fullscreen mode
Well, I would like that to be more concise.
Fortunately, Java currently does not need all that boilerplate code:
var myMap = new HashMap<Integer, String>();
Enter fullscreen mode Exit fullscreen mode
I am not here to convince you that Java is the most concise language, but I am here to tell you that Java is probably way less verbose and way more concise than you last met it.
Modern Java
I would list out some improvements in Java, take note, this is nowhere an exhaustive list and there are way more improvements than I would mention here.
If I tried to mention all the improvements, I would have enough content to publish a book about it.
JShell
The Java Shell tool (JShell) is an interactive tool for learning the Java programming language and prototyping Java code.
JShell is a Read-Evaluate-Print Loop (REPL), which evaluates declarations, statements, and expressions as they are entered and immediately shows the results. The tool is run from the command line.
You can test individual statements, try out different variations of a method, and experiment with unfamiliar APIs within the JShell session.
It was added in JDK 9, it’s quite a useful tool, might be a lifesaver one of these days.
Switch Expressions
Switch expressions are expected to be in JDK 12+, they simplify the traditional switch statements we know, for example, the code:
switch (day) {
case MONDAY:
case FRIDAY:
case SUNDAY:
System.out.println(6);
break;
case TUESDAY:
System.out.println(7);
break;
case THURSDAY:
case SATURDAY:
System.out.println(8);
break;
case WEDNESDAY:
System.out.println(9);
break;
}
Enter fullscreen mode Exit fullscreen mode
Would be written as:
switch (day) {
case MONDAY, FRIDAY, SUNDAY -> System.out.println(6);
case TUESDAY -> System.out.println(7);
case THURSDAY, SATURDAY -> System.out.println(8);
case WEDNESDAY -> System.out.println(9);
}
Enter fullscreen mode Exit fullscreen mode
CompletableFuture (The Promises of Java)
CompletableFuture allows the creation and execution of chains of asynchronous tasks.
CompletableFuture works the same way Promises work in JavaScript.
public static void main(String[] args) throws InterruptedException {
CompletableFuture.supplyAsync(() -> longProcess()).thenAccept(System.out::println);
Thread.sleep(4000);
System.out.println("Done!");
}
private static int longProcess() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 1;
}
Enter fullscreen mode Exit fullscreen mode
CompletableFuture was introduced in JDK 8, though people focused on the shiny new Java streams and didn’t notice the wonderful tool.
If you are interested you could go watch and learn about it:
Project Loom
Project Loom, which is not yet fully implemented, will introduce fibers as lightweight, efficient threads managed by the Java Virtual Machine, that let developers use the same simple abstraction but with better performance and lower footprint.
Fibers are much more lightweight than kernel threads in terms of memory footprint, and the overhead of task-switching among them is close to zero.
Millions of fibers can be spawned in a single JVM instance, and programmers need not hesitate to issue synchronous, blocking calls, as blocking will be virtually free.
Records (Data classes for Java)
Records, which is also a feature yet to be implemented, is going to help make creating POJO classes easier. For example a class such as:
class Point {
double x;
double y;
Point(double x, double y) {
this.x = x;
this.y = y;
}
public void setX(double x) {
this.x = x;
}
public double getX() {
return x;
}
public void setY(double y) {
this.y = y;
}
public double getY() {
return y;
}
@Override
public int hashCode() {
return super.hashCode();
}
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
@Override
public String toString() {
return super.toString();
}
}
Enter fullscreen mode Exit fullscreen mode
Would be rewritten as:
record Point(double x, double y);
Enter fullscreen mode Exit fullscreen mode
There are some other very interesting Java features which I would leave to your curiosity to find out.
Release Cycle
Recently, it was announced that Java would be using a 6-month release cycle, so we would expect a release every 6 months. The good news is that you can expect to get new features faster.
Java on Android
Google announced Kotlin as an official language and since then it has seen huge growths.
I do like Kotlin, I use Kotlin, but I am of the opinion that Kotlin isn’t here to replace Java.
Now, you might have heard some Android devs complain of Java. I usually like them to be specific.
What they actually complain of is the verbosity of Java 7 whereas the latest to be released at the time of this writing is Java 12.
Kotlin looks cool on Android partly because Android developers can’t access the latest Java features and are stuck using Java 7 ~ Java 8.
Nevertheless, most Android applications are still currently written in Java. Probably a lot of professional developers are fine using Java 7 🙂
We all shouldn’t make the mistake of comparing Kotlin to Java the way Swift was compared to Objective C.
Unfortunately, a lot of Android developers aren’t aware of how far Java has come as a language because they still use the Java of their CS class.
By the way, I am an Android developer and I also think Kotlin is cool 😉
Java Is Still free
Oracle recently announced that they would be charging for the commercial use of Oracle JDK and long-term support. Well, it got to the headlines and people wrote a lot of factually incorrect articles.
To whom it may concern, Java is still free.
Most big companies who use Oracle JDK for their development wouldn’t be so bothered by the change.
For people who are worried, there is no need to be!
As at JDK 11, OpenJDK has feature parity with Oracle’s JDK. Hence, they are interchangeable.
Moreover, Amazon recently announced Amazon Corretto which is a no-cost, multiplatform, production-ready distribution of the Open Java Development Kit (OpenJDK).
Corretto comes with long-term support that will include performance enhancements and security fixes.
You could also go for other OpenJDK providers such as AdoptOpenJDK, there is actually no cause for alarm.
Java is still free.
Java’s current rank as a language
Currently, Java is #1 language for the TIOBE index programming language ranking. It was also #2 in the GitHub’s State of the Octoverse.
This isn’s a sales pitch
This article isn’t a quit your job and go learn Java article. Instead, I am here to tell you how far Java has come and where it is heading as a language.
Java’s ability to keep on adapting has helped it remain one of the top languages for a very long period.
暂无评论内容