Concatenating strings without highlighting in java

Is there a way to combine two lines (not final) without allocating memory?

For example, I have two lines:

final String SCORE_TEXT = "SCORE: "; String score = "1000"; //or int score = 1000; 

When I combine these two lines, a new String object is created.

 font.drawMultiLine(batch, SCORE_TEXT + score, 50f, 670f);//this creates new string each time 

Since this is done in the main game loop (performed ~ 60 times in one second), there are many distributions.

Is there any way to do this without highlighting?

+6
source share
5 answers

The obvious solution is not to recreate the String output for each frame, but only when it changes.

One way to do this is to save it somewhere outside the main loop and update it when a certain event occurs, that is, the “score” will actually change. In your main loop, you simply use the pre-created String .

If you cannot / do not want to have this event-based approach, you can always save the “previous” score and only concatenate a new line when the previous result is different from the current score.

Depending on how often your score actually changes, this should cut out most redistributions. Unless, of course, the estimate does not change at a speed of 60 frames per second, and in this case the whole point does not work completely, because no one will be able to read the text that you are printing.

+4
source

I did not understand this question for the first time. Have you tried using the following?

 SCORE_TEXT.concat(score); 
+1
source

It seems that drawMultiLine does not accept String , but CharSequence . That way, you can probably implement your own CharSequence , which does not actually concatenate the two lines. Here's the implementation project:

 public class ConcatenatedString implements CharSequence { final String left, right; final int leftLength; public ConcatenatedString(String left, String right) { this.left = left; this.right = right; this.leftLength = left.length(); } @Override public int length() { return leftLength+right.length(); } @Override public char charAt(int index) { return index < leftLength ? left.charAt(index) : right.charAt(index-leftLength); } @Override public CharSequence subSequence(int start, int end) { if(end <= leftLength) return left.substring(start, end); if(start >= leftLength) return right.substring(start-leftLength, end-leftLength); return toString().substring(start, end); } @Override public String toString() { return left.concat(right); } } 

Use it as follows:

 font.drawMultiLine(batch, new ConcatenatedString(SCORE_TEXT, score), 50f, 670f); 

Internally, in your case, drawMultiLine only needs the length and charAt . Using ConcatenatedString , you create only one new object. Unlike when you use SCORE_TEXT + score , you create a temporary StringBuilder that creates an internal char[] array, copies the input characters, StringBuilder array if necessary, and then creates a final String object that creates a new char[] array and copies characters again. Thus, it is likely that ConcatenatedString will be faster.

+1
source

I don’t think you can fill the value without allocating memory for it .. what you can do is create a global string variable and give it a SCORE_TEXT + value. Use this global string variable in the font.drawMultiLine () method.

This way you can minimize the amount of memeory allocated since memory is allocated only once, and the same location is updated again and again.

0
source

The string is intended to be immutable in Java. use StringBuilder

0
source

Source: https://habr.com/ru/post/988084/


All Articles