๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
BackEnd๐ŸŒฑ/Java

[Java] Object to int

by ์•ˆ์ฃผํ˜• 2022. 5. 6.

์„œ๋ก 

Java์˜ Object ํด๋ž˜์Šค๋Š” java.lang ํŒจํ‚ค์ง€ ์ค‘์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ํด๋ž˜์Šค์ด๋ฉฐ, ๋ชจ๋“  ์ž๋ฐ” ํด๋ž˜์Šค์˜ ์ตœ๊ณ  ์กฐ์ƒ ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ ๊ฒŒ์‹œ๊ธ€์—์„œ๋Š” Object ํƒ€์ž…์„ intํƒ€์ž…์œผ๋กœ ์บ์ŠคํŒ…ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ํ•œ๋ฒˆ ์ •๋ฆฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๋ฐฉ๋ฒ•

1. Object์˜ ๊ฐ’์ด Integer๋ผ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

int i = (Integer) object;

2. ๋งŒ์•ฝ ์ž๋ฐ” 7 ์ด์ƒ์ด๋ผ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

int i = (int) object;

Object๊ฐ€ Interger๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ClassCastException์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ, Object๊ฐ€ null์ผ ๊ฒฝ์šฐ์—๋Š” NullPointerException์ด ๋ฐœ์ƒํ•˜๊ธฐ์— Object์˜ ํƒ€์ž…์ด Interger์ž„์ด ๋ถ„๋ช…ํ•  ๋•Œ ์‚ฌ์šฉ๊ธฐ๋ฅผ ๊ถŒ์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

3. ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” String์œผ๋กœ ๋ณ€ํ™˜ํ•œ ํ›„์— ๋‹ค์‹œ int๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

int i = Integer.parseInt((String.valueOf(Object)));

์œ„ ์ฝ”๋“œ์˜ ๋™์ž‘ ์›๋ฆฌ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

    /**
     * Parses the string argument as a signed decimal integer. The
     * characters in the string must all be decimal digits, except
     * that the first character may be an ASCII minus sign {@code '-'}
     * ({@code '\u005Cu002D'}) to indicate a negative value or an
     * ASCII plus sign {@code '+'} ({@code '\u005Cu002B'}) to
     * indicate a positive value. The resulting integer value is
     * returned, exactly as if the argument and the radix 10 were
     * given as arguments to the {@link #parseInt(java.lang.String,
     * int)} method.
     *
     * @param s    a {@code String} containing the {@code int}
     *             representation to be parsed
     * @return     the integer value represented by the argument in decimal.
     * @exception  NumberFormatException  if the string does not contain a
     *               parsable integer.
     */
    public static int parseInt(String s) throws NumberFormatException {
        return parseInt(s,10);
    }

์œ„ ์ฝ”๋“œ๋Š”  Integer.parseInt() ๋ฉ”์„œ๋“œ์˜ ์‹ค์ œ ๊ตฌํ˜„ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ณด๋ฉด String์œผ๋กœ ๋ฐ›๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ String์œผ๋กœ ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›๊ธฐ ์œ„ํ•ด Object๊ฐ’์„ ๋จผ์ € String์œผ๋กœ ๋ณ€ํ™˜์‹œ์ผœ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. String.valueOf() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด์„œ Object๋ฅผ String์œผ๋กœ ๋ณ€ํ™˜์„ ์‹œ์ผœ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

   /**
     * Returns the string representation of the {@code int} argument.
     * <p>
     * The representation is exactly the one returned by the
     * {@code Integer.toString} method of one argument.
     *
     * @param   i   an {@code int}.
     * @return  a string representation of the {@code int} argument.
     * @see     java.lang.Integer#toString(int, int)
     */
    public static String valueOf(int i) {
        return Integer.toString(i);
    }

String.valueOf() ๋ฉ”์„œ๋“œ์˜ ์„ธ๋ถ€ ๊ตฌํ˜„์€ ์œ„์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

4. ์‚ฌ์‹ค valueOf๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋ฐ”๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด String์œผ๋กœ ์บ์ŠคํŒ…ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

int i = Integer.parseInt((String) Object);

 

์ •๋ฆฌ

  1. int i = (Integer) object;
  2. int i = (int) object; (์ž๋ฐ” 7์ด์ƒ)
  3. int i = Integer.parseInt((String.valueOf(Object)));
  4. int i = Integer.parseInt((String) Object);

๋Œ“๊ธ€