안녕하세요, 여행벌입니다.

오늘은 Wrapper 클래스 중 자주 쓰이는 Integer 클래스에 대해서 정리해보겠습니다.


public static int bitCount(int i)

 매개변수로 들어온 정수를 2의 보수법으로 표현했을 때, bit 1이 몇 개 있는지 return 해줍니다.

1
2
3
4
5
public class test{
    public static void main(String args[]) {
        System.out.println(Integer.bitCount(4));
    }
}
cs

 4는 2진수로 표현하면 100(2) 이므로 bit 1이 1개, bit 0이 31개 이므로 1을 return 해줍니다.

public static String toString(int i)

 toString 메소드는 입력된 정수를 String 으로 바꿔서 return 해줍니다.

1
2
3
4
5
public class test{
    public static void main(String args[]) {
        System.out.println(Integer.toString(400000));
    }
}
cs

 int 형 값을 String 으로 바꿀 때 자주 사용되는 메소드입니다.

public static String toString(int i, int radix)

 toString 메소드는 오버로딩 되어있어, 매개변수를 하나 더 입력받을 수 있습니다.

 radix에 따라서 다른 진법으로 정수를 표현해 줍니다.

1
2
3
4
5
6
public class test{
    public static void main(String args[]) {
        // 2진법으로 10을 표현하고 String 으로 반환.
        System.out.println(Integer.toString(102));
    }
}
cs

 위의 코드는 숫자 10을 2진법으로 표현한 1010을 String 으로 반환합니다.

public static int parseInt(String s) throws NumberFormatException

 반대로 String 을 매개변수로 입력받아 Int 형으로 변환해주는 메소드입니다.

1
2
3
4
5
6
public class test{
    public static void main(String args[]) {
        // 2진법으로 10을 표현하고 String 으로 반환.
        System.out.println(Integer.parseInt("10000"));
    }
}
cs

public static int parseInt(String s, int radix) throws NumberFormatException

 parseInt 메소드는 오버로딩 되어있어, 입력받은 String을 radix 진법으로 해석해서 10진법으로 변환 후 return 해줍니다.

1
2
3
4
5
6
public class test{
    public static void main(String args[]) {
        // 3진법으로 10000 을 10진법으로 반환.
        System.out.println(Integer.parseInt("10000"3));
    }
}
cs

 3진법으로 표현한 "10000" 을 10진법으로 return 해주는 코드로, 결과는 "81" 이 됩니다.

public static Integer ValueOf(String s) throws NumberFormatException

 ValueOf 메소드는 parseInt 메소드와 동일하나, return 해주는 형식이 int가 아닌 Integer 인스턴스로 반환해줍니다.

1
2
3
4
5
6
public class test{
    public static void main(String args[]) {
        // 3진법으로 10000 을 10진법으로 반환.
        System.out.println(Integer.valueOf("500"));
    }
}
cs

public static Integer ValueOf(String s, int radix) throws NumberFormatException

 parseInt 메소드와 마찬가지로 오버로딩 되어있어, 원하는 진법으로 값을 해석해달라고 요청할 수 있습니다.

1
2
3
4
5
6
public class test{
    public static void main(String args[]) {
        // 3진법으로 10000 을 10진법으로 반환.
        System.out.println(Integer.valueOf("500"7));
    }
}
cs

 7진법으로 표현된 "500" 을 return 하므로 결과는 245가 됩니다.

public static int max(int a, int b)

 메소드 이름 그대로 입력받은 2개의 정수 중 더 큰 값을 return 해줍니다.

1
2
3
4
5
public class test{
    public static void main(String args[]) {
        Integer.max(5700);
    }
}
cs

public static int min(int a, int b)

 반대로 더 작은 값을 return 해줍니다.

1
2
3
4
5
public class test{
    public static void main(String args[]) {
        Integer.min(5700);
    }
}
cs

public static int highestOneBit(int i)

 입력받은 정수를 2진수로 표현했을 때, 비트가 1인 가장 큰 값을 return 해줍니다.

1
2
3
4
5
public class test{
    public static void main(String args[]) {
        System.out.println(Integer.highestOneBit(43));
    }
}
cs

 예를 들어, 43은 2진수로 101011(2) 이므로 비트가 1인 가장 큰 값, 10000(2) 인 32를 return 해줍니다.

public static int lowestOneBit(int i)

 입력받은 정수를 2진수로 표현했을 때, 비트가 1인 가장 작은 값을 return 해줍니다.

1
2
3
4
5
public class test{
    public static void main(String args[]) {
        System.out.println(Integer.lowestOneBit(43));
    }
}
cs

 마찬가지로 43은 2진수로 101011(2) 이므로 비트가 1인 가장 작은 값, 1(2) 인 1을 return 해줍니다.

 


Integer 클래스에 있는 메소드 중 자주 사용되는 메소드들을 정리해보았습니다.

이 외에도 다양한 메소드가 존재하니, 꼭 JDK 문서를 참고하시길 바랍니다!

 

+ Recent posts