Memory Handling in Java
Tags: java, programming, computerscience, learning
Published: 2024-03-04 13:19:15

> Before diving into memory management of java one must know java has primitive datatypes and more complex objects (reference types)

- Primitive type
- refference type

> Java has no concept of pointers and java only has pass by value, there is nothing like pass by reference in java

---
### primitives
- Primitive types are the basic data types provided by a programming language. 
- They are the simplest and most fundamental building blocks of data. In Java, the primitive types include:

![primitives](https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExd3FyMTJyemtpdWhjNzE1dmgwbzV1MWozMHAxc2tubWo2cTM1Zjk4ayZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/aACeieARpEEE/giphy.gif)

- Integral Types:

 - byte: 8-bit signed integer
 - short: 16-bit signed integer
 - int: 32-bit signed integer
 -long: 64-bit signed integer
- Floating-Point Types:
 - float: 32-bit floating-point
 - double: 64-bit floating-point
- Characters:
 - char: 16-bit Unicode character
- Boolean:
 - boolean: Represents true or false.

---

### Reference type
- Reference types are more complex and are used to store references (memory addresses) to objects. Objects are instances of classes or arrays. In Java, reference types include:

![modern](https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExYTAyaG0zdzg0bHN2MXI3d3EwN2Q5bnp0aXM5eHlobTIxN3VwcTFjcCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/xT9DPvELn0M57XxXoY/giphy.gif)

- Objects:
  - Instances of classes created using the new keyword.
- Arrays:
  - Ordered collections of elements.
- Interfaces:
  - Types representing a contract for classes to implement.

---

## How primitives are stored ?
- All data for primitive type variables is stored on the stack
- when setting a primitive type variable equal to another primitive type variable, a copy of value is made.

```
int a  = 10;
int b = 20;
int c = b;
int c = 100;
```
![primitive_stack](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l2ld0vw94lz7d3eov2lm.png)

> int a = 10; -> int 10 is stored on stack memory
  int b = 20; -> int 20 is stored on stack memory
  int c = b; -> value of b(20) is copied to c
  int c = 100; -> value of c is modified to 100

---

## How Reference type stores value ?
- For reference types, the stack holds a pointer to the object on the heap memory
- When setting a reference type variable equal to another reference type variabel, a copy of only the pointer is made
- Certain object types can't be manipulated on the heap(immutables)


![referecne type](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hdvxwxaa6ovupevc6ahm.png)



- int[ ] c = {1,2,3,4}; -> creates an array of integers in the heap memory and stack has a reference to that objetc in heap
- int[ ] d = c; -> value of reference(stored in stack) is copied from variable c(stack) to variable d(stack), no new object is cretaed in the heap memory (and not the actual object)

> d[1] = 99; -> value of object at index 1 is changed by variable d, which had reference of object {1,2,3,4,5} hence value is also changed for variable c, as they were having the same reference in the stack memory

- d = new int[5]; -> a new array is created in th heap memory and d rerences to that new array
- int[ ] e = {5,6,7,8} -> creates a new array in heap memory

> int [ ] f = {5,6,7,8} -> also creates a new array in the heap although content of array `e` and `f`is same still the exist in various memory space 

> f[1] = 99 -> this would only change index at 1 position for array `f` and not for array `e`

- String g = "hello"; -> a new string with value "hello" is created in the heap memory
- String h = g; -> value of reference stored in stack memory is copied form `g` to `h`(and not the actual object object)

>  h = "goodbye"; -> you might be expecting that the value of string `g` should also have been changed as both `g` and `h` are pointing at same string, but as Strings are `immutable` meaning they can't be modified, so instead a new string "goodbye" is cretaed and it's reference is assigned to `h` and `g` still points to hello

[for more reference and image credit](https://www.youtube.com/@BillBarnum)