1. String.valueOf(int i)
/**
* 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 str = String.valueOf(12);
// str = "12"
- 들어온 데이터를 String형으로 변환하여 반환
2. Integer.toString(int i)
/**
* Returns a {@code String} object representing the
* specified integer. The argument is converted to signed decimal
* representation and returned as a string, exactly as if the
* argument and radix 10 were given as arguments to the {@link
* #toString(int, int)} method.
*
* @param i an integer to be converted.
* @return a string representation of the argument in base 10.
*/
public static String toString(int i) {
if (i == Integer.MIN_VALUE)
return "-2147483648";
int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
char[] buf = new char[size];
getChars(i, size, buf);
return new String(buf, true);
}
String str = Integer.toString(12);
// str = "12"
- 입력 받은 정수를 String형으로 변환하여 반환
3. String.valueOf() vs Integer.toString()
1) 속도 측면
String.valueOf() < Integer.toString()
- 기본적으로 String.valueOf()는 내부적으로 Integer.toString()을 호출하기 때문에 같은 기능을 함
- 따라서 속도 측면에서는 Integer.toString()이 유리
2) 유연성 측면
String.valueOf() > Integer.toString()
- String.valueOf()는 int형뿐만 아니라 boolean, char, long, float 등 다양한 형식을 지원
- 따라서 유연성 측면에서는 String.valueOf()
3) null값 지원
- String.valueOf() : null을 인자로 받을 수 있으나, NullPointerException을 발생시킴
- Integer.toString() : null을 인자로 받을 수 없음
4. Integer.toString(int i, int radix) -> 진법 변환
/**
* Returns a string representation of the first argument in the
* radix specified by the second argument.
*
* <p>If the radix is smaller than {@code Character.MIN_RADIX}
* or larger than {@code Character.MAX_RADIX}, then the radix
* {@code 10} is used instead.
*
* <p>If the first argument is negative, the first element of the
* result is the ASCII minus character {@code '-'}
* ({@code '\u005Cu002D'}). If the first argument is not
* negative, no sign character appears in the result.
*
* <p>The remaining characters of the result represent the magnitude
* of the first argument. If the magnitude is zero, it is
* represented by a single zero character {@code '0'}
* ({@code '\u005Cu0030'}); otherwise, the first character of
* the representation of the magnitude will not be the zero
* character. The following ASCII characters are used as digits:
*
* <blockquote>
* {@code 0123456789abcdefghijklmnopqrstuvwxyz}
* </blockquote>
*
* These are {@code '\u005Cu0030'} through
* {@code '\u005Cu0039'} and {@code '\u005Cu0061'} through
* {@code '\u005Cu007A'}. If {@code radix} is
* <var>N</var>, then the first <var>N</var> of these characters
* are used as radix-<var>N</var> digits in the order shown. Thus,
* the digits for hexadecimal (radix 16) are
* {@code 0123456789abcdef}. If uppercase letters are
* desired, the {@link java.lang.String#toUpperCase()} method may
* be called on the result:
*
* <blockquote>
* {@code Integer.toString(n, 16).toUpperCase()}
* </blockquote>
*
* @param i an integer to be converted to a string.
* @param radix the radix to use in the string representation.
* @return a string representation of the argument in the specified radix.
* @see java.lang.Character#MAX_RADIX
* @see java.lang.Character#MIN_RADIX
*/
public static String toString(int i, int radix) {
if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
radix = 10;
/* Use the faster version */
if (radix == 10) {
return toString(i);
}
char buf[] = new char[33];
boolean negative = (i < 0);
int charPos = 32;
if (!negative) {
i = -i;
}
while (i <= -radix) {
buf[charPos--] = digits[-(i % radix)];
i = i / radix;
}
buf[charPos] = digits[-i];
if (negative) {
buf[--charPos] = '-';
}
return new String(buf, charPos, (33 - charPos));
}
int n = 10;
String str = Integer.toString(n, 16);
// str = "a"
str = str.toUpperCase();
// str = "A"
- 입력 받은 정수(i)를 입력 받은 진수(radix)로 변환한 뒤 String형으로 반환
- int형으로 반환받고 싶으면 Integer.parseInt(String s, int radix)를 사용
'공부 > Java' 카테고리의 다른 글
[Java] Logging, SLF4J, Logback (0) | 2021.10.06 |
---|---|
[Java-14] 람다식(Lambda expression) (0) | 2021.07.06 |
[Java-13] 쓰레드(Thread) (0) | 2021.07.06 |
[Java-12] 애너테이션(Annotation) (0) | 2021.07.02 |
[Java-12] 열거형(Enums) (0) | 2021.07.02 |