Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Charicteristic of Java reference type

String type example

  • 참조 타입에서 ==, != 연산은 같은 객체인지를 비교한다. 즉 메모리 번지가 같은지를 비교한다.
  • 값을 비교하기 위해서는 Object.equals API를 이용한다
jshell> String var1 = "appkr";
strVar1 ==> "appkr"

jshell> String var2 = "appkr";
strVar2 ==> "appkr"

jshell> String var3 = new String("appkr");
strVar3 ==> "appkr"

jshell> var1 == var2;
$4 ==> true

jshell> var1 == var3;
$5 ==> false

jshell> var1.equals(var3);
$6 ==> true

Stack

  • var1은 Heap 메모리 번지 (a)를 참조
  • var2는 같은 리터럴이므로 Heap 메모리 번지 (a)를 참조
  • var3는 new 키워드를 써서 객체를 직접 생성했으므로 Heap 메모리 번지 (b)를 참조
+----+-----+
|var1| (a) |
+----+-----+
|var2| (a) |
+----+-----+
|var3| (b) |
+----+-----+

StringPool on Heap

+-----+-------+
| (a) | appkr |
+-----+-------+
| (b) | appkr |
+-----+-------+
@appkr
Copy link
Author

appkr commented Oct 5, 2020

Primitive type

int a = 10;
int b = a;
a = 100;

System.out.println("a=" + a + ", b=" + b); // a=100, b=10

@appkr
Copy link
Author

appkr commented Oct 5, 2020

Reference type

class Person {
  String name;
  Person(String name) {
    this.name = name;
  }
}

Person foo = new Person("foo");
Person bar = foo;

System.out.println("foo=" + foo.name + ", bar=" + bar.name); // foo=foo, bar=foo

foo.name = "qux";

System.out.println("foo=" + foo.name + ", bar=" + bar.name); // foo=qux, bar=qux

bar.name = "baz";

System.out.println("foo=" + foo.name + ", bar=" + bar.name); // foo=baz, bar=baz
class Person implements Cloneable {
  String name;
  Person(String name) {
    this.name = name;
  }
  public Person clone() throws CloneNotSupportedException {
    return (Person)super.clone();
  }
}

Person foo = new Person("foo");
Person bar = foo.clone();

@appkr
Copy link
Author

appkr commented Oct 5, 2020

Another Interview Ququestions:

Why this is wrong?

byte b = 1_000; // byte can express only -128 to 127

What is this byte array in string?

byte[] b = {97, 114, 116}; 
System.out.println(new String(b)); // art

What is the value of

@appkr
Copy link
Author

appkr commented Mar 17, 2021

String[] a = {"one", "two"}
String[] b = a
String[] c = new String[] {"one", "two"}

a == b // true
a == c // false

@appkr
Copy link
Author

appkr commented Apr 3, 2021

char v1 = 'A'; // 1 시점의 메모리 상태는?

if (v1 == 'A') {
  int v2 = 100;
  double v3 = 3.14;
  int[] v4 = {10, 20, 30}; // 2 시점의 메모리 상태는?
}

boolean v5 = true; // 3 시점의 메모리 상태는?

1 시점

스택
+----+---------+
| v1 |  A      |
+----+---------+

2 시점

스택                   힙 5번지
+----+---------+      +----+------+------+------+
| v4 |  addr5  |  ->  | v4 |  10  |  20  |  30  |
+----+---------+      +----+------+------+------+
| v3 |  3.14   |
+----+---------+
| v2 |  100    |
+----+---------+
| v1 |  A      |
+----+---------+

3 시점

+----+---------+
| v5 |  true   |
+----+---------+
| v1 |  A      |
+----+---------+

@appkr
Copy link
Author

appkr commented Apr 5, 2021

byte[] b = {0b1000001}; new String(b); // "A"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment