As we know that unlike java, scala has everything as object.
For example we have-
object A{
val arg1=1
def myFun(arg2:Int)=arg1
}
class A{
val arg1=1
def myFun(arg2:Int)=arg1
}
trait A{
val arg1=1
def myFun(arg2:Int)=arg1
}
There are two basic types of memory allocation: When you declare a variable or an instance of a structure or class. The memory for that object is allocated by the operating system. The name you declare for the object can then be used to access that block of memory.
The heap is a large area of memory available for use by the program. The program can request areas, or “blocks”, of memory for its use within the heap. In order to allocate a block of some size, the program makes an explicit request by calling the heap allocation operation. In Java or C++, this is the new operator.
When a variable is declared compiler automatically allocates memory for it. This is known as compile time memory allocation or static memory allocation. Memory can be allocated for data variables after the program begins execution. This mechanism is known as runtime memory allocation or dynamic memory allocation.
The heap is a memory used by programming languages to store global variables. By default, all global variable are stored in heap memory space. It supports Dynamic memory allocation. The heap is not managed automatically for you and is not as tightly managed by the CPU.
Scala runs on the JVM and it's base on the Java library.
scala file(*.scala) will be compiled to the java class bytecode and run these on JVM. for your example:
object A{
val arg1=1
def myFun(arg2:Int)=arg1
}
will be translated to(decompile bytecode by javap):
public class A$ extends java.lang.Object{
public static final A$ MODULE$;
private final int arg1;
public static {};
public int arg1();
public int myFun(int);
public A$();
}
class A{
val arg1=1
def myFun(arg2:Int)=arg1
}
will be translated to(decompile bytecode by javap):
public class A extends java.lang.Object{
private final int arg1;
public int arg1();
public int myFun(int);
public A();
}
trait A{
val arg1=1
def myFun(arg2:Int)=arg1
}
will be translated to(decompile bytecode by javap):
public interface A{
public abstract void $line5$$read$A$_setter_$arg1_$eq(int);
public abstract int arg1();
public abstract int myFun(int);
}
so for your other memory questions, I think it's same as Java.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With