본문 바로가기

국비과정/JAVA (기초)

20230613 _[8일차]_05. String 사용법 *

[ String 객체생성 & 값비교 ]

package jun13;
// String 사용법
/*  문자열, "값", 값 불변, 주소, 참조타입
 */
import java.util.Arrays;

public class String01 {
	public static void main(String[] args) {
	
		int num = 10;
		int num2 = 10;	// 메모리 주소값을 복사
		
        
		String str = "Hi";
		String str2 = "Hi";
		
		System.out.println(str);	// Hi
		System.out.println(str2);	// Hi
        // 이미 해당값이 있을때는 같은 주소를 참조 => str == str2 같음 
		
		if(str == str2) {		// 메모리주소비교
			System.out.println("같습니다.");	// 같습니다.
		} else {
			System.out.println("다릅니다.");
		}
	
 // *******************************************************   
    
    
		String str = new String("Hi");	
		String str2 = new String("Hi");
		
		System.out.println(str);	// Hi
		System.out.println(str2);	// Hi	
        // 값은 같지만 값을 가지고 있는 주소가 다름
		
		if(str == str2) {		// 메모리주소비교
			System.out.println("같습니다.");
		} else {
			System.out.println("다릅니다.");	// 다릅니다.
		}
        
		if(str.equals(str2)) {		// 값비교(각각의 객체내부의)
			System.out.println("같습니다."); // 같습니다.
		} else {
			System.out.println("다릅니다.");
		}
		

		
	}
}

[ char & byte 배열 --> String ]

		// 문자열 = 문자 + 문자 + 문자 + 문자....
        
		char[] ch = {'가', '&', 'A', '1'};
//		char[] ch = new Char[] {'가', '&', 'A', '1'};  -> 같은표현
		String str3 = new String(ch);
		System.out.println(str3);	// 가&A1 --> 문자들이 하나로 합쳐져서 String화됨.
		
		
		byte[] by = new byte[] {65, 66, 67, 68, 69, 70};
		String str4 = new String(by);
		System.out.println(str4);	// ABCDEF --> 캐릭터로 변환-> 스트링화
		
//					str4		
		String str5 = new String(by, 0, 2); // src, 시작위치, length
		System.out.println(str5);	// AB

*** String str3 = new String(ch);

String 은 데이터타입이고, String 은 메서드의 역할

> String 메서드는 byte만 수용가능(ctrl + 마우스클릭-> 확인가능),  int는 String화 불가능

 


[ 문자열 쪼개기 ]   .length() & .charAt()

		String str2 = "안녕하세요";
		System.out.println(str2.length());	// 5
		char ch2 = str2.charAt(0);
		System.out.println(ch2);	// 안
		
		for (int i = 0; i < str2.length(); i++) {
			System.out.print(str2.charAt(i));
		}	// 안녕하세요
		
		str2 = "가나다라마바사";
		System.out.println(str2.length());	// 7
		// 맨 마지막 글자 "사"를 뽑아주세요
		System.out.println(str2.charAt( 6 ));	// 사
		System.out.println(str2.charAt( str2.length()-1 ));	// 사
		
		
		
		// .charAt() 활용
		String str7 = "sdfawekbnjsriwasadkwieotfskhnvngreaiwrwqaksdglm";
		// 여기에서 e가 몇개 있는지 갯수 출력
		
		int count = 0;
		for (int i = 0; i < str7.length(); i++) {
			if(str7.charAt(i) == 'e') {
				count++;
			}
		}
		System.out.println(count);	// 3

[ 문자열 메서드들 ]    concat, contains, indexOf, replaceAll, substring, equalsIgnoreCase, getByte

		String str = "가나다라마바사";
		
		// .concat() - 문자열추가
		String str2 = str.concat("님");  
		System.out.println(str2); // 가나다라마바사 + 님 --> 가나다라마바사님
		
		// .contains() - 해당 글자를 포함하고 있는지 물어보기
		System.out.println(str.contains("님"));	// false
		System.out.println(str2.contains("님"));	// true
		System.out.println(str2.contains("가"));	// true
		System.out.println(str2.contains("가나다"));	// true
		
		// .indexOf() - 글자위치 및 글자포함유무(-1) 알수있음
		System.out.println(str2.indexOf("나"));	// 1
		System.out.println(str2.indexOf("님"));	// 7
		System.out.println(str2.indexOf("라마바"));	// 3
		System.out.println(str2.indexOf("타"));	// -1  (int타입)
		System.out.println(str2.indexOf("타파하"));	// -1  (int타입)
		
		// .replaceAll() - 문자열 대체
		String apple = "apple";
		apple = apple.replaceAll("p", "피");
		System.out.println(apple);	// a피피le

		// .substring() - 문자열 뽑아내기 
		String result = str.substring(0); // (원하는 위치부터 끝까지)
		System.out.println(result);	// 가나다라마바사
		System.out.println(str.substring(3));	// 라마바사
		System.out.println(str.substring(str.indexOf("라")));	// 라마바사
		
        
		result = str.substring(1, 5);	// (시작index ~ 끝index)
		System.out.println(result);	// 나다라마 (끝index 바로전까지)
		
		result = str.substring(3, 4);
		System.out.println(result);	// 라
		
		// .equalsIgnoreCase(), .equals()
		apple = "apple";
		System.out.println(apple.equalsIgnoreCase("apple"));	// true
		System.out.println(apple.equalsIgnoreCase("APPLE"));	// true
		System.out.println(apple.equals("apple")); // true
		System.out.println(apple.equals("APPLE")); // false -> 대소문자구분
		
		// getByte() - 문자열 -> 문자(char) -> byte로 변환 
		byte[] appleByte = apple.getBytes();
		System.out.println(Arrays.toString(appleByte));	// [97, 112, 112, 108, 101]

[Java] - 깊은 복사(Deep Copy) vs 얕은 복사(Shallow Copy) (tistory.com)

 

[Java] - 깊은 복사(Deep Copy) vs 얕은 복사(Shallow Copy)

📎 Java 깊은 복사(Deep Copy)와 얕은 복사(Shallow Copy) 안녕하세요! 이번에 정리할 내용은 자바에서의 깊은 복사와 얕은 복사 입니다. 깊은 복사와 얕은 복사라는 개념은 평소에 접한적이 꽤 있었습

zzang9ha.tistory.com

* String 생성자, 메서드 *

https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html

 

String (Java SE 17 & JDK 17)

All Implemented Interfaces: Serializable, CharSequence, Comparable , Constable, ConstantDesc The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. Strings are constan

docs.oracle.com

 


[String2.java] 정리 ㅠㅠㅠ