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

java se面试题

In my previous post, I have discussed some important Java SE 8 Interview Questions and Answers. In this post, we are going to discuss some more Java SE 8 Interview Questions and Answers.

在我以前的文章中,我讨论了一些重要的Java SE 8面试问答。 在这篇文章中,我们将讨论更多Java SE 8面试问答。

Before reading this post, please go through my previous post at: “Java SE 8 Interview Questions (Part 1)“.

在阅读本文之前,请仔细阅读我以前的文章:“ Java SE 8面试问题(第1部分) ”。

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

  1. What is Internal Iteration in Java SE 8?

    Java SE 8中的内部迭代是什么?
  2. Differences between External Iteration and Internal Iteration?

    外部迭代和内部迭代之间的区别?
  3. What are the major drawbacks of External Iteration?

    外部迭代的主要缺点是什么?
  4. What are the major advantages of Internal Iteration over External Iteration?

    内部迭代相对于外部迭代的主要优点是什么?
  5. What is the major drawback of Internal Iteration over External Iteration?

    内部迭代优于外部迭代的主要缺点是什么?
  6. What is the major advantage of External Iteration over Internal Iteration?

    外部迭代相对于内部迭代的主要优点是什么?
  7. When do we need to use Internal Iteration? When do we need to use External Iteration?

    什么时候需要使用内部迭代? 我们什么时候需要使用外部迭代?
  8. Differences between Intermediate Operations and Terminal Operations of Java 8’s Stream API?

    Java 8的Stream API的中间操作和终端操作之间的区别?
  9. Is it possible to provide method implementations in Java Interfaces? If possible, how do we provide them?

    是否可以在Java接口中提供方法实现? 如果可能,我们如何提供它们?
  10. What is a Default Method? Why do we need Default methods in Java 8 Interfaces?

    什么是默认方法? 为什么在Java 8接口中需要Default方法?
  11. What is a Static Method? Why do we need Static methods in Java 8 Interfaces?

    什么是静态方法? 为什么在Java 8接口中需要静态方法?
  12. Differences between Functional Programming and Object-Oriented Programming?

    函数式编程和面向对象的编程之间的区别?
  13. Explain issues of Old Java Date API? What are the advantages of Java 8’s Date and Time API over Old Date API and Joda Time API?

    解释旧Java Date API的问题? 与旧日期API和Joda Time API相比,Java 8的日期和时间API有何优势?
  14. Why do we need new Date and Time API in Java SE 8?Explain how Java SE 8 Data and Time API solves issues of Old Java Date API?

    为什么我们在Java SE 8中需要新的日期和时间API?解释Java SE 8数据和时间API如何解决旧Java日期API的问题?
  15. What are the Differences between Java’s OLD Java Date API and Java 8’s Date and Time API?

    Java的OLD Java Date API和Java 8的Date and Time API有什么区别?
  16. What is Multiple Inheritance? How Java 8 supports Multiple Inheritance?

    什么是多重继承? Java 8如何支持多重继承?
  17. What is Diamond Problem in interfaces due to default methods? How Java 8 Solves this problem?

    由于默认方法,接口中的钻石问题是什么? Java 8如何解决这个问题?

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

Java SE 8中的内部迭代是什么? (What is Internal Iteration in Java SE 8?)

Before Java 8, We don’t Internal Iteration concept. Java 8 has introduced a new feature known as “Internal Iteration”. Before Java 8, Java Language has only External Iteration to iterate elements of an Aggregated Object like Collections, Arrays etc.

在Java 8之前,我们没有内部迭代概念。 Java 8引入了一个称为“内部迭代”的新功能。 在Java 8之前,Java语言只有外部迭代才能迭代聚合对象的元素,例如集合,数组等。

Internal Iteration means “Iterating an Aggregated Object elements one by one internally by Java API”. Instead of Java Application do iteration externally, We ask Java API to do this job internally.

