Class2
Class2
Heap memory is used by all the parts of the application whereas stack memory is
used only by one thread of execution.
Whenever an object is created, it’s always stored in the Heap space and stack
memory contains the reference to it. Stack memory only contains local primitive
variables and reference variables to objects in heap space.
Objects stored in the heap are globally accessible whereas stack memory can’t be
accessed by other threads.
Memory management in stack is done in LIFO manner whereas it’s more complex in Heap
memory because it’s used globally. Heap memory is divided into Young-Generation,
Old-Generation etc, more details at Java Garbage Collection.
Stack memory is short-lived whereas heap memory lives from the start till the end
of application execution.
We can use -Xms and -Xmx JVM option to define the startup size and maximum size of
heap memory. We can use -Xss to define the stack memory size.
When stack memory is full, Java runtime throws java.lang.StackOverFlowError whereas
if heap memory is full, it throws java.lang.OutOfMemoryError: Java Heap Space
error.
Stack memory size is very less when compared to Heap memory. Because of simplicity
in memory allocation (LIFO), stack memory is very fast when compared to heap
memory.
https://www.digitalocean.com/community/tutorials/java-heap-space-vs-stack-memory
Garbage Collection - Mark and sweep algorithm to clean up the memory in heap
Whichever variable is alive, it is marked by refrence count
Whichever variable goes out of scope, or dies, it is swept
https://medium.com/@khurshidbek-bakhromjonov/java-memory-management-understanding-
the-jvm-heap-method-area-stack-24a4d4fa2363
In young generation of memory, when eden space is full minor garbage collection
algorithm is run
In old generation of memory, when space is full major garbage collection algorithm
is run
Modifiers -
default access - Only accessible within the current package and not outside of the
package
private - only accessible inside the class where it is defined and no other class
in the package can access
protected - accessible within the class and the derived (Child) classes of the
parent class even if its outside package
public - accessible across multiple packages
Java Constructors-
Constructors have the same name as the Class name
Constructors do not have return value
We have two types - Parametrized and Non-parameterized constructors
Default constructor is non-parameterized constructors
Constructors implement Method overloading
The constructor(s) of a class must have the same name as the class name in which it
resides.
A constructor in Java can not be abstract, final, static, or Synchronized.
Access modifiers can be used in constructor declaration to control its access i.e
which other class can call the constructor.
class Geek {
// data members of the class.
String name;
int id;
// Parameterized Constructor
Geek(String name, int id)
{
this.name = name;
this.id = id;
}
// Copy Constructor
Geek(Geek obj2)
{
this.name = obj2.name;
this.id = obj2.id;
}
}
class GFG {
public static void main(String[] args)
{
// This would invoke the parameterized constructor.
System.out.println("First Object");
Geek geek1 = new Geek("Avinash", 68);
System.out.println("GeekName :" + geek1.name
+ " and GeekId :" + geek1.id);
System.out.println();
Constructor Chaining -
Class A {}
Class B extends Class A {}
Class C extends Class B {}
Class D extends Class C {}