Top features in Java 11 for Developers

March 25, 2020 | No comments

New Features for Java 11

Following articles will show important features available to use for developers in JDK 11. It mostly covers the new methods available to write better code.
Notable updates for developers
  • New methods in String class.
  • New methods to read/write Files.
  • Default method toArray in Collection.
  • Unicode 10 Support.
  • Standard HTTP Client.

New Methods in String class

String::repeat(n)
Using this method we can build a new string which concatenation of the original string n times.
String  xyz = "xyz";
String r = xyz.repeat(4); //xyzxyzxyzxyz
String::lines()
This method is to return a collection of strings which are spliced by line terminators.
String multiLineString = "mulitiline paragraph this is line one.\n this is line two.";
multiLineString.lines().forEach(System.out::println);
//prints
mulitiline paragraph this is line one.
this is line two.
    
String::isBlank
Returns true if the string is empty or contains only white space. For example in the following code userName is not empty as it's length is not 0, but it contains only white space. IsBlank will return true.
String userName = "  ";
if (userName.isBlank()) {
      System.out.println("invalid userName");
 }
String:: strip
Strip the white spaces from the beginning and end of the string.
System.out.println("password::" + passowrd.strip() + "::");
String::stripLeading and String::stripTrainling
stripLeading will remove white space from the begining of the String and stripTrainling will remove white space from the tail of the String.
String passowrd = " abcde ";
System.out.println("password::" + passowrd.stripLeading()+ "::");  // "abcd  "
System.out.println("password::" + passowrd.stripTrailing()+ "::");   // "  abcd"

Collection toArray

New default method toArray in Collection Class returns an array of the corresponding length based on the length of the input data.
List list = Arrays.asList("UserOne", "UserTwo", "UserThree", "UserFour", "UserFive");
String arr[] = list.toArray(String[]::new);

New File Methods

Files.readString and Files.writeString
Following example method reads a file named record.txt and updates the content of the File using the two new methods in Java 11.
public void filesMethod (String updateContent) throws IOException {
    String studentFileContent = Files.readString(Path.of("record.txt"));
    Files.writeString(Path.of("record.txt"),updateContent);
  }
New Optional::isEmpty method
Useful method to check if an optional contained object is null. It is reverse of Optional:: isPresent();
Optional opt = Optional.empty();
if (str.isEmpty()) {
System.out.println("opt is empty");
}

Not all the features introduced in Java 11 are covered here, and will be covered later in a different article.
Other notable features
  • Launch Single-File Source-Code Programs
  • Standard HTTP Client
  • Epsilon Garbage Collector
  • Low-overhead Heap Profiling
  • Improved KeyStore Mechanisms
  • Z Garbage Collector
  • Dynamic Allocation of Compiler Threads

No comments :

Post a Comment

Please leave your message queries or suggetions.