java9 升级功能

(Evaluation of Java:)

  1. In java 1.7, things are very slow as compared to other JDK languages like Scala, Kotlin. when Java 8 releases, it changes the entire landscape of Java because functional programming introduced with Lambdas, Stream API, and Completable Future.
    在Java 1.7中, Scala,Kotlin 等其他JDK语言相比,运行速度非常慢Java 8发行时,它改变了Java的整个格局,因为Lambdas,Stream API和Completable Future引入了功能性编程。
  2. Till now Java 9 is the biggest release even-though Java 8 changes the entire landscape, because it changed the complete ecosystem of the Java and we can build and deploy the applications with it.
    到目前为止,尽管Java 8改变了整个格局,但Java 9仍是最大的发行版 ,因为Java 8改变了Java 的完整生态系统,我们可以使用它构建和部署应用程序。
  3. Release cycle of the JAVA: The Release cycle of the java changes from JAVA 9, the oracle is releasing the JAVA every 6 months. it doesn’t mean JAVA is developing every 6 months. Features are taking years to develop with out compromise in testing and quality, what ever the features they would complete at that release point of time, they will release that features Instead of waiting for other features to complete.
    JAVA的发布周期:Java的发布周期与JAVA 9有所不同, oracle每6个月发布一次JAVA 。 这并不意味着JAVA每6个月就会开发一次。 开发功能要花费数年的时间,而又不会在测试和质量上做出任何妥协, 无论在发布时间点他们将完成什么功能,他们都会发布该功能,而不是等待其他功能完成

(Java 9 Features:)

I categorized Java 9 Features into 3 categories

我将Java 9功能分为3类

  1. Improvements
  2. Additional Features
  3. Advanced Features

In this blog we will Focus on Improvements , Remaining will be published in next part.

在此博客中,我们将重点关注改进,其余内容将在下一部分中发布。

(Improvements:)

(1. Private Methods In Interfaces:)

  1. Until Java 7, Interface is the body of the bundle of Unimplemented methods.
    Java 7之前 ,Interface是未实现方法包主体。
  2. In Java 8 , Default and static methods are implemented in Interfaces just like classes. Generally in classes, what ever the common code we have used in the methods can be written as private methods.
    在Java 8中,与类一样, 在接口中实现Default和static方法 。 通常在类中,我们在方法中使用的通用代码可以写为私有方法。
  3. If we need to use the common code in these static and Default methods in Interface, where we need to write that common code ???
    如果需要在Interface的这些static和Default方法中使用通用代码,则需要在其中编写该通用代码?
    Java 9 provided the solution
    Java 9提供了解决方案
    with the improvement of private methods in interfaces, it provides two types of private methods
    随着接口私有方法的改进 ,它提供了两种类型的私有方法。
    1. normal
    1.正常
    private methods
    私人方法
    2.
    2。
    private static methods
    私有静态方法

private static methods are used in the static methods and normal private methods are used in default methods of Interface

静态方法中使用private静态方法,而Interface的默认方法中使用普通的private方法




java 代码 update语句 拼接 java update available_java


private methods in interfaces 接口中的私有方法

NOTE: I recommend you to not use this feature more in the real time because your interface is blotted with full of code which is not good for understand.

注意:我建议您不要实时使用此功能,因为您的界面上充满了很多代码,不便于理解。

(2. Try with Resources:)

  1. when the Java was introduced, At that point of time the most trending language is C++. The painful point in C++ is garbage collection.
    引入Java时,当时最流行的语言是C ++。 C ++中最痛苦的一点是垃圾回收
  2. The advantage and disadvantage of the JAVA is the garbage collection because we don’t know at what time it was invoked.
  3. Generally, Programming requires of two types of garbage collection
    通常,编程需要两种类型的垃圾回收
    (a) Memory in
    (a)记忆在
    the Runtime
    运行时
    (b)
    (b)
    External Resources (Files, Database and socket connections etc).
    外部资源(文件,数据库和套接字连接等)。


code before java 1.7 to close external resources

Java 1.7之前的代码以关闭外部资源

java 代码 update语句 拼接 java update available_python_02

output for above code

以上代码的输出

if we observe the above code, finalize method is not invoked. Here Managing memory in runtime and Managing external resources both are two different things (which are conflict each other) but Oracle developers thought they will solve these conflict things with the single solution which doesn’t work in the long run.There is anthor method in JDK which is not usable at all i.e is System.gc()Don’t use this because there is no use in it.

如果我们观察以上代码, 则不会调用finalize方法 。 这里在运行时管理内存和管理外部资源都是两个不同的东西(彼此冲突),但是Oracle开发人员认为他们将使用单一的解决方案来解决这些冲突的问题,该解决方案从长远来看是行不通的。根本无法使用的JDK,即System.gc()不要使用它,因为其中没有用。

Good News is In JAVA 9, finalize() method is deprecated

好消息是在Java 9中,不建议使用finalize()方法

In life and also in programming we solve the problems is to create the new problems

