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은 값이 변하지 않는다
}
}
String 클래스
2024. 1. 27. 12:24