Memory Management and Class Organization
Memory Management and Class Organization
Java does not use ptr, hence memory address can not be overwritten
accidentally or intentionally.
Devs don't need to care about the allocated mem in heap to free it later.
Heaps
Stacks
Stack
Local val in Stack mem is used as a reference pointer to Heap.
Val of primitive data is written directly in Stack.
Heap
Garbage collector
Sweep through the JVM's list of obj periodically and reclaims the resources
held by unreferenced obj
Ref out of scope, objects to which u have assigned null, and so forth.
Note
Any class has finalize() executed right after the GC process takes place.
Object comparison
Primitive data types: use ==
Obj: == check wether they refer to the same obj
Class1 c = new("abc");
// a==c -> return false
equals() method
import javax.swing.*;
Wrapper
Constants
String
String
Not a primitive data type, but a class, made up of any char between the
double quotes.
// initialized a string
String str1 = new String();
String str2 = new String("New String");
String str3 = String.valueOf(1.23);
String str4 = null;
String a = "My " + "name " + "is Tu";// "My name is Tu"
System.out.println("Result is" + res) //auto convert primitive data types to
string
Note
When string literals are stored in String Constant Pool, String Objs are
stored in Heap
Caution
String concatention will create a new obj to store the result -> memory
comsuming
StringBuffer vs StringBuilder
Math Class
Java Math Class