在生活中以及在编程中,我们解决问题的方法就是创造新问题

Here they solved the Garbage collection for memory in runtime. but they created the problem for external resources.

在这里,他们解决了运行时内存中的垃圾回收问题。 但是他们为外部资源带来了问题。

Java 1.7 provides the solution with try- with- resources

Java 1.7提供具有尝试资源的解决方案


java 代码 update语句 拼接 java update available_java 代码 update语句 拼接_03

JDk 1.7 to close external Resources JDk 1.7关闭外部资源


java 代码 update语句 拼接 java update available_JAVA_04

Output for above code 以上代码的输出

try(Resource res = new Resource())

In 1.7 , They solved the problem with external Resources but it leads to create a anthor problem. we observed the code block, we are making Resource Object is tightly coupled(Bad coding practice).

在1.7中,他们使用外部资源解决了问题,但是这导致了另一个问题。 我们观察到代码块,我们正在使资源对象紧密耦合(错误编码实践)。

Due to this tight coupling , it is very difficult to automate the testcases with Mock or Stub

由于这种紧密的耦合,使用Mock或Stub自动化测试用例非常困难。

In Java 9 , they solved the problem

在Java 9中,他们解决了问题


code with Java 9 to close external Resources Java 9编写代码以关闭外部资源

java 代码 update语句 拼接 java update available_JAVA_04

Output of the above code 上面代码的输出

In Java9, they solved the External Resource tightly coupling problem. Now caller can send Resource Object or any extension class of the Resource Object or Resource Mock Object. but it will leads to anthor problem.

在Java9中,他们解决了外部资源紧密耦合的问题。 现在,调用者可以发送资源对象或资源对象或资源模拟对象的任何扩展类。 但这会导致其他问题。


still resource object is accessible even it is closed 仍然可以访问资源对象,即使它已关闭

  1. Before Java 9, Resource Object is not accessible out side the try block, But In Java 9 , Resource Object is still accessible for some time even it is closed. we must be take care while writing the code
    在Java 9之前, 不能在try块之外访问Resource Object ,但是在Java 9中, 即使关闭了Resource Object,仍然可以访问一段时间 。 我们在编写代码时必须小心


object used in try block is not mutable 尝试块中使用的对象是不可变的

2. Object used in the try block is final or effectively final and it is not allowed to mutate it .

2. try块中使用的对象是final或实际上是final,并且不允许对其进行突变。

(3. Usage of _ as variable name is completely removed in Java 9)

  1. _ is having lot of importance in different programming languages like scala,kotlin etc
    _ 诸如scala,kotlin等不同的编程语言中非常重要
  2. In Java 8, usage of _ as variable showed as warning and In Java 9, they completely removed it .
    在Java 8中, 将_用作变量显示为警告 ,在Java 9中, 他们将其完全删除。


java 代码 update语句 拼接 java update available_JAVA_06

_ removed in java 9 _在Java 9中删除

(4. Take while and Drop while In Streams)

we all know that In Java 8 , Streams API completely changes the landscape of the Java. Before Discuss about the Java9 improvement, let’s understand the existing methods in Java 8, after that we will clearly understand the improvement in Java 9.

我们都知道,在Java 8中,Streams API完全改变了Java的格局。 在讨论Java9改进之前,让我们了解Java 8中的现有方法 ,之后我们将清楚地了解Java 9中的改进。


java 代码 update语句 拼接 java update available_JAVA_07

Java 8 Stream functionalities Java 8 Stream功能

java 代码 update语句 拼接 java update available_python_08

Output of the above program 上述程序的输出

Observations from the above program :

以上程序的观察结果

  1. filter takes a Predicate . It is like a gate, that opens or closes per element
    过滤器采用谓词就像一扇门,每个元素都会打开或关闭
  2. limit takes a integer. it is like a door, that is open. but once it is closed then it may closes for ever
    限制整数就像一扇门,那是开着的。 但是一旦关闭,它可能永远关闭
  3. skip takes a integer. it is also like a door, that is closed. but once it is opened that it may opened forever.(opposite to limit).
    skip需要一个整数它也像一扇门,是关着的。 但是一旦打开,它可能会永远打开 。(与限制相反)。

observe the limit and skip functions , it takes integer as input.

遵守限制和跳过功能 ,它以整数作为输入。

lets think about below use cases. Use Case: 1

让我们考虑以下用例用例:1

List<Integer> numbers = Arrays.asList(11,12,13,14,51,16,17,81,91)I need to limit this  list until number < 50

Now we need to limit the list based on the condition , not on the number. How we will solve this use case in Java 8 ???solution with imperative style of coding is simple:

现在,我们需要根据条件而不是数量来限制列表。 我们将如何解决Java 8中的这种用例? 具有命令式编码风格的解决方案很简单:

for(int number : numbers){
   if(number > 50)break;System.out.println(number);
}

How can you solve this use case with FUNCTIONAL PROGRAMMING In Java 8 ???? — it’s not possible in Java 8

