Primitive Types & Reference Types

invisibleufo101·2024년 9월 14일

Java

목록 보기
2/10
post-thumbnail

Primitive Types

int, double, float, String, boolean, literals

  • Storage: Stored in the stack area (JVM)

Reference Types

Object representations of data.

  • Storage: Memory address is stored in the stack, but the actual value (object) is stored in the heap.

Memory Allocation

  • Memory Address: Stack
  • Value: Heap

Runtime Data Area (JVM)

Will post more about this later.

But for now, here's what I know:

  • Method Area:
    • Stores methods in categories:
      • Static fields
      • Constants
      • Constructors
      • Methods
  • Heap Area:
    • Objects and arrays are created here.
  • JVM Stack Area:
    • A frame is added whenever a method is called.
    • When methods in the stack are fully executed, the frame is removed (via Garbage Collection - System.gc).

Boolean Operations with Reference Types

  • For primitive types, boolean operations like == and != compare values.
  • For reference types, these operators compare memory addresses.

Example

int a = 1;
int b = 1;
System.out.println(a == b); // true

String a = new String("a");
String b = new String("a");
System.out.println(a == b); // false
  • a == b is false because String a and String b are different objects with different memory addresses.

Assigning Reference Types

String a = new String("a");
String b = a;
System.out.println(a == b); // true
  • a == b is true because b now references the same object as a.

Null and NullPointerException

  • NULL: Reference types can have the value NULL, meaning they are not referencing any object.
  • NullPointerException: Occurs when you try to access methods on a null reference.

Examples

int[] arr = null;
arr[0] = 10; // NullPointerException

String str = null;
System.out.println(str.length()); // NullPointerException

Both examples cause a NullPointerException because arr and str are not pointing to any object.

The new Keyword

Used to create a new object for a reference type variable.

String a = new String("a");

In this case, the value "a" is passed to the constructor of String.

.equals() for String Comparison

.equals() evaluates the value of the string object, not the memory address like ==.

boolean ret = str1.equals(str2);

Primitive Types Behavior

int a = 1;
int b = a;

a += 2;

System.out.println(a); // 3
System.out.println(b); // 1
  • For primitive types, the value is stored directly in the stack.
  • When int b = a is executed, b gets a copy of a's value. Therefore, when a is modified, it doesn't affect b.

Extra)

Pretty much in most langauges, Strings are immutable!
I think that's why things like this happen:

String Immutability

String a = new String("a");
String b = a;

a += "bcd";

System.out.println(a); // "abcd"
System.out.println(b); // "a"

What is going on here?

  • When a is concatenated with "bcd", Java creates a new String object.
  • Therefore, a and b are now referencing different objects.
profile
하나씩 차근차근

0개의 댓글