/Java11-AND-Java17

Java 17 was released on September 14, 2021. Java 17 is an LTS (Long Term Support) release, like Java 11 and Java 8. Oracle will support it for bug fixes, patches and performance enhancements for the next few years.

Primary LanguageJava

Dive Into Java11 & Java17

For Java 11 we need to download Amazon Corretto - https://docs.aws.amazon.com/corretto/latest/corretto-11-ug/downloads-list.html Corretto is a distribution of Open JDK with patches included by Amazon that are not yet integrated in the corresponding OpenJDK update projects. We focus on patches that improve performance or stability in OpenJDK, chosen based on Amazon's observations running large services.

For Java 17 we need to download Java SE Development Kit 17.0.1 - https://www.oracle.com/java/technologies/downloads/#jdk17-windows

Frequently used Java features are covered as part of this page

Java 11 Features :

Running Java File with single command


java "C:\Users\Yashwanth\Documents\Alpha Geek\Yash_World\JDK11_17\src\com\yash\java\HelloJava11.java"

Java String Methods

  • IsBlank()

    This instance method returns a boolean value. Empty Strings and Strings with only white spaces are treated as blank.


    System.out.println(" ".isBlank()); //true
    String yash = "Yashwanth";
    System.out.println(yash.isBlank()); //false
    String s1 = "";
    System.out.println(s1.isBlank()); //true
  • lines()

    This method returns a stream of strings, which is a collection of all substrings split by lines.


     String val = "Yashwanth\nAlphaGeek\nVampire"; 
     System.out.println(val);
     System.out.println(val.lines().collect(Collectors.toList()));
  • strip(), stripLeading(), stripTrailing()

    Removes the white space from both, beginning and the end of string.


        String firstName=" Yashwanth ";
	String lastName=".P";
	System.out.println("Before stripping:"+firstName+lastName);
	System.out.println("After stripping:"+firstName.strip()+lastName);
  • repeat(int)

    The repeat method simply repeats the string that many numbers of times as mentioned in the method in the form of an int.


        String repeatStr = "Never Give Up\n";
        String repeatVal = repeatStr.repeat(2);
	System.out.println(repeatVal);

Local-Variable Syntax for Lambda Parameters

Declaring of formal parameters of an implicitly typed lambda expression

        Adder adder=(double a, int b)->((int)a+b);
	System.out.println(adder.add(10, 10));

Nested Based Access Control


public class Main {
 
    public void myPublic() {
    }
 
    private void myPrivate() {
    }
 
    class Nested {
 
        public void nestedPublic() {
            myPrivate();
        }
    }
}

private method of the main class is accessible from the above-nested class in the above manner.

HTTP Client

Java 11 standardizes the Http CLient API. The new API supports both HTTP/1.1 and HTTP/2. It is designed to improve the overall performance of sending requests by a client and receiving responses from the server. It also natively supports WebSockets.

Reading/Writing Strings to and from the Files

Java 11 strives to make reading and writing of String convenient

  • readString()
  • writeString()

        Path path = Files.writeString(Files.createTempFile("test", ".txt"), "Yashwanth");
	System.out.println(path);
	String s = Files.readString(path);
	System.out.println(s);

Java 17 Features :

Pattern Matching for switch

We can reduce the if else statement into switch case as shown below

  • if…else chain

            public static String test(Object obj) {
	    return switch(obj) {
	    case Integer i -> "It is an integer";
	    case String s -> "It is a string";
	    default -> "It is none of the known data types";
	    };
  • null

            public static String test(Object obj) {
	    return switch(obj) {
	    case Integer i -> "It is an integer";
	    case String s -> "It is a string";
	    case null -> "Null Pointer Exception";
	    default -> "It is none of the known data types";
	    };
  • Refining patterns in switch

            public static String test(Object obj) {
	    return switch(obj) {
	    case Integer i -> "It is an integer";
	    case String s -> "It is a string";
	    case null -> "Null Pointer Exception";
	    case Boolean bool && bool==true -> performOperation(bool);	    
	    default -> "It is none of the known data types";
	    };
	}
	
	public static String performOperation(Boolean bool)
	{
		return "Boolean is "+bool;
	}

Info :

Pattern matching for switch statements and expressions. Since this is a preview feature, we need to use --enable-preview option to enable it.

Tips :

Standard Naming Conventions in Java

Package : Always lower case, use your internet domain name, reversed

Eg : java.lang

Classes : CamelCase , Class names should be nouns, Start with capital letter

Eg : LinkedList, Main

Inerfaces : CamelCase , Consider what objects implementing the interface will become of what they will be able to do

Eg : Comparbale

Methods : mixedCase, Verbs

Eg : getName()

Constants : All Uppercase, Separate words with underscore, Declared with final keyword

Eg : Static final int MAX_NUMB

Variables : mixedCase, Meaningful and indicative, Start with lower case letter, Do not use underscore

Eg : league

Type Parameters : Single Character, captial letters

Eg : E - Element (used extensively by the Java Collections Framework)

“Thanks for watching. If you liked this page, make sure to subscribe for more!”

First, solve the problem. Then, write the code. 

😀