java se面试题_Java SE 8面试问答(第1部分)

java se面试题


In this post, we are going to discuss some important Java SE 8 Interview Questions with Answers. I will write one more post to discuss the remaining Java SE 8 Interview Questions.


在本文中,我们将讨论一些重要的Java SE 8面试问题和答案。 我将再写一篇文章,讨论其余的Java SE 8面试问题。

Java 8面试问题 (Java 8 Interview Questions)

  1. Why do we need change to Java again?

    为什么我们需要再次更改为Java?
  2. Java SE 8 New Features?

    Java SE 8的新功能?
  3. Advantages of Java SE 8 New Features?

    Java SE 8的优点新功能?
  4. What is Lambda Expression?

    什么是Lambda表达式?
  5. What are the three parts of a Lambda Expression? What is the type of Lambda Expression?

    Lambda表达式的三个部分是什么? Lambda表达式的类型是什么?
  6. What is a Functional Interface? What is SAM Interface?

    什么是功能接口? 什么是SAM接口?
  7. Is is possible to define our own Functional Interface? What is @FunctionalInterface? What are the rules to define a Functional Interface?

    是否可以定义我们自己的功能接口? 什么是@FunctionalInterface? 定义功能接口的规则是什么?
  8. Is @FunctionalInterface annotation mandatory to define a Functional Interface? What is the use of @FunctionalInterface annotation? Why do we need Functional Interfaces in Java?

    是否必须使用@FunctionalInterface批注来定义功能接口? @FunctionalInterface注释的用途是什么? 为什么我们需要Java中的功能接口?
  9. When do we go for Java 8 Stream API? Why do we need to use Java 8 Stream API in our projects?

    什么时候使用Java 8 Stream API? 为什么我们需要在项目中使用Java 8 Stream API?
  10. Explain Differences between Collection API and Stream API?

    解释Collection API和Stream API之间的区别?
  11. What is Spliterator in Java SE 8?Differences between Iterator and Spliterator in Java SE 8?

    Java SE 8中的Spliterator是什么?Java SE 8中的Iterator和Spliterator之间的区别是什么?
  12. What is Optional in Java 8? What is the use of Optional?Advantages of Java 8 Optional?

    Java 8中的Optional是什么? Optional的用途是什么?Java 8 Optional的优点?
  13. What is Type Inference? Is Type Inference available in older versions like Java 7 and Before 7 or it is available only in Java SE 8?

    什么是类型推断? 类型推断是否在Java 7和7之前的较早版本中可用,还是仅在Java SE 8中可用?

Java 8面试问答 (Java 8 Interview Questions and Answers)

In this section, we will pick up each question from the previous section and answer it with an in-detailed description. If you need any more information and examples, please go through previous Java SE 8 posts available in JournalDEV.

在本节中,我们将从上一节中选取每个问题,并以详细的说明回答。 如果您需要更多信息和示例,请阅读JournalDEV中提供的Java SE 8以前的文章。

为什么我们需要再次更改为Java? (Why do we need change to Java again?)

Oracle Corporation has introduced a lot of new concepts in Java SE 8 to introduce the following benefits:

Oracle Corporation在Java SE 8中引入了许多新概念,以带来以下好处:

  • To Utilize Current Multi-Core CPUs Efficiently

    有效利用当前的多核CPU
  • Recently, we can observe drastic changes in Hardware. Nowadays, all systems are using Multi-Core CPUs (2,4,8,16-Core, etc.) to deploy and run their applications. We need new Programming Constructs in Java to utilize these Multi-Core Processors efficiently to develop Highly Concurrently and Highly Scalable applications.

    最近,我们可以观察到硬件的巨大变化。 如今,所有系统都在使用多核CPU(2、4、8、16核等)来部署和运行其应用程序。 我们需要Java中的新编程构造来有效利用这些多核处理器来开发高度并发和高度可扩展的应用程序。

  • To Utilize FP Features

    利用FP功能
  • Oracle Corporation has introduced a lot of FP(Functional Programming) concepts as part of Java SE 8 to utilize the advantages of FP.

    作为Java SE 8的一部分,Oracle Corporation引入了许多FP(功能性编程)概念,以利用FP的优势。

Java SE 8的新功能? (Java SE 8 New Features?)

  • Lambda Expressions

    Lambda表达式
  • Functional Interfaces

    功能介面
  • Stream API

    流API
  • Date and Time API

    日期和时间API
  • Interface Default Methods and Static Methods

    接口默认方法和静态方法
  • Spliterator

    分流器
  • Method and Constructor References

    方法和构造方法参考
  • Collections API Enhancements

    集合API增强功能
  • Concurrency Utils Enhancements

    并发实用程序增强
  • Fork/Join Framework Enhancements

    叉/联接框架增强
  • Internal Iteration

    内部迭代
  • Parallel Array and Parallel Collection Operations

    并行阵列和并行收集操作
  • Optional

    可选的
  • Type Annotations and Repeatable Annotations

    类型注释和可重复注释
  • Method Parameter Reflection

    方法参数反映
  • Base64 Encoding and Decoding

    Base64编码和解码
  • IO and NIO2 Enhancements

    IO和NIO2增强
  • Nashorn JavaScript Engine

    Nashorn JavaScript引擎
  • javac Enhancements

    javac增强
  • JVM Changes

    JVM更改
  • Java 8 Compact Profiles: compact1,compact2,compact3

    Java 8 Compact概要文件:compact1,compact2,compact3
  • JDBC 4.2

    JDBC 4.2
  • JAXP 1.6

    JAXP 1.6
  • Java DB 10.10

    Java DB 10.10
  • Networking

    联网
  • Security Changes

    安全变更

Java SE 8的优点新功能? (Advantages of Java SE 8 New Features?)

We can get the following benefits from Java SE 8 New Features:

我们可以从Java SE 8新功能中获得以下好处:

  • More Concise and Readable code

    更简洁易读的代码
  • More Reusable code

    更多可重用的代码
  • More Testable and Maintainable Code

    更具可测试性和可维护性的代码
  • Highly Concurrent and Highly Scalable Code

    高度并行和高度可扩展的代码
  • Write Parallel Code

    编写并行代码
  • Write Database Like Operations

    编写类似数据库的操作
  • Better Performance Applications

    性能更好的应用
  • More Productive code

    更高效的代码

什么是Lambda表达式? (What is Lambda Expression?)

Lambda Expression is an anonymous function which accepts a set of input parameters and returns results.

Lambda Expression是一个匿名函数,它接受一组输入参数并返回结果。

Lambda Expression is a block of code without any name, with or without parameters and with or without results. This block of code is executed on demand.

Lambda表达式是没有任何名称,带有或不带有参数以及带有或不带有结果的代码块。 此代码块按需执行。

Lambda表达式的三个部分是什么? Lambda表达式的类型是什么? (What are the three parts of a Lambda Expression? What is the type of Lambda Expression?)

A Lambda Expression contains 3 parts:

Lambda表达式包含3个部分:

  • Parameter List

    参数表
  • A Lambda Expression can contain zero or one or more parameters. It is optional.

    Lambda表达式可以包含零个或一个或多个参数。 它是可选的。

  • Lambda Arrow Operator

    Lambda箭头运算符
  • “->” is known as Lambda Arrow operator. It separates the parameters list and body.

    “->”被称为Lambda Arrow运算符。 它将参数列表和主体分开。

  • Lambda Expression Body

    Lambda表达主体

The type of “Journal Dev” is java.lang.String. The type of “true” is Boolean. In the same way, what is the type of a Lambda Expression?
The Type of a Lambda Expression is a Functional Interface.

“ Journal Dev”的类型为java.lang.String。 “ true”的类型是布尔值。 同样,Lambda表达式的类型是什么?
Lambda表达式的类型是功能接口 。

Example:- What is the type of the following Lambda Expression?

示例:-以下Lambda表达式的类型是什么?

() -> System.out.println("Hello World");

This Lambda Expression does not have parameters and does return any results. So it’s type is “java.lang.Runnable” Functional Interface.

此Lambda表达式没有参数,并且会返回任何结果。 所以它的类型是“ java.lang.Runnable”功能接口。

什么是功能接口? 什么是SAM接口? (What is a Functional Interface? What is SAM Interface?)

A Functional Interface is an interface, which contains one and only one abstract method. Functional Interface is also known as SAM Interface because it contains only one abstract method.

功能接口是一个接口,它仅包含一个抽象方法。 功能接口也称为SAM接口,因为它仅包含一种抽象方法。

SAM Interface stands for Single Abstract Method Interface. Java SE 8 API has defined many Functional Interfaces.

SAM接口代表单一抽象方法接口。 Java SE 8 API定义了许多功能接口。

是否可以定义我们自己的功能接口? 什么是@FunctionalInterface? 定义功能接口的规则是什么? (Is is possible to define our own Functional Interface? What is @FunctionalInterface? What are the rules to define a Functional Interface?)

Yes, it is possible to define our own Functional Interfaces. We use Java SE 8’s @FunctionalInterface annotation to mark an interface as Functional Interface.

是的,可以定义我们自己的功能接口。 我们使用Java SE 8的@FunctionalInterface批注将接口标记为Functional Interface。

We need to follow these rules to define a Functional Interface:

我们需要遵循以下规则来定义功能接口:

  • Define an interface with one and only one abstract method.

    用一个并且只有一个抽象方法定义一个接口。
  • We cannot define more than one abstract method.

    我们不能定义多个抽象方法。
  • Use @FunctionalInterface annotation in interface definition.

    在接口定义中使用@FunctionalInterface批注。
  • We can define any number of other methods like Default methods, Static methods.

    我们可以定义许多其他方法,例如默认方法,静态方法。
  • If we override java.lang.Object class’s method as an abstract method, which does not count as an abstract method.

    如果我们将java.lang.Object类的方法重写为抽象方法,则该方法不算作抽象方法。

是否必须使用@FunctionalInterface批注来定义功能接口? @FunctionalInterface注释的用途是什么? 为什么我们需要Java中的功能接口? (Is @FunctionalInterface annotation mandatory to define a Functional Interface? What is the use of @FunctionalInterface annotation? Why do we need Functional Interfaces in Java?)

It is not mandatory to define a Functional Interface with @FunctionalInterface annotation. If we don’t want, We can omit this annotation. However, if we use it in Functional Interface definition, Java Compiler forces to use one and only one abstract method inside that interface.

使用@FunctionalInterface注释定义功能接口不是强制性的。 如果我们不想,我们可以省略该注释。 但是,如果我们在“功能接口”定义中使用它,则Java编译器会强制在该接口内部使用一种且仅一种抽象方法。

Why do we need Functional Interfaces? The type of a Java SE 8’s Lambda Expression is a Functional Interface. Whereever we use Lambda Expressions that means we are using Functional Interfaces.

为什么需要功能接口? Java SE 8的Lambda表达式的类型是功能接口。 无论我们在哪里使用Lambda表达式,都意味着我们在使用功能接口。

什么时候使用Java 8 Stream API? 为什么我们需要在项目中使用Java 8 Stream API? (When do we go for Java 8 Stream API? Why do we need to use Java 8 Stream API in our projects?)

When our Java project wants to perform the following operations, it’s better to use Java 8 Stream API to get lot of benefits:

当我们的Java项目想要执行以下操作时,最好使用Java 8 Stream API来获得很多好处:

  • When we want perform Database like Operations. For instance, we want perform groupby operation, orderby operation etc.

    当我们想执行数据库操作时。 例如,我们要执行groupby操作,orderby操作等。
  • When want to Perform operations Lazily.

    想要懒惰地执行操作时。
  • When we want to write Functional Style programming.

    当我们要编写函数式编程时。
  • When we want to perform Parallel Operations.

    当我们要执行并行操作时。
  • When want to use Internal Iteration

    何时要使用内部迭代
  • When we want to perform Pipelining operations.

    当我们要执行流水线操作时。
  • When we want to achieve better performance.

    当我们想获得更好的性能时。

解释Collection API和Stream API之间的区别? (Explain Differences between Collection API and Stream API?)

S.No. Collection API Stream API
1. It’s available since Java 1.2 It is introduced in Java SE 8
2. It is used to store Data (A set of Objects). It is used to compute data (Computation on a set of Objects).
3. We can use both Spliterator and Iterator to iterate elements. We can use forEach to performs an action for each element of this stream. We can’t use Spliterator or Iterator to iterate elements.
4. It is used to store unlimited number of elements. Stream API is used to process on the elements of a Collection.
5. Typically, it uses External Iteration concept to iterate Elements such as Iterator. Stream API uses internal iteration to iterate Elements, using forEach methods.
6. Collection Object is constructed Eagerly. Stream Object is constructed Lazily.
7. We add elements to Collection object only after it is computed completely. We can add elements to Stream Object without any prior computation. That means Stream objects are computed on-demand.
8. We can iterate and consume elements from a Collection Object at any number of times. We can iterate and consume elements from a Stream Object only once.
序号 集合API 流API
1。 从Java 1.2开始可用 在Java SE 8中引入
2。 它用于存储数据(一组对象)。 它用于计算数据(一组对象的计算)。
3。 我们可以同时使用Spliterator和Iterator来迭代元素。 我们可以使用forEach为该流的每个元素执行一个动作。 我们不能使用Spliterator或Iterator来迭代元素。
4。 它用于存储无限数量的元素。 Stream API用于处理Collection的元素。
5, 通常,它使用外部迭代概念来迭代诸如Iterator的元素。 Stream API使用forEach方法使用内部迭代来迭代Elements。
6。 收集对象是急切地构造的。 流对象是惰性构造的。
7 仅在完全计算完之后,才向Collection对象添加元素。 我们可以将元素添加到流对象,而无需任何事先计算。 这意味着Stream对象是按需计算的。
8。 我们可以多次迭代和使用Collection对象中的元素。 我们只能迭代和使用一次Stream对象中的元素。

Java SE 8中的Spliterator是什么?Java SE 8中的Iterator和Spliterator之间的区别是什么? (What is Spliterator in Java SE 8?Differences between Iterator and Spliterator in Java SE 8?)

Spliterator stands for Splitable Iterator. It is newly introduced by Oracle Corporation as part Java SE 8.
Like Iterator and ListIterator, It is also one of the Iterator interface.

Spliterator代表可拆分迭代器。 它是Oracle Corporation作为Java SE 8的一部分新引入的。
像Iterator和ListIterator一样,它也是Iterator接口之一。

S.No. Spliterator Iterator
1. It is introduced in Java SE 8. It is available since Java 1.2.
2. Splitable Iterator Non-Splitable Iterator
3. It is used in Stream API. It is used for Collection API.
4. It uses Internal Iteration concept to iterate Streams. It uses External Iteration concept to iterate Collections.
5. We can use Spliterator to iterate Streams in Parallel and Sequential order. We can use Iterator to iterate Collections only in Sequential order.
6. We can get Spliterator by calling spliterator() method on Stream Object. We can get Iterator by calling iterator() method on Collection Object.
7. Important Method: tryAdvance() Important Methods: next(), hasNext()
序号 分流器 迭代器
1。 它是在Java SE 8中引入的。 从Java 1.2开始可用。
2。 可拆分迭代器 不可拆分迭代器
3。 它在Stream API中使用。 它用于Collection API。
4。 它使用内部迭代概念来迭代流。 它使用外部迭代概念来迭代集合。
5, 我们可以使用Spliterator以并行和顺序顺序迭代流。 我们只能使用Iterator来按顺序对Collection进行迭代。
6。 我们可以通过在Stream Object上调用spliterator()方法来获得Spliterator。 我们可以通过在Collection Object上调用iterator()方法来获得Iterator。
7 重要方法:tryAdvance() 重要方法:next(),hasNext()

