-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5. reference data types.txt
More file actions
31 lines (27 loc) · 1.63 KB
/
5. reference data types.txt
File metadata and controls
31 lines (27 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
reference data types are also known as non-primitive types
they are used to store references (memory addresses) to objects rather than the actual data itself
This means that when you assign a reference type variable to another, both variables point to the same object in memory.
modifying the object via one reference affects the other, since both refer to the same memory location.
types of reference data types in java:
classes
interfaces
arrays
strings (immutable in java)
enums
some importtant points to consider:
if not initialized explicitly, the reference variables are automatically initialized to null indicating that they do not
point to any object right now
the default value of the primitives depends on the type
the objects whose addresses are stored by these reference variables are stored in the heap memory whereas the primitives
are stored in the stack memory
when an object is not being referenced by any reference variable, it becomes eligible for garbage collection and freeing up memory
reference types have some parameters associated with them which can be accessed via the dot operator;
-fields: these are the properties of the reference types
-methods: these are the functions associated with the reference types
the primitives are immutable but the reference types are mutable
NullPointerException:
-Accessing methods or fields on a reference variable that is null will result in a NullPointerException
-Always ensure the reference is not null before accessing its members.
Shared References:
-Assigning one reference variable to another copies the reference, not the actual object.
-Changes made through one reference affect the other