内部迭代是指“通过Java API在内部逐个迭代聚合对象元素”。 而不是Java应用程序在外部进行迭代,我们要求Java API在内部进行此工作。

外部迭代和内部迭代之间的区别? (Differences between External Iteration and Internal Iteration?)

S.No. External Iteration Internal Iteration
1. Available before Java 8 too. It is introduced in Java SE 8
2. Iterating an Aggregated Object elements externally. Iterating an Aggregated Object elements internally (background).
3. Iterate elements by using for-each loop and Iterators like Enumeration, Iterator, ListIterator. Iterate elements by using Java API like “forEach” method.
4. Iterating elements in Sequential and In-Order only. Not required to iterate elements in Sequential order.
5. It follows OOP approach that is Imperative Style. It follows Functional Programming approach that is Declarative Style.
6. It does NOT separate responsibilities properly that is, it defines both “What is to be done” and “How it is to be done”. It defines only “What is to be done”. No need to worry about “How it is to be done”. Java API takes care about “How to do”.
7. Less Readable Code. More Readable code.
序号 外部迭代 内部迭代
1。 Java 8之前也可用。 在Java SE 8中引入
2。 从外部迭代“聚合对象”元素。 在内部(背景)迭代聚合对象元素。
3。 通过使用for-each循环和诸如Enumeration,Iterator,ListIterator之类的迭代器来迭代元素。 通过使用Java API(例如“ forEach”方法)来迭代元素。
4。 仅按顺序和顺序迭代元素。 不需要按顺序迭代元素。
5, 它遵循命令式风格的OOP方法。 它遵循声明式样式的函数编程方法。
6。 它没有正确区分职责,即定义了“要做的事情”和“如何做的事情”。 它仅定义“要做的事情”。 无需担心“如何完成”。 Java API注意“如何做”。
7 可读性较低的代码。 更多可读的代码。

外部迭代的主要缺点是什么? (What are the major drawbacks of External Iteration?)

External Iteration has the following drawbacks:

外部迭代具有以下缺点:

  • We need to write code in Imperative Style.

    我们需要以命令式风格编写代码。
  • There is no clear separation of Responsibilities. Tightly-Coupling between “What is to be done” and “How it is to be done” code.

    没有明确区分职责。 “要做的事情”和“要做的事情”代码之间紧密耦合。
  • Less Readable Code.

    可读性较低的代码。
  • More Verbose and Boilerplate code.

    更多详细和样板代码。
  • We have to iterate elements in Sequential order only.

    我们只需要按顺序迭代元素。
  • It does not support Concurrency and Parallelism properly.

    它不正确支持并发和并行。

内部迭代相对于外部迭代的主要优点是什么? (What are the major advantages of Internal Iteration over External Iteration?)

Compare to External Iteration, Internal Iteration has the following advantages:

与外部迭代相比,内部迭代具有以下优点:

  • As it follows Functional Programming style, we can write Declarative Code.

    遵循函数式编程风格,我们可以编写声明性代码。
  • More Readable and concise code.

    更可读,简洁的代码。
  • Avoids writing Verbose and Boilerplate code

    避免编写Verbose和Boilerplate代码
  • No need to iterate elements in Sequential order.

    无需按顺序迭代元素。
  • It supports Concurrency and Parallelism properly.

    它正确支持并发和并行。
  • We can write Parallel code to improve application performance.

    我们可以编写并行代码来提高应用程序性能。
  • Clear separation of Responsibilities. Loosely-Coupling between “What is to be done” and “How it is to be done” code.

    明确区分职责。 “要做的事情”和“要做的事情”代码之间的松散耦合。
  • We need to write code only about “What is to be done” and Java API takes care about “How it is to be done” code.

    我们只需要编写关于“要做的事情”的代码,而Java API则关注“要做的事情”的代码。

内部迭代优于外部迭代的主要缺点是什么? (What is the major drawback of Internal Iteration over External Iteration?)

