JavaRush /Java Blog /Random EN /Coffee break #154. Three types of loops in Java. JDK 19: ...

Coffee break #154. Three types of loops in Java. JDK 19: New Java 19 features coming in September

Published in the Random EN group

Three Types of Loops in Java

Source: Medium After reading this post, you will learn about different ways of looping in Java. Coffee break #154.  Three types of loops in Java.  JDK 19: New Java 19 features coming in September - 1A Loop is designed to execute a block of code until a condition becomes true. There are three types of loops in Java:
  • while
  • do-while
  • for

While loop

The while loop repeats until the specified Boolean condition evaluates to true. As shown in the diagram below, the statement will be executed until the condition test returns true. Coffee break #154.  Three types of loops in Java.  JDK 19: New Java 19 features coming in September - 2

Syntax

while (condition true) {
       // Блок codeа
}

Example

Here the while loop will be executed until the condition is true for x less than 3.
public class WhileLoop {
     static int x = 1;
     public static void main(String[] args) {
         while(x < 3) {
             System.out.println("x = "+x);
             x++;
         }
     }
}
Conclusion:
x = 1 x = 2

do-while loop

A do-while loop is similar to a while loop with one small difference. The do-while loop always runs once before testing the condition. Coffee break #154.  Three types of loops in Java.  JDK 19: New Java 19 features coming in September - 3

Syntax

do {
     //  Блок codeа
   } while(condition);

Example

In this example, you can see that the do statement or block of code is always executed once before testing whether the condition is true or false.
static int x = 5;
        public static void main(String[] args) {
            do {
                System.out.println("x = "+x);
                x++;
            } while(x < 3);
        }
Conclusion
x = 5

for loop

The for loop is very different from the while and do-while loops. In one statement line we define initialization, condition (true or false), increment/decrement. Coffee break #154.  Three types of loops in Java.  JDK 19: New Java 19 features coming in September - 4

Syntax

For (initialization; condition; increment/decrement) {
    // Блок codeа
}

Example

public static void main(String[] args) {
     for(int i = 0 ; i < 2 ; i++) {
         System.out.println("i = " + i);
     }
}
Conclusion:
i=0 i=1
Initialization: int i = 0 is the loop initialization condition. Condition: Checks to see if the condition for running the block or code inside the for loop is true. If the condition is false then this will end the loop. Increment/decrement: update the variable for the next iteration.

Extended for loop (for-each)

There is another version of the for loop that is more compact and readable than a simple for loop. It's called an extended for loop (or for-each loop) and is used to iterate through collections and arrays. It is available to any object that implements the Iterable interface.

Example

class Main
{
    public static void main(String[] args)
    {
        int[] A = { 1, 2, 3, 4, 5 };

        for (int i: A) {
            System.out.println(i);
        }
    }
}
Conclusion:
1 2 3 4 5

JDK 19: New Java 19 features coming in September

Source: Infoworld Here is a list of new features that will appear in the upcoming JDK release. Its release is scheduled for September 20, 2022. Java Development Kit 19, a short-term support release due in September, has reached release candidate stage. There are seven features to look out for: structured parallelism, record templates, preview of foreign and memory APIs, and support for the open-source Linux/RISC-V instruction set architecture (ISA). Since new features are already frozen for JDK 19, other planned features, such as generics and value objects, are being deferred to a later version of the platform. Typically a new version of Java is released every six months. The JDK 19 release candidate was published on August 11, following two rampdown releases in June and July. The second release candidate is expected on August 25th. Early builds of JDK 19 can be downloaded from jdk.java.net/19 . JDK 19 features include:
  • Structured parallelism (during the incubation phase). It is designed to simplify multi-threaded programming using the Structured Concurrency API. This parallelism treats multiple tasks running on different threads as a single unit of work. Accordingly, this will simplify error handling and cancellation, and reliability will also be improved.

  • Preview of record templates (for parsing record values). Record patterns and type patterns can now be nested, providing a declarative, powerful, and composable form of navigation and data manipulation. The new feature includes extending pattern matching to express more complex compound data queries without changing the syntax or semantics of type patterns.

    This innovation builds on the pattern matching for instanceof introduced in JDK 16 in March 2021. Future releases may require that record templates be extended with features such as array templates and vararg templates.

    Post templates are part of the Amber project , which aims to explore and develop small, performance-oriented Java features.

  • Preview versions of the foreign and memory API functions . The new release will introduce an API that allows Java programs to interact with code and data outside the Java runtime. By efficiently calling third-party functions (that is, code outside the JVM) and securely accessing foreign memory (memory not managed by the JVM), the API will allow Java programs to call native libraries and process native data without the drawbacks of the Java Native Interface (JNI).

    The foreign and memory API combine two earlier incubation APIs: the external memory access API and the external linker API. The foreign function and memory API were previously incubated in JDK 17 and re-incubated in JDK 18.

  • Preview of Virtual threads. This feature introduces lightweight threads that greatly reduce the effort of writing, maintaining, and monitoring high-performance parallel applications. The purpose of the innovation is to provide the ability to scale server applications written in a simple thread-per-request style. The feature targets code that uses the java.lang Thread API to inject virtual threads with minimal changes, and to troubleshoot, debug, and profile virtual threads using existing JDK tools.

  • Third preview of pattern matching for switch expressions and statements. It extends pattern matching to switch, which will allow an expression to be tested against a number of patterns, each with a specific effect, so complex data-centric queries can be expressed concisely and safely.

    This feature was previously previewed in JDK 17 and JDK 18. The third preview adds improvements, including replacing protected when patterns with clauses in switch blocks. Additionally, the runtime semantics of the switch pattern when the value of the selector expression is null now supports the legacy switch semantics.

    The innovation will expand the expressiveness and applicability of switch expressions and operators by allowing patterns to appear in case labels. It will also give developers the ability to relax historical animosity toward nulls and switches when needed, improve the safety of switch statements, and ensure that existing switch statements and statements continue to compile unchanged and execute with identical semantics.

  • Fourth incubation of vector API . It will express vector computations that reliably compile at runtime into optimal vector instructions on supported processor architectures. This will provide better performance than equivalent scalar calculations. Developers using the API gain the ability to write complex vector algorithms in Java using HotSpot's autovectorizer, but with a custom model that makes vectorization more predictable and reliable. The Vector API was previously incubated in JDK 16, JDK 17, and JDK 19.

    The Vector API will extend bitwise integral linear operations, including operations such as counting the number of one's bits, reversing the order of bits, and compressing and expanding bits.

  • The Linux/RISC-V Java port will gain hardware instruction set support that is already supported by a wide range of language toolkits. The Linux/RISC-V port will now support the RV64GV RISC-V configuration, a 64-bit general purpose ISA that includes vector instructions.

    The port will also support the following HotSpot virtual machine options: template interpreter, C1 JIT compiler (client), C2 JIT compiler (server), and all current major garbage collectors, including ZGC and Shenandoah.

Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION