Java 如何成为“命令式”编程语言而不是“声明式”编程语言?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1694025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How is Java an 'imperative' programming language and not a 'declarative' one?
提问by Moeb
Specially in comparison to C/C++ (which are declarative), how is Java imperative?
特别是与 C/C++(声明式)相比,Java 的命令式如何?
回答by Mike Q
C/C++ is imperative too.
C/C++ 也是必不可少的。
Edit: Imperative is do this, then do that, then do the next thing and so on. Declarative is, this are the rules, now what's the answer to this question. Google, you'll find plenty of info.
编辑:当务之急是做这个,然后做那个,然后做下一件事情等等。声明性的是,这是规则,现在这个问题的答案是什么。谷歌,你会发现很多信息。
回答by Jo?o Silva
You are confusing the concepts, C and C++ are not declarative languages. Refer to Declarative Programmingand Imperative Programming. Basically, with declarative languages (e.g. Prolog), you specifiy whatyou want to accomplish, without specifying howto accomplish it, which contrasts with imperative languages.
您混淆了概念,C 和 C++ 不是声明性语言。参考Declarative Programming和Imperative Programming。基本上,声明性语言(例如Prolog的),你specifiy什么你要完成,而无需指定如何完成它,这与命令式语言对比。
回答by Tal Joffe
For future readers, although both Java and C/C++ are usually written imperatively, Both languages support writing in a more declarative way for example by applying functional principles.
对于未来的读者,虽然 Java 和 C/C++ 通常都是命令式编写的,但这两种语言都支持以更声明的方式编写,例如应用函数式原则。
In the past years it has been more and more prominent and is encouraged by many influential software engineers such as Uncle Bob to go to a more functional (and thus declarative) approach.
在过去的几年里,它变得越来越突出,并受到许多有影响力的软件工程师的鼓励,比如鲍勃叔叔,去更实用(因此是声明性的)方法。
In Java this was made much more easy with Java 8 that introduced Lambda, Streams etc.
在 Java 中,引入 Lambda、Streams 等的 Java 8 使这变得更加容易。
I read this book about functional programming in Java and found it useful: https://pragprog.com/book/vsjava8/functional-programming-in-java
我阅读了这本关于 Java 函数式编程的书,发现它很有用:https: //pragprog.com/book/vsjava8/functional-programming-in-java