Compare to External Iteration, Internal Iteration has one major drawback:

与外部迭代相比,内部迭代有一个主要缺点:

  • In Internal Iteration, as Java API takes care about Iterating elements internally, we do NOT have control over Iteration.

    在内部迭代中,由于Java API在内部处理迭代元素,因此我们无法控制迭代。

外部迭代相对于内部迭代的主要优点是什么? (What is the major advantage of External Iteration over Internal Iteration?)

Compare to Internal Iteration, External Iteration has one major advantage:

与内部迭代相比,外部迭代具有一个主要优点:

  • In External Iteration, as Java API does NOT take care about Iterating elements, we have much control over Iteration.

    在外部迭代中,由于Java API不关心迭代元素,因此我们对迭代有很多控制。

什么时候需要使用内部迭代? 我们什么时候需要使用外部迭代? (When do we need to use Internal Iteration? When do we need to use External Iteration?)

We need to understand the situations to use either Internal Iteration or External Iteration.

我们需要了解使用内部迭代或外部迭代的情况。

  • When we need more control over Iteration, we can use External Iteration.

    当需要对迭代进行更多控制时,可以使用外部迭代。
  • When we do NOT need more control over Iteration, we can use Internal Iteration.

    当我们不需要对迭代的更多控制时,可以使用内部迭代。
  • When we need to develop Highly Concurrency and Parallel applications and we , we should use Internal Iteration.

    当我们需要开发高度并发和并行应用程序时,我们应该使用内部迭代。

Java 8的Stream API的中间操作和终端操作之间的区别? (Differences between Intermediate Operations and Terminal Operations of Java 8’s Stream API?)

S.No. Stream Intermediate Operations Stream Terminal Operations
1. Stream Intermediate operations are not evaluated until we chain it with Stream Terminal Operation. Stream Terminal Operations are evaluated on it’s own. No need other operations help.
2. The output of Intermediate Operations is another Stream. The output of Intermediate Operations is Not a Stream. Something else other than a Stream.
3. Intermediate Operations are evaluated Lazily. Terminal Operations are evaluated Eagerly.
4. We can chain any number of Stream Intermediate Operations. We can NOT chain Stream Terminal Operations.
5. We can use any number of Stream Intermediate Operations per Statement. We can use only one Stream Terminal Operation per Statement.
序号 流中间操作 流终端操作
1。 除非我们将其与Stream Terminal Operation链接在一起,否则不会评估Stream Intermediate操作。 流终端操作是独立评估的。 无需其他操作帮助。
2。 中间操作的输出是另一个流。 中间操作的输出不是流。 除了Stream以外的其他东西。
3。 懒惰评估中级操作。 对终端操作进行认真的评估。
4。 我们可以链接任意数量的Stream Intermediate Operations。 我们不能链接流终端操作。
5, 每个语句我们可以使用任意数量的流中间操作。 每个语句只能使用一个流终端操作。

是否可以在Java接口中提供方法实现? 如果可能,我们如何提供它们? (Is it possible to provide method implementations in Java Interfaces? If possible, how do we provide them?)

In Java 7 or earlier, It is not possible to provide method implementations in Interfaces. Java 8 on-wards, it is possible.

在Java 7或更早版本中,不可能在Interfaces中提供方法实现。 从Java 8开始,这是可能的。

In Java SE 8, We can provide method implementations in Interfaces by using the following two new concepts:

在Java SE 8中,我们可以使用以下两个新概念在Interfaces中提供方法实现:

  • Default Methods

    默认方法
  • Static Methods

    静态方法

什么是默认方法? 为什么在Java 8接口中需要Default方法? (What is a Default Method? Why do we need Default methods in Java 8 Interfaces?)

A Default Method is a method which is implemented in an interface with “default” keyword. It’s new featured introduced in Java SE 8.

