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部分) ”。
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在内部进行此工作。
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 | 可读性较低的代码。 | 更多可读的代码。 |
External Iteration has the following drawbacks:
外部迭代具有以下缺点:
Compare to External Iteration, Internal Iteration has the following advantages:
与外部迭代相比,内部迭代具有以下优点:
Compare to External Iteration, Internal Iteration has one major drawback:
与外部迭代相比,内部迭代有一个主要缺点:
Compare to Internal Iteration, External Iteration has one major advantage:
与内部迭代相比,外部迭代具有一个主要优点:
We need to understand the situations to use either Internal Iteration or External Iteration.
我们需要了解使用内部迭代或外部迭代的情况。
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, | 每个语句我们可以使用任意数量的流中间操作。 | 每个语句只能使用一个流终端操作。 |
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中提供方法实现:
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:
由于以下原因,我们需要默认方法:
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:
由于以下原因,我们需要静态方法:
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’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具有以下问题或缺点。
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具有以下优点。
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问题。
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。 |
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 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面试题