我有一个不适合屏幕宽度的长字符串。例如。
String longString = "This string is very long. It does not fit the width of the screen. So you have to scroll horizontally to read the whole string. This is very inconvenient indeed.";
为了便于阅读,我想到了这样写——
String longString = "This string is very long." +
"It does not fit the width of the screen." +
"So you have to scroll horizontally" +
"to read the whole string." +
"This is very inconvenient indeed.";
但是,我意识到第二种方法使用字符串连接,并将在内存中创建 5 个新字符串,这可能会导致性能下降。是这样吗?或者编译器是否足够聪明,可以找出我真正需要的只是一个字符串?我怎么能避免这样做呢?