默认方法是在接口中使用“默认”关键字实现的方法。 它是Java SE 8中引入的新功能。

We need Default Methods because of the following reasons:

由于以下原因,我们需要默认方法:

  • It allow us to provide method’s implementation in Interfaces.

    它允许我们在接口中提供方法的实现。
  • To add new Functionality to Interface without breaking the Classes which implement that Interface.

    在不破坏实现该接口的类的情况下向接口添加新的功能。
  • To provide elegant Backwards Compatibility Feature.

    提供优雅的向后兼容性功能。
  • To ease of extend the existing Functionality.

    为了简化现有功能的扩展。
  • To ease of Maintain the existing Functionality.

    易于维护现有功能。

什么是静态方法? 为什么在Java 8接口中需要静态方法? (What is a Static Method? Why do we need Static methods in Java 8 Interfaces?)

A Static Method is an Utility method or Helper method, which is associated to a class (or interface). It is not associated to any object.

静态方法是与类(或接口)相关联的Utility方法或Helper方法。 它没有与任何对象关联。

We need Static Methods because of the following reasons:

由于以下原因,我们需要静态方法:

  • We can keep Helper or Utility methods specific to an interface in the same interface rather than in a separate Utility class.

    我们可以将特定于接口的Helper或Utility方法保留在同一接口中,而不是在单独的Utility类中。
  • We do not need separate Utility Classes like Collections, Arrays etc to keep Utility methods.

    我们不需要单独的实用程序类(如集合,数组等)来保留实用程序方法。
  • Clear separation of Responsibilities. That is we do not need one Utility class to keep all Utility methods of Collection API like Collections etc.

    明确区分职责。 也就是说,我们不需要一个Utility类来保留Collection API的所有Utility方法(例如Collections等)。
  • Easy to extend the API.

    易于扩展API。
  • Easy to Maintain the API.

    易于维护的API。

函数式编程和面向对象的编程之间的区别? (Differences between Functional Programming and Object-Oriented Programming?)

Functional Programming OOP
Does not exist State Exists State
Uses Immutable data Uses Mutable data
It follows Declarative Programming Model It follows Imperative Programming Model
Stateless Programming Model Stateful Programming Model
Main Fcous on: “What you are doing” Main focus on “How you are doing”
Good for Parallel (Concurrency) Programming Poor for Parallel (Concurrency) Programming
Good for BigData processing and analysis NOT Good for BigData processing and analysis
Supports pure Encapsulation It breaks Encapsulation concept
Functions with No-Side Effects Methods with Side Effects
Functions are first-class citizens Objects are first-class citizens
Primary Manipulation Unit is “Function” Primary Manipulation Unit is Objects(Instances of Classes)
Flow Controls: Function calls, Function Calls with Recursion Flow Controls: Loops, Conditional Statements
It uses “Recursion” concept to iterate Collection Data. It uses “Loop” concept to iterate Collection Data. For example:-For-each loop in Java
Order of execution is less importance. Order of execution is must and very important.
Supports both “Abstraction over Data” and “Abstraction over Behavior”. Supports only “Abstraction over Data”.
We use FP when we have few Things with more operations. We use OOP when we have few Operations with more Things. For example: Things are classes and Operations are Methods in Java.
功能编程 面向对象
不存在状态 存在状态
使用不可变数据 使用可变数据
它遵循声明式编程模型 它遵循命令式编程模型
无状态编程模型 有状态编程模型
主要观点:“你在做什么” 主要关注“你的表现”
适合并行(并发)编程 并行(并发)编程能力差
适用于BigData处理和分析 不适合BigData处理和分析
支持纯封装 它打破了封装的概念
无副作用的功能 副作用方法
职能是一等公民 对象是一等公民
主要操作单元是“功能” 主要操作单位是对象(类的实例)
流控制:函数调用,带递归的函数调用 流控制:循环,条件语句
它使用“递归”概念来迭代收集数据。 它使用“循环”概念来循环收集数据。 例如:Java中的for-for循环
执行顺序不太重要。 执行顺序是必须且非常重要的。
支持“基于数据的抽象”和“基于行为的抽象”。 仅支持“基于数据的抽象”。
当我们的事物很少且需要更多操作时,我们将使用FP。 当我们很少进行具有更多事物的操作时,我们将使用OOP。 例如:在Java中,事物是类,而操作是Java中的方法。

