Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java String Parameters

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);
    }
}
like image 633
tgeros Avatar asked Aug 09 '26 12:08

tgeros


1 Answers

Use a java.lang.StringBuilder - basically a mutable String. However, it would be safer and more "Java style" to return the modified String.

like image 71
Michael Borgwardt Avatar answered Aug 11 '26 03:08

Michael Borgwardt