DEV Community

Cover image for Memory Handling in Java
Atul Kushwaha
Atul Kushwaha

Posted on

Memory Handling in Java

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

  • 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

  • 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;
Enter fullscreen mode Exit fullscreen mode

primitive_stack

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

  • 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 fis 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

Top comments (0)