Responsive Advertisement

Java์—์„œ ๋ฌธ์ž ๋‹ค๋ฃจ๊ธฐ: char ํƒ€์ž…๊ณผ Character ํด๋ž˜์Šค ํ™œ์šฉ๋ฒ•

Java์—์„œ ๋ฌธ์ž(Character)๋Š” ๋‹จ์ผ ๋ฌธ์ž๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…์œผ๋กœ, ๋ฌธ์ž์™€ ๊ด€๋ จ๋œ ๋‹ค์–‘ํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Java์—์„œ๋Š” ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž… char์™€ ๊ฐ์ฒดํ˜• ๋ž˜ํผ ํด๋ž˜์Šค Character๋ฅผ ์‚ฌ์šฉํ•ด ๋ฌธ์ž๋ฅผ ๋‹ค๋ฃจ๋ฉฐ, ์œ ๋‹ˆ์ฝ”๋“œ๋ฅผ ์ง€์›ํ•˜๋ฏ€๋กœ ์ „ ์„ธ๊ณ„์˜ ๋‹ค์–‘ํ•œ ๋ฌธ์ž ์„ธํŠธ๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ๊ธ€์—์„œ๋Š” Java์—์„œ char์™€ Character๋ฅผ ์„ ์–ธํ•˜๊ณ  ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

Java์˜ char ๋ฐ์ดํ„ฐ ํƒ€์ž…

char ๋ฐ์ดํ„ฐ ํƒ€์ž…์€ ๋‹จ์ผ ๋ฌธ์ž๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ฌธ์ž ํ•˜๋‚˜๋ฅผ ์ž‘์€ ๋”ฐ์˜ดํ‘œ(' ')๋กœ ๊ฐ์‹ธ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. char๋Š” 2๋ฐ”์ดํŠธ ํฌ๊ธฐ์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ฐจ์ง€ํ•˜๋ฉฐ, ์œ ๋‹ˆ์ฝ”๋“œ ๊ฐ’์„ ํ†ตํ•ด ๋‹ค์–‘ํ•œ ์–ธ์–ด์˜ ๋ฌธ์ž๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

char ๋ณ€์ˆ˜ ์„ ์–ธ ์˜ˆ์ œ

