0% found this document useful (0 votes)
9 views7 pages

Memory Management and Class Organization

Uploaded by

Đỗ Hoàng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

Memory Management and Class Organization

Uploaded by

Đỗ Hoàng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

5.

Memory Management and Class


organization
1. Memory management in Java

Java does not use ptr, hence memory address can not be overwritten
accidentally or intentionally.

The allocation or re-alloc of mem, management of mem that is controlled by


JVM, are transparent with devs

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

Used to write information created by operator new .

String s = new String("hello"); // s is ptr to str "hello" in Heap


String t = s; // t is also a ptr to str "hello" (copy address of ptr s)
int i = 201; //
int j = i;
Garbage collector

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.

When does GC run?

When the mem is low


May not be run at all
Unpredictable timing

Cannot prevent GC from running, but can request (not guarantee)

System.gc()// call out Garbage collector of JVM, but not guarantee

Java destructor? (technically no)

Deallocation of mem is done automatically by the JVM (through the


finallize() method)

finalize() method (deprecated)

Process just like GC


Sever asssociation between obj - obj reference by assigning another val to
the obj reference.

Note

Any class has finalize() executed right after the GC process takes place.

Overide this method in some special cases in order to self-clean used


resources when obj are freed by GC

Object comparison
Primitive data types: use ==
Obj: == check wether they refer to the same obj

Class1 a = new Class1("abc");


Class1 b = a;
// a == b -> return true

Class1 c = new("abc");
// a==c -> return false

equals() method

Can't be apply for primitive data types

Class1 o1 = new Class1("name", 10, 15);


Class1 o2 = new Class1("name", 10, 15);
// o1 == o2 -> false
// o1.equals(o2) -> true
2. Class organization
3. Abstraction & Encapsulation > Package

java.lang auto imported into all classes

Fully qualified Class name

Fully qualified class name

In the same package: class name


In different packages: contain package name + class name (if packages
hasn't been imported)

import javax.swing.*;

String result = JOptionPane.showInputDialog("Enter a str:");

Basic Java Package

3. Utility classes in Java


Wrapper Class
Since primitive data types have no asssociated methods -> need a wrapper
class

Wrapper

Corresponding classes for primitive data types, included in Java API

Each wrapper obj store a single primitive var


Offer methods which process the stored val

Converting data type with wrapper

Interger i1 = new Interger("10"); // i1 =10


String temp = i1.toString(i1) // temp = "10"
int i = Interger.parseInt("123"); // primitive var
Interger = Interger.valueOf("123"); // wrapper object

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

Java String Methods

Java String Comparision

String Literal vs String Object

Note

When string literals are stored in String Constant Pool, String Objs are
stored in Heap

String s1 = ''New string''; // string literal


String s2 = new String("New String"); // string obj

String Constant Pool

an area in heap memory where Java stores literal string values

String buffer/String Builder

String is an immutable type

Caution
String concatention will create a new obj to store the result -> memory
comsuming

StringBuffer and StringBuilder

Mutable type that can change value after being created.

StringBuffer strb = new StringBuffer("Halo");


StringBuffer strb_temp = strb;
strb.append(" hiha"); // strb point to "Halo hiha"
// strb_temp == strb -> true since strb not change

StringBuffer vs StringBuilder

Math Class
Java Math Class

You might also like