Posts

Showing posts with the label Java9

Java 9 Module Internals Explained in Brief

One of the new features added by Java 9 is the module system. This article is the second part in a  3 part article. In Part 1 , I gave a high level introduction to modules and the advantages that they provide. In this part, I will dive more into the details of what exactly a module is, what it consists of and other module internals. See also Module Introduction and Creating a Module . What is a module As explained in the introductory article on modules, modules are basically groups of packages. Modules help to organize the code better and to achieve the Single Responsibility Principle(SRP). Also, a module can be deployed by itself. So if there is a large application, you can break it down into modules. Each module can be built into a separate jar file and deployed as required instead of deploying a big fat jar file for the whole application. Module Descriptor Along with very module, you need to provide a module descriptor. The module descriptor needs to be present at the root of the ...

Java 9 Stream API iterate method example

Another improvement made by Java 9 is adding a new Stream.iterate method. This is a new feature in Java 9 Stream API. Java 8 Stream.iterate Prior to Java 9, The Stream Interface already had an iterate method. The Stream.iterate is a static method on the Stream interface. You can use it to create a new Stream. It accepts the following parameters: Initial Value UnaryOperator instance which is used to find subsequent values. It returns a Stream instance. The following code demonstrates this method: Stream<Integer> numberStream = Stream.iterate(2, num -> num+2).limit(10);numberStream.forEach(System.out::println); This code uses an initial value of 2. Also, the UnaryOperator is implemented via a lambda expression that simply increments the previous value by 2. Note that the Stream.iterate method returns an infinite Stream. So the code uses the limit method to terminate the Stream. So this code prints the following output: 2468101214161820 Java 9 Stream.iterate Java 9 has added an o...

Java 9 Stream takeWhile with examples

Java 8 introduced the Stream API which allows processing and applying some operations to the elements in a Collection.  Java 9 has made some improvements to the Stream API . One such improvement is the takeWhile method. In this article, I will be covering the Java 9 Stream takeWhile method in detail. I will also be writing some Java 9 takeWhile examples. The takeWhile method operates on a Stream. It accepts a Predicate instance and applies the Predicate to all the elements in the input stream and produces an output stream. It behaves differently depending on whether the input Stream is ordered or unordered. takeWhile on Ordered Stream As per the API documentation, when the takeWhile is applied on an ordered Stream, it returns, a stream consisting of the longest prefix of elements taken from this stream that match the given predicate if the stream is ordered. So basically, the takeWhile method applies the Predicate on each element in the input Stream until the condition in the Predica...

JShell in Java 9

Image
One of the new features added by Java 9 is the JShell.  In this blog post,  I will be explaining how JShell works. What is JShell ? JShell provides REPL capabilities to Java. REPL stands for Read Evaluate Print Loop. Many languages like Python also provide REPL capabilities. REPL allows you to write Java code and test it without the need to compile it.   How to launch JShell Step 1 – Make sure you have JDK 9 installed   Step 2 -Open a Command prompt and navigate to the JDK\bin folder   Step 3 – Type JShell How to Use JShell Type the code that you want to test. I have typed 4+5. Press Enter   Step 5 – View the results Example 2  – Using a Sysout statement Example 3 – Assigning a value to a variable and using it How to exit JShell Type /exit