Is there a way to pass a String to a method as mutable?

I know that by default String variables are immutable in Java. But is there a way to set a String variable, or some special way (when passing a specified String variable) to make it mutable?

Or at least temporarily allow Stirng to change?

I understand that I can change the value of the String simply by assigning the changed immutable variable to the original variable, like show:

String A = "Test";
A.toUpperCase(); //will make A upper case, but not save the new upper case string to a variable
Sring A = A.toUpperCase; //will give me TEST and save it to A, replacing A

      

Is there a way to tell Java "Hey, for this one line specifically, treat it as mutable"?

+3


source to share


6 answers


Not. Strings are immutable.



You should either create your own mutable structure that wraps a string or char array if you need to, or use something like StringBuilder

this if it suits your needs.

+8


source


No, the class String

is immutable in java. To do this, you can create a class:

public class MutableString {

    private String str;

    public MutableString(String str) {
        this.str = str;
    }

    public void toUpperCase() {
        str = str.toUpperCase();
    }

    public String toString() {
        return str;
    }

}

      



The feasibility of this technique depends on the number of techniques you need from String

.

+4


source


String variables are mutable in Java. String objects are not modified.

However, method arguments are passed by value, not by reference. Therefore, you cannot return a value for a String via a reference parameter.

For example, you can do this because a

is a mutable variable, pointing first to one immutable String object, then to another.

 String a = "Test";
 a = a.toUpperCase(); 

      

And you can do this:

  public void myfunction( String a ) {
     a = a.toUppercase
  }

      

But this caller will not observe his own variable change because the actual and formal arguments are two different variables.

  public void mycaller() {
      String actual = "test";
      myfunction( actual );
      System.out.println( actual ); // Prints "test"
  }

      

What can you do:

  • Returns a string from the method.
  • Returns from a result object method that includes a string.
  • Create your own class MyMutableString

    that contains a mutable string reference.
  • Pass an array String[]

    with one element.

If you have multiple return values, consider the second option - return an object that encapsulates the results of the method. If you have one value to return, just return it. Other options are less useful.

+4


source


No, there is no way to do String

mutable in Java. The class does not expose its internal character array and does not offer methods that can modify it. If you want to change lines, you have several options:

  • StringBuilder

    and its synchronized version StringBuffer

    are essentially mutable strings, albeit a little more cumbersome in code.
  • char

    arrays are mutable - you can use toCharArray()

    to get a mutable copy of the internal char String array.
  • Create your own class with an internal char array and methods that offer the manipulation functions you need and toString()

    that will allow you to use it as you plan.
+2


source


The short answer is no , you cannot mutate objects java.lang.String

.

To make Java more memory efficient , the JVM allocates a special area of ​​memory called the "string constant pool". When the compiler encounters a string literal, it checks the pool to see if an identical string already exists.

If a match is found, the new literal reference is directed to the existing String, and no new string literal is created. (The existing string just has an additional reference.) Now we can begin to understand why making String objects immutable is such a good idea. If several reference variables refer to the same String without even knowing it, it would be very bad if either of them could change the String value.

You could say, "Okay, that's okay and okay, but what if someone overrides the Functionality of the String class; might this cause problems in the pool?" the main reasons why the String class is marked final.

No one can override the behavior of any of the String methods, so you can be sure that the String objects that you expect to be immutable will in fact be immutable.

Help: SCJP Sun Certified Programmer for Java 6 - Study Guide Exam (310-065) - Cathy Sierra and Bert Bates

+1


source


You can replace the link (!) With a string as often as you like:

String A = "Test";
A = A.toUpperCase(); //will make A upper case
A = A.substring( 1 ); 

      

etc. Overusing this may not be the best approach - it depends on the context.

Since some people seem to think that this cannot be done with a String going through a method:

String modify( String x ){
    x = x.toLowerCasae();
    x = x.substring( 1 );
    return x;
}

      

and the call should, of course, replace the original String reference.

String tomod = "abc";
tomod = modify( tomod );

      

Three are, of course, classes more appropriate for this type of String modification.

0


source







All Articles