We have
int x= 10;
String y = “Word”;
Employee emp = new
Employee();
Now the data store in Stack
and Heap how means
Stack Heap
(Stores
the primitive data type values ) (Stores
the referenced objects data)
Now java supports only passbyValue();
Not the passbyReference();
How means
For primitive data types the
data stored in Stack as shown above. While accessing the data it send the data as
copy of value.
For referenced object
related will be store in Heap as shown above. While accessing the data it sends
the objects data as copy of reference
value.
Diff between copy of value and copy of reference value
EX:
import java.util.ArrayList;
import java.util.List;
public class PassByValue {
public static void addList(List l){
l.add("1");
l.add("6");
}
public static void addInt(int xx){
xx = 1000;
}
public static void
main(String[] args) {
List list = new
ArrayList();
addList(list);
System.out.println("List
Size : "+list.size());
int x =10;
addInt(x);
System.out.println("Integer
Value of X : "+x);
}
}
O/P:
List Size : 2
Integer Value of X : 10
System.out.println(x); it print value as 10 only
because the copyofxValue has changed
from 10 to 1000. But not the original value of x.
System.out.println(list.size());
it prints as 2. Because both list
and l are referred to the same
object reference value i.e, 0X10. So if we change the data structure of list in
addList() method that is also reflected in main() method.
Other main Concept is in Strings
String constant pool(SCP)
Before going to this SCP(String Constant Pool) lemme say difference between
1) String s= new String (“hello”); and
2) String s=”hello”;
In the first case two obects will be created. one in heap and the other in SCP.(s will always refer to heap object but not to SCP)
In the second case only one object will be created in SCP and s will always refer to the object in SCP.
Object creation in SCP is always optional, first JVM checks if there is any obect present with the required content , if exists then will use the same else create new object. i.e SCP do not allow duplicate objects.
GC(garbage collector) is not allowed to enter SCP. But when ever JVM shutdown, automatically SCP area gets flushes out.
The main adv of this approach is memory utilaization will be improved hence the perforamnce improves.(Coz object creation is lil costly in java.)
Other main Concept is in Strings
String constant pool(SCP)
Before going to this SCP(String Constant Pool) lemme say difference between
1) String s= new String (“hello”); and
2) String s=”hello”;
In the first case two obects will be created. one in heap and the other in SCP.(s will always refer to heap object but not to SCP)
In the second case only one object will be created in SCP and s will always refer to the object in SCP.
Object creation in SCP is always optional, first JVM checks if there is any obect present with the required content , if exists then will use the same else create new object. i.e SCP do not allow duplicate objects.
GC(garbage collector) is not allowed to enter SCP. But when ever JVM shutdown, automatically SCP area gets flushes out.
The main adv of this approach is memory utilaization will be improved hence the perforamnce improves.(Coz object creation is lil costly in java.)