0

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?

2
  • Can you explain why you think that having a heap and a stack (which more or less all language implementations do) is at odds with functional programming? Commented Dec 6, 2018 at 11:13
  • From what I understood we have one heap and one stack per thread.When we create an object we allocate memory in the heap, but the variables are stored in the stack. So imagine now that we have several threads and we share heap memory, some of them may try to manipulate the same value (concurrency),and values in the heap may change in function of time (from here the stateful nature of Java SE I assume).I was wondering how memory management is working in a programming language like Scala, and what makes it stateless. I assume that something in the memory management is different comparing to java Commented Dec 6, 2018 at 11:23

1 Answer 1

4

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).

Sign up to request clarification or add additional context in comments.

1 Comment

"there's absolutely no reason why one couldn't implement a purely functional language on the JVM" – You can even write purely functional programs in Java, without needing to change anything about Java and/or the JVM: just don't use side-effects in your program!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.