Java – how do I clear strings?

In the program I am writing, the text field must display some text at some time

output.setText( outputString );
outputString = "";

Output is a jtextfield These lines of code are in a method, and when it is called for the first time, it is completely normal However, when it is called by another time, the original outputstring text still exists Why does this happen and how can I solve it?

Well, I think this is because strings are immutable The problem is that outputstring never changes, so it still has the text of the initial method call

How can I change the text in a string in some way?

Solution

The content of setting text as a variable does not establish a permanent relationship between the variable and the text field. If you want to clear the text, you can use

output.setText("");
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>