ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [In Java] Difference between 'String', 'StringBuffer' and 'StringBuilder'
    Java 2016. 8. 18. 15:35

    [In Java] Difference between 'String', 'StringBuffer' and 'StringBuilder'


    This all paragragh is quoted from blog 'http://ooz.co.kr/298'.




    First, We get to know the difference between String and ('StringBuffer' and 'StringBuilder').


    String is immutable, but ('StringBuffer' and 'StringBuilder') are mutable.

    Once we make instance with String, that memory is never changed. When we use '+' operator, JVM makes new instance, not modify existing memory.


    So, If we use '+' operator frequently, it occurs many garbage memory. 

    But, because of this feature, String is Thread-Safe.


    On the other hand, 'StringBuffer' and 'StringBuilder' are mutable.

    'StringBuffer' and 'StringBuilder' can change their memory.

    If there are not enough memory to save something, they can increase memory. 


    Second, What is the difference  'StringBuffer' and 'StringBuilder'?


    The difference is 'Synchronized'.


    StringBuffer has a Synchronized keyword in each method. So it can be Thread-Safe in Multi-Thread environment.




    ps. After JDK 1.5, When we use String, JVM compile String like StringBuilder. So there is no difference after JDK 1.5.



    Thanks.

    Rio

    반응형

    'Java' 카테고리의 다른 글

    [In Java] Difference between 'FileReader' and 'BufferedReader'  (0) 2016.08.30
Designed by Tistory.