Java 11 new features

Java 11 organizational changes

Java 11 did a lot of house-keeping. Java EE, CORBA, and Java FX have been removed from JDK. They will be available from Maven repositories. JavaScript Nashorn engine has been deprecated. Java applets have been removed for good.


Dowloading Java 11

We download either?OpenJDK?or?Oracle JDK.

IntelliJ IDEA 2018.2.4 Community Edition already has support for Java 11.

$ ~/bin/jdk-11/bin/java --version
openjdk 11 2018-09-25
OpenJDK Runtime Environment 18.9 (build 11+28)
OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)        

In our examples we have used OpenJDK.

Java 11 - launching single-file source files

It is possible to launch single-file Java source files without prior compilation with?javac. This helps new programmers learn the basics of Java and promotes creating simpler programs.

We do not clutter up our space with byte-code files and we do not need to worry about Java packaging rules.

No alt text provided for this image

This is a simple Java source file. Note that the file does not have to be located in?com/zetcode?subdirectory.



$ ~/bin/jdk-11/bin/java SimpleEx.java
Java 11 example        

We launch the program with?java?tool.

HttpClient standardized

The new HttpClient has been standardized. It is located in the?java.net.http?package.

No alt text provided for this image

In the example, we create a new http client. Then we generage an asynchronous HTTP request to webcode.me website.

$ ~/bin/jdk-11/bin/java HttpClientEx.java
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>My html page</title>
</head>
<body>

    <p>
        Today is a beautiful day. We go swimming and fishing.
    </p>
    
    <p>
         Hello there. How are you?
    </p>
    
</body>
</html>        

The server responds with this HTTP file.


Java 11 new string methods

There are new?String?methods in Java 11.

No alt text provided for this image

In the example, we demonstrate the usage of the new?String?methods.

System.out.println(word.repeat(5));        

The?repeat()?method returns the string repeated n times.

System.out.println(word2.stripTrailing() + "sky");
System.out.println(word2.stripLeading() + "sky");
System.out.println(word2.strip() + "sky");        

The?stringTailing()?method returns the string with all trailing white space removed. The?stringTailing()?method returns the string with all leading white space removed. The?stringTailing()?method returns the string with all leading and trailing white space removed.

System.out.println(word3.isBlank());        

The?isBlank()?returns true if the string is empty or contains only white space.

words.lines().forEach(System.out::println);        

The?lines()?method returns a stream of lines extracted from the string, separated by line terminators.

$ ~/bin/jdk-11/bin/java StringMethodsEx.java
falcon falcon falcon falcon falcon 
    nice blue   sky
    nice bluesky
nice blue   sky
nice bluesky
false
true
falcon
eagle
sky
wood
forest        

This is the output.

The asMatchPredicate method

There is a new?asMatchPredicate?method for working with regular expressions.

No alt text provided for this image

The?asMatchPredicate()?method creates a new predicate from the compiled pattern. On the predicate we call the?test()?method.

$ ~/bin/jdk-11/bin/java AsMatchPredicateEx.java
dog matches
Dog matches
DOG matches
Doggy does not match        

This is the output.

Files readString and writeString

The?readString()?method reads all content from a file into a string and the?writeString()?method writes a?CharSequence?to a file.

No alt text provided for this image

In this example, we write four words into the?words.txt?file.

No alt text provided for this image

In this example, we read the contents of the?words.txt?file and write it to the console.

$ ~/bin/jdk-11/bin/java ReadStringEx.java
forest
wood
sky
rock        

This is the output.

要查看或添加评论,请登录

Omar Ismail的更多文章

社区洞察

其他会员也浏览了