자바 기초 - Type

자료형과 타입

기본형과 참조형

기본형 Primitive

20210807-163240

참조형


Call by Value & Call by Reference

Call by Value(값에 의한 호출)

public class Example {
    public static void methodBefore() {
        int a = 1;
        int b = 2;
        System.out.println("Before: " + "a = " + a + ", b = " + b);
        methodAfter(a, b);
        System.out.println("After: " + "a = " + a + ", b = " + b);
    }

    public static void methodAfter(int a, int b) {
        a = 3;
        b = 4;
    }

    public static void main(String[] args) {
        methodBefore();
    }
}


Call by Reference(참조에 의한 호출)

public class Example {
    public static void methodBefore() {
        Person person = new Person(27, "Poogle");
        System.out.println("Before: " + "age = " + person.age + ", name = " + person.name);
        methodAfter(person);
        System.out.println("After: " + "age = " + person.age + ", name = " + person.name);
    }

    public static void methodAfter(Person person) {
        person.age = 29;
        person.name = "Solar";
    }

    public static void main(String[] args) {
        methodBefore();
    }
}

class Person {
    int age;
    String name;

    public Person(int age, String name) {
        this.age = age;
        this.name = name;
    }
}
public class Example {
    public static void methodBefore() {
        Person person = new Person(27, "Poogle");
        System.out.println("Before: " + "age = " + person.age + ", name = " + person.name);
        methodAfter(person);
        System.out.println("After: " + "age = " + person.age + ", name = " + person.name);
    }

    public static void methodAfter(Person person) {
        person = new Person(29, "Solar");
    }

    public static void main(String[] args) {
        methodBefore();
    }
}

class Person {
    int age;
    String name;

    public Person(int age, String name) {
        this.age = age;
        this.name = name;
    }
}

20210803-172059


Constant Pool

String 객체를 생성하는 방법

Literal (“”)

new 연산자 사용

String, StringBuffer, StringBuilder

String

StringBuffer, StringBuilder

StringBuffer

StringBuffer sb1 = new StringBuffer("abc");
StringBuffer sb2 = new StringBuffer("abc");

sb1 == sb2; //false
sb1.equals(sb2); //false

String s1 = sb1.toString();
String s2 = sb2.toString();

s1.equals(s2); //true

StringBuilder