NOTE:- For more information about FP, IP and OOP comparisons, Please go through my previous post at: “Compare FP, OOP(IP)”

注意:-有关FP,IP和OOP比较的更多信息,请浏览我以前的文章:“ 比较FP,OOP(IP) ”

解释旧Java Date API的问题? 与旧日期API和Joda Time API相比,Java 8的日期和时间API有何优势? (Explain issues of Old Java Date API? What are the advantages of Java 8’s Date and Time API over Old Date API and Joda Time API?)

Java’s OLD Java Date API means Date API available before Java SE 8 that is Date, Calendar, SimpleDateFormat etc.

Java的OLD Java Date API表示Java SE 8之前的Date API,即Date,Calendar,SimpleDateFormat等。

Java’s Old Date API has the following Issues or Drawbacks compare to Java 8’s Date and Time API and Joda Time API.

与Java 8的Date and Time API和Joda Time API相比,Java的Old Date API具有以下问题或缺点。

  • Most of the API is deprecated.

    大多数API已弃用。
  • Less Readability.

    可读性较低。
  • java.util.Date is Mutable and not Thread-Safe.

    java.util.Date是可变的,并且不是线程安全的。
  • java.text.SimpleDateFormat is not Thread-Safe.

    java.text.SimpleDateFormat不是线程安全的。
  • Less Performance.

    性能降低。

Java SE 8’s Date and Time API has the following Advantages compare to Java’s OLD Date API.

与Java的OLD Date API相比,Java SE 8的Date and Time API具有以下优点。

  • Very simple to use.

    使用非常简单。
  • Human Readable Syntax that is More Readability.

    人类可读的语法,更具可读性。
  • All API is Thread-Safe.

    所有API都是线程安全的。
  • Better Performance.

    更好的性能。

为什么我们在Java SE 8中需要新的日期和时间API?解释Java SE 8数据和时间API如何解决旧Java日期API的问题? (Why do we need new Date and Time API in Java SE 8?Explain how Java SE 8 Data and Time API solves issues of Old Java Date API?)

We need Java 8’s Date and Time API to develop Highly Performance, Thread-Safe and Highly Scalable Java Applications.

我们需要Java 8的Date and Time API来开发高性能,线程安全和高度可扩展的Java应用程序。

Java 8’s Date and Time API solves all Java’s Old Date API issues by following Immutability and Thread-Safety principles.

Java 8的Date and Time API通过遵循不变性和线程安全性原则解决了Java的所有Old Date API问题。

Java的OLD Java Date API和Java 8的Date and Time API有什么区别? (What are the Differences between Java’s OLD Java Date API and Java 8’s Date and Time API?)

Differences between Java’s OLD Java Date API and Java 8’s Date and Time API:

Java的OLD Java Date API和Java 8的Date and Time API之间的区别:

S.No. Java’s OLD Java Date API Java 8’s Date and Time API
1. Available before Java 8 too. It is introduced in Java SE 8
2. Not Thread Safe. Thread Safe.
3. Mutable API. Immutable API.
4. Less Performance. Better Performance.
5. Less Readability. More Readability.
6. It’s not recommended to use as its deprecated. It’s always recommended to use.
7. Not Extendable. Easy to Extend.
8. It defines months values from 0 to 11, that is January = 0. It defines months values from 1 to 12, that is January = 1.
9. It’s an old API. It’s a new API.
序号 Java的旧Java Date API Java 8的日期和时间API
1。 Java 8之前也可用。 在Java SE 8中引入
2。 不是线程安全的。 线程安全。
3。 可变API。 不可变的API。
4。 性能降低。 更好的性能。
5, 可读性较低。 更具可读性。
6。 不建议使用它,因为它已被弃用。 始终建议使用。
7 不可扩展。 易于扩展。
8。 它定义了从0到11的月份值,即一月= 0。 它定义了从1到12的月份值,即一月= 1。
9。 这是一个旧的API。 这是一个新的API。

什么是多重继承? Java 8如何支持多重继承? (What is Multiple Inheritance? How Java 8 supports Multiple Inheritance?)

Multiple Inheritance means a class can inherit or extend characteristics and features from more than one parent class.

多重继承是指一个类可以从多个父类继承或扩展特征和特征。

In Java 7 or Earlier, Multiple Inheritance is not possible because Java follows “A class should extend one and only one class or abstract class” Rule. However, it’s possible to provide Multiple Implementation Inheritance using Interface because Java follows “A class can extend any number of Interfaces” Rule.

在Java 7或更早版本中,多重继承是不可能的,因为Java遵循“一个类应扩展一个并且只能扩展一个类或抽象类”的规则。 但是,可以使用接口提供多重实现继承,因为Java遵循“一个类可以扩展任意数量的接口”规则。

However, Java 8 supports “Implementing Methods in Interfaces” by introducing new features: Default methods in Interface. Because of this feature, Java 8 supports Multiple Inheritance with some limitations.

但是,Java 8通过引入新功能来支持“接口中的实现方法”:接口中的默认方法。 由于此功能,Java 8支持多重继承,但有一些限制。

由于默认方法,接口中的钻石问题是什么? Java 8如何解决这个问题? (What is Diamond Problem in interfaces due to default methods? How Java 8 Solves this problem?)

Java 8 default methods can introduce a diamond problem when a class implements multiple interfaces. It occurs when a Class extends more than one interfaces with the same method implementations (Default method).

当一个类实现多个接口时,Java 8默认方法可能会带来菱形问题。 当一类使用相同的方法实现(默认方法)扩展多个接口时,会发生这种情况。

Sample Java SE 8 Code to show the Diamond Problem with interface default methods.

示例Java SE 8代码以使用接口默认方法显示Diamond问题。

interface A {
	default void display() {
		System.out.println("A");
	}
}

interface B extends A {
	default void display() {
		System.out.println("B");
	}
}

interface C extends A {
	default void display() {
		System.out.println("C");
	}
}

class D implements B, C {
}

In the above code snippet, class D gives compile-time error as “Duplicate default methods named display with the parameters () and () are inherited from the types C and B”. It’s because Java Compiler will get confused about which display() to use in class D. Class D inherits display() method from both interfaces B and C. To solve this problem, Java 8 has given the following solution.

在上面的代码片段中,类D给出了编译时错误,因为“名称为display且具有参数()和()的重复默认方法是从类型C和B继承的”。 这是因为Java编译器会对在类D中使用哪个display()感到困惑。类D从接口B和C都继承了display()方法。为解决此问题,Java 8提供了以下解决方案。

class D implements B, C {

	@Override
	public void display() {
		B.super.display();
	}
	
}

This B.super.display(); will solve this Diamond Problem. If you want to use C interface default method, then use C.super.display();.

这个B.super.display(); 将解决这个钻石问题。 如果要使用C接口默认方法,请使用C.super.display();

That’s all about Java 8 Interview Questions. We will discuss some more Java SE 8 Interview Questions in my coming posts.

这就是关于Java 8面试问题的全部内容。 在我的后续文章中,我们将讨论更多Java SE 8面试问题。

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

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

翻译自: https://www.journaldev.com/10081/javase8-interview-questions-part2

java se面试题

你可能感兴趣的:(java se面试题_Java SE 8面试问答(第2部分))