0

Is there a more computationally-efficient way to write a simple constant string than this:

private void parseLabel(FileOutputStream asciiout) {
    asciiout.write(new String("Label: ").getBytes());
}

Seems that the alternative would be to allocate a byte array, put the string in the byte array, and then print the byte array...thought there would be something more direct.

8
  • 1
    You can avoid a string creation: asciiout.write("Label: ".getBytes()); Commented Mar 12, 2014 at 1:01
  • 2
    I have good memories of my 486dx2-66. It had a turbo button and I could download games from a BBS :-) Commented Mar 12, 2014 at 1:01
  • cache those bytes instead of creating String instance everytime Commented Mar 12, 2014 at 1:01
  • 2
    Depends on the use case, and it's also worth questioning whether or not you need this to be more efficient. Premature optimization is evil Commented Mar 12, 2014 at 1:01
  • 1
    All valid comments. @Leo, Yes, 486-DX2-66 was my first computer. Remember lordsoth.happypuppy.org? Commented Mar 12, 2014 at 1:48

1 Answer 1

2
private void parseLabel(FileOutputStream asciiout) {
    asciiout.write("Label: ".getBytes());
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.