Java 8中的Optional是什么? Optional的用途是什么?Java 8 Optional的优点? (What is Optional in Java 8? What is the use of Optional?Advantages of Java 8 Optional?)

Optional:
Optional is a final Class introduced as part of Java SE 8. It is defined in java.util package.

可选的:
可选的是作为Java SE 8的一部分引入的最终类。它在java.util包中定义。

It is used to represent optional values that are either exist or not exist. It can contain either one value or zero value. If it contains a value, we can get it. Otherwise, we get nothing.

它用于表示存在或不存在的可选值。 它可以包含一个值或零值。 如果它包含一个值,我们可以得到它。 否则,我们什么也得不到。

It is a bounded collection that is it contains at most one element only. It is an alternative to the “null” value.

它是一个有界集合,它最多仅包含一个元素。 它是“ null”值的替代方法。

Main Advantage of Optional is:

Optional的主要优点是:

  • It is used to avoid null checks.

    它用于避免空检查。
  • It is used to avoid “NullPointerException”.

    用于避免“ NullPointerException”。

什么是类型推断? 类型推断是否在Java 7和7之前的较早版本中可用,还是仅在Java SE 8中可用? (What is Type Inference? Is Type Inference available in older versions like Java 7 and Before 7 or it is available only in Java SE 8?)

Type Inference means determining the Type by compiler at compile-time.

类型推断是指在编译时由编译器确定类型。

It is not a new feature in Java SE 8. It is available in Java 7 and before Java 7 too.

它不是Java SE 8中的新功能。它在Java 7和Java 7之前可用。

Before Java 7:-
Let us explore Java arrays. Define a String of Array with values as shown below:

在Java 7之前:
让我们探索Java数组。 定义一个数组字符串,其值如下所示:

String str[] = { "Java 7", "Java 8", "Java 9" };

Here we have assigned some String values at the right side, but not defined its type. Java Compiler automatically infers its type and creates a String of Array.

在这里,我们在右侧分配了一些String值,但未定义其类型。 Java编译器会自动推断其类型并创建一个Array字符串。

Java 7:
Oracle Corporation has introduced “Diamond Operator” new feature in Java SE 7 to avoid unnecessary Type definition in Generics.

Java 7:
Oracle Corporation在Java SE 7中引入了“ Diamond Operator”新功能,以避免在泛型中不必要的Type定义。

Map> customerInfoByCity = new HashMap<>();

Here we have not defined Type information at the right side, simply defined Java SE 7’s Diamond Operator.

这里我们没有在右侧定义类型信息,只是定义了Java SE 7的Diamond运算符。

Java SE 8:
Oracle Corporation has enhanced this Type Inference concept a lot in Java SE 8. We use this concept to define Lambda Expressions, Functions, Method References etc.

Java SE 8:
Oracle Corporation在Java SE 8中大大增强了该类型推断概念。我们使用此概念来定义Lambda表达式,函数,方法引用等。

ToIntBiFunction add = (a,b) -> a + b;

Here Java Compiler observes the type definition available at the left-side and determines the type of Lambda Expression parameters a and b as Integers.

Java编译器在这里观察左侧可用的类型定义,并将Lambda Expression参数a和b的类型确定为Integers。

That’s all about Java 8 Interview Questions.

这就是关于Java 8面试问题的全部内容。

I have discussed some Java SE 8 Interview Questions in this post and will discuss some more Java SE 8 Interview Questions in my coming posts.

我在这篇文章中讨论了一些Java SE 8面试问题,在以后的文章中还将讨论其他Java SE 8面试问题。

Please drop me a comment if you like my post or have any issues/suggestions.

如果您喜欢我的帖子或有任何问题/建议,请给我评论。

翻译自: https://www.journaldev.com/8697/javase8-interview-questions-part1

java se面试题

你可能感兴趣的:(java,python,编程语言,大数据,设计模式)