ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JAVA] StringBuilder클래스와 Math클래스 확인하기
    JAVA 2022. 8. 8. 11:17
    728x90
    반응형

    1. StringBuilder란?

    StringBuffer는 동기화되어 있으며 멀티 스레드에 안전하지만 싱글 스레드에 사용 시 불필요한 성능 저하를 가져온다.

    싱글 스레드는 StringBuilder를 사용하게 성능면에서 좋다.

    class StringBuilderTest1{
        // StringBuffer로 만들기
        StringBuffer sb1 = new StringBuffer();
        void Append1(){
            sb1.append("abc");
        }
        
        // ⭐StringBuilder로 만들기
        StringBuilder sb2 = new StringBuilder();
        void Append2(){
            sb2.append("abc");
            sb2.insert(2,'.'); // ab.c
            sb2.delete(1,3); // ac
        }
    }

    2. Math 클래스

    • static Type abs(Type Value) : 주어진 값(Value)의 절댓값을 반환한다.
      • Type : double, float, int, long
    class MathClassMethod1{
        int i = Math.abs(-10); // 10
        double d = Math.abs(-10.0); //10.0
    }
    • static double ceil(double Value) : 주어진 값(Value)을 (무조건) 올림 하여 반환한다.
    class MathClassMethod2{
        double d1 = Math.ceil(10.1); // 11
        double d2 = Math.ceil(-10.1); // -10
        double d3 = Math.ceil(10.00000015); // 11
    }
    • static Type max(Type a, Type b) : 주어진 두 값(a, b)을 비교해서 큰 값을 반환한다.
    • static Type min(Type a, Type b) : 주어진 두 값(a, b)을 비교해서 작은 값을 반환한다.
      • Type : double, float, int, long
    class MathClassMethod3{
        double d = Math.max(9.5, 9.500001); // 9.500001
        int i = Math.max(0, -1); // 0
        double d2 = Math.min(9.5, 9.500001); // 9.5
        int i2 = Math.min(0, -1); // -1
    }
    • static double random() : 0.0 <= x <1.0 안에 있는 실수 값을 반환한다.
    class MathClassMethod4{
        double d = Math.random();
        int i = (int)(Math.random()*10)+1; // 1<= i < 11
    }
    • static double rint(double a) : 주어진 값과 가장 가까운 정수 값을 double형으로 반환한다.
      • 단, 두 정수의 가운데 있는 1.5,2.5 등등의 값은 가까운 짝수를 반환한다.
    class MathClassMethod5{
        double d1 = Math.rint(1.2); // 1.0
        double d1 = Math.rint(2.6); // 3.0
        double d1 = Math.rint(3.5); // 4.0
        double d1 = Math.rint(4.6); // 5.0
        double d1 = Math.rint(-4.5); // -4.0
    }
    • static long round(Type a) : 소수점 첫 번째 자리에서 반올림한 정수 값(long)을 반환한다.
    class MathClassMathod6{
        long l1 = Math.round(1.2); // 1
        long l2 = Math.round(2.6); // 3
        long l3 = Math.round(3.5); // 4
        long l4 = Math.round(4.5); // 5
        
        double d1 = 90.7552;
        double d2 = Math.round(d1*100)/100.0 // 90.76
    }
    728x90
    반응형
Designed by Tistory.