I’m coming from a .net background and want to know the accepted way of creating a method that returns a boolean and modifies a string that was passed in via parameter. I understand Strings are immutable in Java so the below snippet will always produce an empty string. I am constrained to return boolean only. Exceptions can’t be thrown. If I need to wrap the String class in, say, a StringHolder, what package could I find this in.
public static void DoStuff() { String msg = ''; if (GetMessage(msg)) { System.out.println(msg); } }
Use a java.lang.StringBuilder – basically a mutable String. However, it would be safer and more ‘Java style’ to return the modified String.