Responsive Advertisement

Java Numbers and Strings: ์ˆซ์ž์™€ ๋ฌธ์ž์—ด ๋‹ค๋ฃจ๊ธฐ

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

1. Java์—์„œ ์ˆซ์ž ๋‹ค๋ฃจ๊ธฐ

์ˆซ์ž๋Š” ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž…(Primitive Data Types)๊ณผ ๋ž˜ํผ ํด๋ž˜์Šค(Wrapper Class)๋ฅผ ํ†ตํ•ด ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

1.1 ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž…

Java์˜ ๊ธฐ๋ณธ ์ˆซ์ž ๋ฐ์ดํ„ฐ ํƒ€์ž…์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:

  • byte: 1๋ฐ”์ดํŠธ ํฌ๊ธฐ, ๋ฒ”์œ„ -128 ~ 127
  • short: 2๋ฐ”์ดํŠธ ํฌ๊ธฐ, ๋ฒ”์œ„ -32,768 ~ 32,767
  • int: 4๋ฐ”์ดํŠธ ํฌ๊ธฐ, ๋ฒ”์œ„ -2,147,483,648 ~ 2,147,483,647
  • long: 8๋ฐ”์ดํŠธ ํฌ๊ธฐ, ๋ฒ”์œ„ -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807
  • float: 4๋ฐ”์ดํŠธ ํฌ๊ธฐ, ์†Œ์ˆ˜์  ์ดํ•˜ ์•ฝ 7์ž๋ฆฌ
  • double: 8๋ฐ”์ดํŠธ ํฌ๊ธฐ, ์†Œ์ˆ˜์  ์ดํ•˜ ์•ฝ 15์ž๋ฆฌ

์ˆซ์ž ๋ฐ์ดํ„ฐ ํƒ€์ž… ์˜ˆ์ œ

public class NumberExample {
    public static void main(String[] args) {
        int age = 25;
        double pi = 3.14159;

        System.out.println("๋‚˜์ด: " + age);
        System.out.println("ํŒŒ์ด ๊ฐ’: " + pi);
    }
}

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

๋‚˜์ด: 25
ํŒŒ์ด ๊ฐ’: 3.14159

1.2 ๋ž˜ํผ ํด๋ž˜์Šค

Java๋Š” ๊ธฐ๋ณธ ํƒ€์ž…์„ ๊ฐ์ฒด์ฒ˜๋Ÿผ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋Š” ๋ž˜ํผ ํด๋ž˜์Šค๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. Integer, Double, Float ๋“ฑ์ด ์ด์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค.

๋ž˜ํผ ํด๋ž˜์Šค ์‚ฌ์šฉ ์˜ˆ์ œ

public class WrapperExample {
    public static void main(String[] args) {
        Integer intObj = Integer.valueOf(100);
        Double doubleObj = Double.valueOf(3.14);

        System.out.println("์ •์ˆ˜ ๊ฐ์ฒด: " + intObj);
        System.out.println("์‹ค์ˆ˜ ๊ฐ์ฒด: " + doubleObj);
    }
}

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

์ •์ˆ˜ ๊ฐ์ฒด: 100
์‹ค์ˆ˜ ๊ฐ์ฒด: 3.14

1.3 ์ˆซ์ž ๋ณ€ํ™˜

Java์—์„œ๋Š” ๋ฌธ์ž์—ด์„ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•˜๊ฑฐ๋‚˜, ์ˆซ์ž๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ˆซ์ž ๋ณ€ํ™˜ ์˜ˆ์ œ

public class NumberConversion {
    public static void main(String[] args) {
        String strNumber = "123";
        int number = Integer.parseInt(strNumber);

        System.out.println("๋ฌธ์ž์—ด์„ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜: " + number);

        String backToString = Integer.toString(number);
        System.out.println("์ •์ˆ˜๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜: " + backToString);
    }
}

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

๋ฌธ์ž์—ด์„ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜: 123
์ •์ˆ˜๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜: 123

2. Java์—์„œ ๋ฌธ์ž์—ด ๋‹ค๋ฃจ๊ธฐ

Java์—์„œ ๋ฌธ์ž์—ด์€ String ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค. String์€ ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

2.1 ๋ฌธ์ž์—ด ์ƒ์„ฑ

๋ฌธ์ž์—ด์€ ํฐ ๋”ฐ์˜ดํ‘œ("")๋กœ ๊ฐ์‹ธ์„œ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค.

๋ฌธ์ž์—ด ์ƒ์„ฑ ์˜ˆ์ œ

public class StringCreation {
    public static void main(String[] args) {
        String greeting = "Hello, World!";
        System.out.println(greeting);
    }
}

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

Hello, World!

2.2 ์ฃผ์š” ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ

Java์˜ String ํด๋ž˜์Šค๋Š” ๋‹ค์–‘ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ ์˜ˆ์ œ

public class StringMethods {
    public static void main(String[] args) {
        String text = "Java Programming";

        System.out.println("๋ฌธ์ž์—ด ๊ธธ์ด: " + text.length());
        System.out.println("๋ถ€๋ถ„ ๋ฌธ์ž์—ด: " + text.substring(5, 16));
        System.out.println("๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜: " + text.toUpperCase());
    }
}

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

๋ฌธ์ž์—ด ๊ธธ์ด: 16
๋ถ€๋ถ„ ๋ฌธ์ž์—ด: Programming
๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜: JAVA PROGRAMMING

2.3 ์ˆซ์ž์™€ ๋ฌธ์ž์—ด์˜ ๊ฒฐํ•ฉ

์ˆซ์ž์™€ ๋ฌธ์ž์—ด์„ ๊ฒฐํ•ฉํ•˜๋ ค๋ฉด + ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜, String.format() ๋˜๋Š” StringBuilder๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ˆซ์ž์™€ ๋ฌธ์ž์—ด ๊ฒฐํ•ฉ ์˜ˆ์ œ

public class StringNumberConcat {
    public static void main(String[] args) {
        int year = 2024;
        String message = "The current year is " + year;

        System.out.println(message);

        String formattedMessage = String.format("The current year is %d", year);
        System.out.println(formattedMessage);
    }
}

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

The current year is 2024
The current year is 2024

3. ์ˆซ์ž์™€ ๋ฌธ์ž์—ด ๋ณ€ํ™˜์˜ ํ™œ์šฉ

์ˆซ์ž์™€ ๋ฌธ์ž์—ด์˜ ๋ณ€ํ™˜์€ ๋‹ค์–‘ํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์‚ฌ์šฉ์ž ์ž…๋ ฅ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•˜๊ฑฐ๋‚˜ ์ˆซ์ž๋ฅผ ๊ฐ€๊ณตํ•˜์—ฌ ์ถœ๋ ฅ ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ™œ์šฉ ์˜ˆ์ œ

public class NumberStringUtil {
    public static void main(String[] args) {
        double price = 19.99;
        int quantity = 3;

        String receipt = String.format("๊ฐ€๊ฒฉ: %.2f, ์ˆ˜๋Ÿ‰: %d, ์ด์•ก: %.2f", price, quantity, price * quantity);
        System.out.println(receipt);
    }
}

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

๊ฐ€๊ฒฉ: 19.99, ์ˆ˜๋Ÿ‰: 3, ์ด์•ก: 59.97

๊ฒฐ๋ก 

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

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