public class CharExample {
    public static void main(String[] args) {
        char letter = 'A';
        char digit = '1';
        char symbol = '#';

        System.out.println("๋ฌธ์ž: " + letter);
        System.out.println("์ˆซ์ž ๋ฌธ์ž: " + digit);
        System.out.println("๊ธฐํ˜ธ: " + symbol);
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ:

๋ฌธ์ž: A
์ˆซ์ž ๋ฌธ์ž: 1
๊ธฐํ˜ธ: #

์œ„ ์ฝ”๋“œ์—์„œ๋Š” ๋ฌธ์ž(A), ์ˆซ์ž ๋ฌธ์ž(1), ํŠน์ˆ˜ ๋ฌธ์ž(#)๋ฅผ ๊ฐ๊ฐ char ํƒ€์ž… ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•˜๊ณ  ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž ํ‘œํ˜„

Java์˜ char๋Š” ์œ ๋‹ˆ์ฝ”๋“œ ๊ฐ’(16์ง„์ˆ˜)์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž๋ฅผ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž๋Š” '\uXXXX' ํ˜•์‹์œผ๋กœ ํ‘œํ˜„๋˜๋ฉฐ, XXXX๋Š” 4์ž๋ฆฌ์˜ 16์ง„์ˆ˜์ž…๋‹ˆ๋‹ค.

์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž ์˜ˆ์ œ

public class UnicodeExample {
    public static void main(String[] args) {
        char letterA = '\u0041'; // 'A'์˜ ์œ ๋‹ˆ์ฝ”๋“œ
        char letterKorean = '\uAC00'; // '๊ฐ€'์˜ ์œ ๋‹ˆ์ฝ”๋“œ

        System.out.println("์œ ๋‹ˆ์ฝ”๋“œ A: " + letterA);
        System.out.println("์œ ๋‹ˆ์ฝ”๋“œ ๊ฐ€: " + letterKorean);
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ:

์œ ๋‹ˆ์ฝ”๋“œ A: A
์œ ๋‹ˆ์ฝ”๋“œ ๊ฐ€: ๊ฐ€

์œ„ ์ฝ”๋“œ์—์„œ๋Š” ์œ ๋‹ˆ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ 'A'์™€ '๊ฐ€' ๋ฌธ์ž๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์œ ๋‹ˆ์ฝ”๋“œ๋Š” ๋‹ค์–‘ํ•œ ์–ธ์–ด์˜ ๋ฌธ์ž ํ‘œํ˜„์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.

Character ํด๋ž˜์Šค

Character ํด๋ž˜์Šค๋Š” char ํƒ€์ž…์— ๋Œ€ํ•œ ๋ž˜ํผ ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค. Character ํด๋ž˜์Šค๋Š” ๋ฌธ์ž์— ๋Œ€ํ•œ ์—ฌ๋Ÿฌ ์œ ์šฉํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•˜๋ฉฐ, ๋ฌธ์ž ๋ณ€ํ™˜, ๋ฌธ์ž ์œ ํ˜• ๊ฒ€์‚ฌ ๋“ฑ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Character ๊ฐ์ฒด ์ƒ์„ฑ

public class CharacterWrapperExample {
    public static void main(String[] args) {
        Character ch = Character.valueOf('A');
        System.out.println("Character ๊ฐ์ฒด: " + ch);
    }
}

์œ„ ์ฝ”๋“œ์—์„œ๋Š” Character.valueOf ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด Character ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

Character ํด๋ž˜์Šค์˜ ์œ ์šฉํ•œ ๋ฉ”์„œ๋“œ

1. ๋ฌธ์ž ์œ ํ˜• ํ™•์ธ ๋ฉ”์„œ๋“œ

Character ํด๋ž˜์Šค๋Š” ๋ฌธ์ž๊ฐ€ ์ˆซ์ž์ธ์ง€, ๋Œ€๋ฌธ์ž์ธ์ง€, ์†Œ๋ฌธ์ž์ธ์ง€๋ฅผ ํ™•์ธํ•˜๋Š” ๋‹ค์–‘ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์ œ ์ฝ”๋“œ

public class CharacterMethodsExample {
    public static void main(String[] args) {
        char letter = 'A';
        char digit = '1';

        System.out.println("isLetter: " + Character.isLetter(letter)); // true
        System.out.println("isDigit: " + Character.isDigit(digit)); // true
        System.out.println("isUpperCase: " + Character.isUpperCase(letter)); // true
        System.out.println("isLowerCase: " + Character.isLowerCase(letter)); // false
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ:

isLetter: true
isDigit: true
isUpperCase: true
isLowerCase: false

์œ„ ์˜ˆ์ œ์—์„œ๋Š” Character.isLetter, Character.isDigit ๋“ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž ์œ ํ˜•์„ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค.

2. ๋ฌธ์ž ๋ณ€ํ™˜ ๋ฉ”์„œ๋“œ

Character ํด๋ž˜์Šค๋Š” ๋ฌธ์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜๊ฑฐ๋‚˜ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์ œ ์ฝ”๋“œ

public class CharacterConversionExample {
    public static void main(String[] args) {
        char lower = 'b';
        char upper = Character.toUpperCase(lower);
        char lowerAgain = Character.toLowerCase(upper);

        System.out.println("๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜: " + upper); // B
        System.out.println("์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜: " + lowerAgain); // b
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ:

๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜: B
์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜: b

์œ„ ์ฝ”๋“œ์—์„œ๋Š” Character.toUpperCase์™€ Character.toLowerCase๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž๋ฅผ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

char์™€ String ๊ฐ„์˜ ๋ณ€ํ™˜

char์™€ String ๊ฐ„ ๋ณ€ํ™˜์€ Java์—์„œ ์ž์ฃผ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. String์—์„œ ๋‹จ์ผ ๋ฌธ์ž๋ฅผ ์ถ”์ถœํ•˜๊ฑฐ๋‚˜, char๋ฅผ String์œผ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

1. char๋ฅผ String์œผ๋กœ ๋ณ€ํ™˜

public class CharToStringExample {
    public static void main(String[] args) {
        char ch = 'A';
        String str = Character.toString(ch); // ๋˜๋Š” String.valueOf(ch);
        
        System.out.println("String์œผ๋กœ ๋ณ€ํ™˜: " + str);
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ:

String์œผ๋กœ ๋ณ€ํ™˜: A

Character.toString ๋˜๋Š” String.valueOf๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ char๋ฅผ String์œผ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

2. String์—์„œ char ์ถ”์ถœ

public class StringToCharExample {
    public static void main(String[] args) {
        String str = "Hello";
        char ch = str.charAt(0); // ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž ์ถ”์ถœ

        System.out.println("์ถ”์ถœํ•œ ๋ฌธ์ž: " + ch);
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ:

์ถ”์ถœํ•œ ๋ฌธ์ž: H

String.charAt ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด์˜ ํŠน์ • ์œ„์น˜์— ์žˆ๋Š” char๋ฅผ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ฒฐ๋ก 

Java์—์„œ char์™€ Character ํด๋ž˜์Šค๋Š” ๋‹จ์ผ ๋ฌธ์ž๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฐ ์œ ์šฉํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. char๋Š” ๋‹จ์ˆœํ•œ ๋ฌธ์ž ์ €์žฅ์šฉ์œผ๋กœ, Character ํด๋ž˜์Šค๋Š” ๋ฌธ์ž ๊ฒ€์‚ฌ์™€ ๋ณ€ํ™˜ ๋“ฑ ๋‹ค์–‘ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ์ง€์›ํ•˜์—ฌ ๋ฌธ์ž๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฐ ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค. ์œ„ ์˜ˆ์ œ๋“ค์„ ํ†ตํ•ด Java์—์„œ ๋ฌธ์ž ๋ฐ์ดํ„ฐ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ณ  ํ™œ์šฉํ•ด ๋ณด์„ธ์š”!

๋Œ“๊ธ€ ์“ฐ๊ธฐ