package com.study.javastudy.study;

public class StringExam {
    public static void main(String[] args) {
        String str1 = "hello"; // 상수형으로 만든다
        String str2 = "hello"; // 새로 만들지 않고 같은 인스턴스를 참조하고 있다

        String str3 = new String("hello"); // 상수영역을 참조하지 않고 새로 만든다
        String str4 = new String("hello");

        if (str1 == str2)
            System.out.println("str1과 str2는 같은 레퍼런스입니다."); // 참

        if (str1 == str3)
            System.out.println("str1과 str3은 같은 레퍼런스입니다."); // 거짓, 다른 부분이다

        if (str3 == str4)
            System.out.println("str3과 str4은 같은 레퍼런스입니다."); // 거짓, 같지 않다

        System.out.println(str1); // hello

        System.out.println(str1.substring(3)); // lo 인덱스가 3번인것부터 잘라서 보여주세요

        System.out.println(str1); // hello, String은 값이 변하지 않는다
    }
}

'자바 기초' 카테고리의 다른 글

메소드 선언  (0) 2024.01.27
필드(field) 선언  (0) 2024.01.27
참조형 타입  (0) 2024.01.27
클래스 선언  (0) 2024.01.27
for each문  (0) 2024.01.27

+ Recent posts