JAVA :: 그 외의 다양한 클래스

📝 StringBuffer 클래스

💬 스트링을 다루는 클래스로서, 내부에 가변 크기의 버퍼를 가지고 문자의 개수에 따라 버퍼 크기를 자동 조절한다.

⚠ 스트링의 수정이 가능하다.

👉 예시
StringBuffer sb = new StringBuffer("This"); // 객체생성
sb.append("is pencil."); // sb = "This is pencil."
sb.insert(7, " my"); // sb = "This is my pencil."
sb.replace(8, 10, "your"); // sb = "This is your pencil."
System.out.println(sb); 
👉 결과

This is your pencil.


📝 StringTokenizer 클래스

💬 문자열을 분리하기 위해 사용한다.

💡 구분 문자

💬 문자열을 분리할 때 사용되는 기준 문자

💡 토큰

💬 구분 문자로 분리된 문자열

💡 주요 메소드

👉 예시
import java.util.StringTokenizer;
public class postingex {
    public static void main(String[] args) {
        String company = "Samsung&Apple&Google";
        StringTokenizer st = new StringTokenizer(company, "&");

        int n = st.countTokens(); //분리된 토근의 개수
        System.out.println("분리된 토큰의 개수 = "+n);
        while(st.hasMoreTokens()) // 다음 토근이 있으면 1 없으면 0
        {
            String token = st.nextToken(); // 분리된 토근 얻기
            System.out.println(token);
        }
    }
}
👉 결과
분리된 토큰의 개수 = 3
Samsung
Apple
Google

📝 Math 클래스

'Math.메소드'

💬 기본적인 산술 연산을 수행하는 메소드를 제공하는 클래스이다.

💡 주요 메소드

⚠ 모든 메소드가 static 타입이다.

💬 random()

👉 예시
for(int i = 0; i < 10; i++)
{
    int n = (int)(Math.random()*100 + 1); // 1~100까지의 랜덤 정수
}

Reference

명품 자바 에센셜 (2014, 황기태) / 혼자 공부하는 자바 (2019, 신용권)

좋은 웹페이지 즐겨찾기