Reputation: 705
I was curious regarding what is happening in memory when we execute a Java program, so I have watched some short tutorials just to have a basic understanding.
Those 'tutorials' are talking about heap and stacks and how those works and what are their purpose.
Java SE as an OOP language is defined as stateful which means that variables changes in function of time ( example x = x + 1 ).
Considering that variable are 'spread' across heaps and stack I can understand why Java is stateful and why we can fall on concurrency issue.
However seems like functional programming languages doesn't suffer of those issues cause they are stateless (with all the benefit which I am not going to list here)..
However Scala, which is defined as functional programming, is build upon the JVM, so I believe it has the same memory management of Java (SE).
1) Is the memory management a peculiarity of the JVM or of the Java SE?
2) if the memory management is a peculiarity of the JVM, how come that Scala is defined as a functional programming language?
Upvotes: 0
Views: 305
Reputation: 370172
First of all, Scala isn't a purely functional language. It has mutable variables and data structures if you want them. However this is not a consequence of the JVM having a heap and stack, but simply pragmatics. In fact most functional languages aren't purely functional.
That said, the purely functional languages that do exist (Haskell for example) also have a heap and a stack (or rather one stack per thread) and there's absolutely no reason why one couldn't implement a purely functional language on the JVM (other than the pragmatic one that, being on the JVM, it would be a good idea to offer the ability to interop with Java libraries, many of which feature mutable objects).
values in the heap may change in function of time
Values in the heap (or stack for that matter) change if you change them. If you don't change them, they don't change. If your language doesn't allow you to change them, they can't change. There is nothing about stacks or heaps that would prevent a language from having immutable variables and/or objects. Just like there's nothing preventing you from declaring all of your variables final
in Java (and only using classes that also only have final
members).
Upvotes: 4