函数式编程和面向对象式编程
As Oracle Corporation has introduced some Functional constructs in Java SE 8, now-a-days most of the Interviewers are interested to ask some questions about Functional Programming.
随着Oracle Corporation在Java SE 8中引入了一些Functional构造,如今,大多数采访者都想问一些有关Functional Programming的问题。
As a Java/Groovy/Scala Developer or any Functional Programming Developer, we should learn the following questions and answers to clear interviews:
作为Java / Groovy / Scala开发人员或任何函数编程开发人员,我们应该学习以下问题和答案以进行清晰的访谈:
- What is Functional Programming? 什么是函数式编程?
- What are the “Pros & Cons” of Functional Programming? 什么是函数式编程的“利弊”?
- What are the differences between Functional Programming and Imperative Programming? 函数式编程和命令式编程之间有什么区别?
- What are the differences between Functional Programming and Object Oriented Programming? 函数式编程和面向对象的编程之间有什么区别?
- What are the differences between Functional Programming, Imperative Programming(IP) and Object Oriented Programming? 函数式编程,命令式编程(IP)和面向对象的编程之间有什么区别?
- What are the major advantages of Functional Programming over Imperative Programming or Object Oriented Programming? 与命令式编程或面向对象的编程相比,函数式编程的主要优势是什么?
- When to use Functional Programming? 何时使用函数式编程?
- When to use Object Oriented Programming? 何时使用面向对象编程?
- What are the drawbacks of OOP? OOP的缺点是什么?
- What are the drawbacks of OOP-Inheritance? OOP继承的缺点是什么?
… And More
… 和更多
In this post, we are going to discuss about the following three popular Programming paradigms one by one and also answer above questions:
在本文中,我们将一个接一个地讨论以下三种流行的编程范例,并回答以上问题:
- Functional Programming (FP) 函数式编程(FP)
- Imperative Programming (IP) 命令式编程(IP)
- Object Oriented Programming (OOP) 面向对象编程(OOP)
什么是函数式编程? (What is Functional Programming?)
In simple words, Functional Programming (FP) is one of the popular Programming paradigms, which does computation like Mathematical Functions without Changing State and Mutating Data.
简而言之, 函数式编程(FP)是一种流行的编程范式,它可以像数学函数一样进行计算,而无需更改状态和更改 数据 。
In Functional Programming, Functions are first class candidates. We write programs by defining a set of Functions and Immutable Data.
在函数式编程中,函数是一等候选。 我们通过定义一组函数和不可变数据来编写程序。
Examples of FP Languages:-
Scala, Haskell, Earlang etc are popular FP Languages. Java SE 8 also have some Functional Constructs (Please refer Java 8 posts for more details)
FP语言示例:-
Scala,Haskell,Earlang等是流行的FP语言。 Java SE 8也具有一些功能构造(有关更多详细信息,请参阅Java 8帖子)
FP的特点是什么? (What are the Characteristics of FP?)
Functional Programming Languages like Scala have the following Characteristics
像Scala这样的函数式编程语言具有以下特征
- State does not exist. 状态不存在。
FP Programs does not contain state. That means all Data is Immutable Data and Functions cannot change state.
FP程序不包含状态。 这意味着所有数据都是不可变数据,功能不能更改状态。
- Low importance of Order of Execution 执行命令的重要性低
In FP Languages, we write programs with a set of Independent Functions. Functions contain a set of statements.
In FP, the order of execution of those Functions does not have much importance because they do not have state and all Functions work independently. Even if we change the order of execution still they produce the same results.在FP语言中,我们编写具有一组独立功能的程序。 函数包含一组语句。
在FP中,这些功能的执行顺序并不重要,因为它们没有状态并且所有功能都独立工作。 即使我们更改执行顺序,它们仍然会产生相同的结果。- Stateless Programming Model 无状态编程模型