如何使用 Java 8中的FUNCTIONAL PROGRAMMING解决此用例 ? —在Java 8中是不可能的

To Solve this use case take-While(Predicate) Comes in to the picture in Java 9

为了解决此用例, 在Java 9中使用While(Predicate)进入图片

numbers.stream()
         .takeWhile(e -> e<50)
         .forEach(System.out::println);

Use Case:2

用例:2

List<Integer> numbers = Arrays.asList(11,12,13,14,51,16,17,81,91)I need to skip this  list until number < 50

Now we need to Skip the list based on the condition , not on the number. How we will solve this use case in Java 8 ???solution with imperative style of coding is simple:

现在,我们需要根据条件而不是数字来跳过列表。 我们将如何解决Java 8中的这种用例? 具有命令式编码风格的解决方案很简单:

boolean isAllowed = false;
for(int number : numbers){
   if(number < 50 && !isAllowed) {
        continue;
   }
  isAllowed = true;System.out.println(number);
}

How can you solve this use case with FUNCTIONAL PROGRAMMING In Java 8 ???? — it’s not possible in Java 8

如何使用 Java 8中的FUNCTIONAL PROGRAMMING解决此用例 ? —在Java 8中是不可能的

To Solve this use case drop-While(Predicate) Comes in to the picture in Java 9

解决此用例drop-While(Predicate)进入Java 9中的图片

numbers.stream()
         .dropWhile(e -> e<50)
         .forEach(System.out::println);


implementation of takewhile and dropwhile 实施takewhile和dropwhile

output of the above code 上面代码的输出

NOTE: break(imperative) = takeWhile(functional)

注意:break(命令)= takeWhile(功能)

Haskell vs Java Naming convention

Haskell vs Java命名约定

Haskell ----------------------- JAVA
   take(number) ------------------ limit(int)
   drop(number) ------------------ skip(int)
   takeWhile(Predicate) ---------- takeWhile(Predicate)
   dropWhile(Predicate) --------   dropWhile(Predicate)

(5. iterate method in streams)

Before we discuss about the iterate method, Just Imagine you and your friend start debate between imperative and functional style implementations. you are supporting functional style of coding . Let us see Both of you giving counter implementations in imperative and functional style in Java 8 .

在我们讨论迭代方法之前,请想象一下您和您的朋友开始在命令式函数式实现之间进行辩论。 您正在支持编码的功能样式 。 让我们看看你们两个都在Java 8中给出了命令式和函数式的计数器实现

Imperative style :

命令式:

for(int i=0; i<5; i++) {
 }

Functional style :

功能风格

IntStream.range(0,5)
        .forEach(System.out::println);

Imperative style :

命令式:

for(int i=0; i<=5; i++) {
}

Functional Style :

功能风格:

IntStream.rangeClosed(0,5)
        .forEach(System.out::println);

Imperative

势在必行

for(int i=0; i<=5; i= i+2) {
}

Functional style in Java 8 ???? — you lost the debate

Java 8中的功能样式 -您输掉了辩论

  1. To Solve the above use case iterate method comes in to the picture in Java 9
    为了解决上述用例迭代方法,Java 9中引入了图片
  2. Most Commonly used interfaces in Functional style of Coding is Predicate,Supplier, Consumer and Function
    函数式编码中最常用的接口是谓词,供应商,使用者和函数
    Let’s See iterate method is how intuitive it is
    让我们看看迭代方法的直观性
for(int i=0; i<=5; i= i+2) {
}
for(seed,predicate,function) {
}
//Java 9:
IntStream.iterate(seed,predicate,function)

Imperative and functional style implementations in JAVA 9

JAVA 9中的命令式和功能性样式实现

// imperative 
for(int i=0; i<=5; i= i+2) {
}// functionalIntStream.iterate(0,i-> i<=5, i-> i+2)
        .forEach(System.out::println);

Infinite Loop Implementation In Imperative and functional style implementations in Java 9

Java 9中命令式和函数式实现中的无限循环实现

// imperative 
for(int i=0; ; i= i+2) {
}// functionalIntStream.iterate(0,i-> i+2)
        .forEach(System.out::println);

Note : In iterate method Predicate is optional Parameter , if you did not mention Predicate , it becomes an infinite loop

注意:在迭代方法中,谓词是可选参数,如果您未提及谓词,它将成为无限循环

In the Blog (Part-2) we concentrated on additional features of Java 9 in

在博客(第2部分)中,我们集中讨论了Java 9中的其他功能。


In the Upcoming Blog (Part-3) we will concentrate on advanced features of Java 9

在即将到来的博客(第3部分)中,我们将重点介绍Java 9的高级功能。

  • JShell
  • Modularization (it changes the entire landscape of Java)
  • Stack Walker API

(Stay Tuned…….)

(Follow this Publication for timely updates)

(Medium :)

https://medium.com/art-of-coding

https://medium.com/art-of-coding

连结在: (Linked in :)

https://www.linkedin.com/company/art-of-coding/

https://www.linkedin.com/company/art-of-coding/