공책

String의 특징 본문

웹 개발/Java

String의 특징

QTHoney 2013. 8. 29. 15:34

1. 객체 생성법이 두 가지이다.(암시적, 명시적)

2. 한번 생성된 문자열의 내용은 변하지 않는다.


public class Ex1_String {

public static void main(String[] args) {

String s0 = "abc";

String s1 = "abc"; //암시적 객체 생성

String s2 = new String("abc"); //명시적 객체 생성

if (s0 == s1){

System.out.println("주소가 같다");

} else {

System.out.println("주소가 다르다");

}

if(s1.equals(s2)){

System.out.println("주소가 같다");

} else {

System.out.println("주소가 다르다.");

}

}

}


public class Ex2_String {

public static void main(String[] args) {

String str = "Lee Sang-Woon";

System.out.println("문자열 str 의 길이" + str.length());

int index = str.indexOf('e');

System.out.println(index);

index = str.indexOf("Sang");

System.out.println("문자열 Sang의 위치"+index);

char c = str.charAt(4);

System.out.println(c);

String str2 = str.substring(0, 4);

System.out.println(str2);

}

}

'웹 개발 > Java' 카테고리의 다른 글

메소드  (0) 2013.08.30
입력받은 문자열중 특정 문자열이 몇개 인지 찾는 로직  (0) 2013.08.29
사람 성적 등록  (0) 2013.08.29
2차원 배열의 모든합 평균 구하기  (0) 2013.08.29
베이스볼 게임 만들기  (0) 2013.08.29
Comments