Does this cause lack of Performance?

I call those methods in every render();

 public void showHealthPotionsQuantity(int healthPotions) {   
        
        slotOne_Health.setText("" + healthPotions);
    }

    public void showManaPotionsQuantity(int manaPotions) {
        slotTwo_Mana.setText("" + manaPotions);
    }

    public void showDiamondsQuantity(int diamondsQuantity) {
        slotThree_Diamonds.setText("" + diamondsQuantity);
    }

    public void showArrowsQuantity(int normalArrows, int heavyArrows) {
        slotFour_ArrowNormal.setText("" + normalArrows);
        slotFive_HeavyArrow.setText("" + heavyArrows);

    }

Should i use StringBuffer/StringBuilder?

I don’t think it cause lack of performance. I use this every time. In this case, creating a stringbuffer every time causes much lag.

Noting that string are immutable (and IIRC the literal is not optimized out in that case) it could if you were working with a tightly packed loop (or one with a lot of iterations), probably not worth considering in most cases though.

Anyways, the problem I have with that code is it doesn’t very well convey the intentions of the code to another person or to the VM which makes it harder to read and harder to optimize. You don’t want to concatenate to a string, you want to convert an integer to a string.

The better way to do this is to use String.valueOf, or Integer.toString(i)

If you are so concerned just use toString then ::slight_smile:

afaik primitives are autoboxed to be able to call toString on them. Jeremys answer is best.

String.valueOf, or Integer.toString(i) are still slow, but acceptable for most PC use.

For high end performance you need something like I a have in my Allbinary Platform, but it cost money (1 dollar) to use each year.

https://github.com/AllBinary/AllBinary-Platform/blob/master/j2me/lib/BasicMathLibraryM/src/main/java/allbinary/logic/math/PrimitiveLongUtil.java

Basically you can’t create strings or stringbuffers in your main game loop everyframe unless you don’t care about performance.

there he is again our old forum lunatic

Only create new Strings when you need to, namely when health and mana values change, store them and use them for rendering.

Use a profiler, don’t optimize blindly.

It is he best practise to use a Stringbuilder with appends, to string, and flushes

however to quote myself

[quote]In most cases it is completely negligible.
[/quote]
Even on android.

I personally may do it properly only for one reason: it floods the memory overview when you profile and it gets annoying ;D

Even on android.

I personally may do it properly only for one reason: it floods the memory overview when you profile and it gets annoying ;D
[/quote]
Thanks for the replies guys!

Just a question then,

String.valueOf
is faster than

“” + 1

?

should be. but more importantly it does not create redundant temporary string objects that cause unwanted garbage collector runs. Also see: http://www.odi.ch/prog/design/newbies.php#3

we are really talking about memory here to really performance, but yeah

“” + 1
is internally changed in to something like

String a = new String("");
String b = String.valueOf(1);
String c = a.concat(b);

so you can see a lot of call and unnecessary String objects for such a simple line D=

I recommend looking at the book “effective java” - just browse through and no shame if you dont get something, but the string are explained there and stuff
I also like it for many pitfalls like “System.out.println(01000);” or something like that, and I learned A LOT of primitive types and auto boxing and the sorts
rounding errors can really hurt you in unexpected ways if you have never heard about these kinds of things

Thanks for that! Already printing it and reading.

Ah, just more one question,

What about this then?

public void showPlayerScore(int score) {
this.score.setText("Score : " + String.valueOf(score));
}

Is that correct?

I love reading books.
But that book is from 2006, java was updated too much since from 2008.
http://www.amazon.com/Effective-Java-Edition-Joshua-Bloch/dp/0321356683/ref=sr_1_1?ie=UTF8&qid=1381589542&sr=8-1&keywords=effective+java

Is there any other book that explains performance/memory coding in java ? But its more actual?

I don’t think performance should be worried about on something this small, but according to the posts above that seems like it will be better then how you had it

dont worry about the age, the 2nd edition still holds up. Joshua Bloch is one of the developers of Java.

This one is good too: Java Puzzlers: Traps, Pitfalls, and Corner Cases

Hm, so its ok if i buy this book?
http://www.amazon.com/Effective-Java-Edition-Joshua-Bloch/dp/0321356683/ref=sr_1_1?ie=UTF8&qid=1381589542&sr=8-1&keywords=effective+java

Im also worried because of Java 8

On desktop it doesn’t matter. On Android it can be a little costly on the GC if you are doing it in many places throughout your game loop. If you are targeting Android and want to do lots of string concats or int-to-strings in your game loop, then use LibGDX’s StringBuffer which uses minimal allocations.

With that said though, it’s best not to optimize blindly.

Where can i learn more about analyzing Netbeans profile and Java Heap and those things? Im checking those things, but sometimes i dont understand whats going on…