Specially in comparison to C/C++ (which are declarative), how is Java imperative?
-
10I believe that C and C++ are both imperative. – Thomas Owens Nov 07 '09 at 18:54
-
maybe the confusion is because C/C++ programs need to declare functions beside defining them? That doesn't have anything to do with declarative languages (or declarative programming, which can be done with imperative languages) – Javier Nov 07 '09 at 20:24
3 Answers
C/C++ is imperative too.
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.

- 22,839
- 20
- 87
- 129
-
3http://stackoverflow.com/questions/1619834/difference-between-declarative-and-procedural-programming has lots of examples of declarative vs. imperative languages which might help hanifr get a handle on what a declarative language is. – itowlson Nov 07 '09 at 18:57
You are confusing the concepts, C and C++ are not declarative languages. Refer to Declarative Programming
and Imperative Programming
. Basically, with declarative languages (e.g. Prolog), you specifiy what you want to accomplish, without specifying how to accomplish it, which contrasts with imperative languages.

- 89,303
- 29
- 152
- 158
-
I like your answer but I think it would help more if you could kindly add an example. – M. Al Jumaily Sep 01 '19 at 20:58
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.
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.
I read this book about functional programming in Java and found it useful: https://pragprog.com/book/vsjava8/functional-programming-in-java

- 5,347
- 4
- 25
- 31