I am writing a static utility method. I know methods isEmpty() and isNew() are threadsafe. In the getTotal(...) method I am using StringBuilder as parameter along with String and int. StringBuilder is mutable. Is getTotal() threadsafe? If so explain why it is even though StringBuilder is mutable. I am not sure if getCharge() is thread safe because it's invoking the method getTotal(). Can someone tell if it is threadsafe or not?
public class NewStringUtils { public static boolean isEmpty(String s){ return (s == null || s.trim().length()==0); } public static boolean isNew(String code){ return( "1009".equals(code) || "1008".equals(code) ); } //StringBuilder is mutable -- is the below method threadsafe public static int getTotal(StringBuilder sb,String oldCode ,int a, int b){ //Is it Threadsafe or not .If so just bcz every thread invoking this method will have its own copy and other threads can't see its value ?? int k =0; if("1011".equals(oldCode) && "1021".equals(sb.toString()) { k = a+b; } return k; } // is the below method threadsafe public static int getCharge(String code,String oldCode,int charge1,int charge2){ int total =0; StringBuilder sb = new StringBuilder("1021"); if(!NewStringUtils.isEmpty(code)){ if(NewStringUtils.isNew(code)){ //here invoking a static method which has StringBuilder(Mutable) as a parameter total = NewStringUtils.getTotal(sb,oldCode,charge1,charge2); } } return total; } }