Wrapper Class의 toString()

public class Test01 {
	public static void main(String[] args) {
//		== : 주소비교연산자
//		equals : new Car("bm7") == new Car("bm7")에서 "" 값을 비교하는?

		Integer i = new Integer(100);
		Integer i2 = new Integer("200");
		
		System.out.println(i);
		System.out.println(i2 + 10);
		System.out.println(i2.toString());
		
		Double d1 = new Double("97.7");
		System.out.println(d1 + 10);
		System.out.println(d1 + d1.toString());  // 그냥 d1은 숫자라 덧셈이 되는데 d1.toString()은 문자열이라 합치기가 되네
		
		Integer i3 = 100; // 이렇게 new Integer 없이 해도 알아듣는다.
		System.out.println(i3);
		
		double d4 = new Double(98.9);  // 이렇게 반대로 해도 안다 객체를 일반 자료형으로 형변환해서 객체에서 사용하는 함수를 쓸 수 없다.
		System.out.println(d4);  //  네트워크할 때는 클래스 단위로 주고받아야 한다. 기본자료형으로는 줄 수가 없음.
		
	}
}

 

============================================================================

String 클래스의 생성자함수

public class Test02 {

	public static void main(String[] args) {
//		String(String original) 아래 4개의 방식으로 하는 생성은 전부 옆에 생성자를 호출한다.
		String ori = "The String class represents character strings";
		String str02 = new String(ori);
		String str03 = new String(new String("The String class represents character strings"));
		String str04 = new String("The String class represents character strings");
		
		System.out.println(ori);
		System.out.println(str02);
		System.out.println(str03);
		System.out.println(str04);
		
		String res = ori.toUpperCase();
		System.out.println(res);

	}

}

 

============================================================================

String과 StringBuffer, StringBuilder의 차이

StringBuffer와 StringBuilder의 CRUD 함수

public class Test03 {

	public static void prn02() {
		// String = C
		// StringBuffer = C : append, insert // R : toString() // U : replace(int start,
		// int end, String str), setCharAt(int index, char ch)
		// D : delete(int start, int end), deleteCharAt(int index)
		// A thread-safe, mutable sequence of characters.

		// StringBuilder = C, R : , U , D

		StringBuffer sb = new StringBuffer(); // StringBuffer의 capacity는 초기값 16 이후 필요할때마다 18의 배수로 증가한다.
		System.out.println(sb.capacity()); // 16 => 34 => 70 => 142 => 286

//		sb.append("abc");
		for (int i = 65; i <= 80; i++) { // 65 ~ 80
			sb.append((char) i);
			System.out.println(i + " : " + sb.capacity());
		}

		System.out.println(sb);
		sb.delete(3, 6);
		System.out.println(sb);

	}

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

	}

}

 

============================================================================

문제1. 자바 API에서 String 클래스에서 메서드를 찾아 문제 해결하기

// 문제 1) The String class represents character string.의 내용을 cnvr()메소드로 대입해서
// 대문자의 개수와 소문자의 개수를 출력하고 대문자를 소문자로, 소문자를 대문자로 변환 후 리턴받는다.

//1. String => char[] 메소드 찾아
//2. for -> character의 소문자인지 대문자인지 판별하는 메소드를 이용해서 조건문을 사용 후 cnt한다.
//character의 대문자로 소문자로 바꿔주는 메소드를 찾아서 변환
public class Exam01 {

	public static String cnvr(String str) {
		int lower = 0;
		int upper = 0;
		char[] new_str = str.toCharArray();

		for (int i = 0; i < new_str.length; i++) {
			if (Character.isLowerCase(new_str[i])) {
				lower++;
				new_str[i] = Character.toUpperCase(new_str[i]);
				continue;
			}
			if (Character.isUpperCase(new_str[i])) {
				upper++;
				new_str[i] = Character.toLowerCase(new_str[i]);
			}

		}
		System.out.println("upper : " + upper);
		System.out.println("lower : " + lower);

		return String.valueOf(new_str);
	}

	public static void main(String[] args) {
		String str = "The String class represents character string.";
		String res = cnvr(str);
		System.out.println(res);

	}

}

 

문제2. 

import java.util.Scanner;

// 문제 2) str을 받은 cnvr을 조작해보자
// 1. 전체를 대문자로 출력
// 2. 전체를 소문자로 출력
// 3. 공백을 제거 후 출력
// 4. 공백을 찾아 하트로 출력
// 5. 입력된 글자를 삭제 후 출력 = Scanner를 사용한다.
// 6. 문자열을 하나씩 바이트로 출력 getBytes
// 7. 문자열을 공백으로 분철해서 분철된 데이터를 출력
public class Exam02 {

	public static void cnvr(String str) {
		System.out.println("1. " + str.toUpperCase());
		System.out.println("2. " + str.toLowerCase());
		System.out.println("3. " + str.replace(" ", ""));
		System.out.println("4. " + str.replace(" ", "♥"));
		Scanner sc = new Scanner(System.in);
		String res = sc.next();
		sc.close();
		System.out.println("5. " + str.replace(res, " "));
		for (byte i : str.getBytes()) {
			System.out.println("6. " + i);
		}
		for (String i : str.split("\s")) {
			System.out.println("7. " + i);
		}

	}

	public static void main(String[] args) {
		String str = "The String class represents character string.";
		cnvr(str);
	}

}

+ Recent posts