2

I might be going the long way around and hopefully I can explain this without code as at the minute the code is all over the place.

What I want is a string

static String locationOneStr = new String ("res/.png");
static StringBuilder locationOneStrBuilder = new StringBuilder(locationOneStr);

That contains a base form of the URL where I want to call to later.

The code that comes later finds the number residing at position 0 or 'locationOne' and a switch choses which code to utilize: if it is a 0 it inserts 0 before the .png if its a 1 it inserts a 1 etc.

public static StringBuilder locationOneNumber(StringBuilder forCharConv,  StringBuilder locationOneStrBuilder) {
    char localChar = forCharConv.charAt(0);
    switch (localChar) {
        case '0':
            BpmCalcFrame.locationOneStrBuilder.insert(3,"0");
            System.out.println("Zero");
            break;

        /*
        *
        * Other Cases Omitted 
        */

        default:
            System.out.println("There is no valid input!");
            break;
        }
    return;

}

The problem I have is that a switch has to return something. However the jframe I am using as a test environment is expecting a String for the URL.

JLabel locationOne = new JLabel("Image 1");
    locationOne.setBackground(Color.WHITE);
    locationOne.setIcon(new ImageIcon(ArrayComparison.locationOneNumber(forCharConv, locationOneStrBuilder)));
    locationOne.setBounds(172, 45, 36, 68);
    contentPane.add(locationOne);

I am using StringBuilder as Strings eat up memory, but if I have to convert the StringBuilder to a String it defeats the point of using the StringBuilder

Am I going about this the wrong way or do I really have to convert back to a String? This would mean I will be making on average 2 new objects a second for the entire of the program's lifecycle.

Is there a way of making an instance of the StringBuilder as a String without creating a new object?

11
  • 2 new objects per second? If you created 1E7 or 1E9 (or more) objects per second you might think about optimizing, but 2? Commented Sep 25, 2013 at 20:04
  • 1
    If it bothers you, I just measured the time a call to StringBuilder.toString takes to be around 14 nano-seconds on my machine! Commented Sep 25, 2013 at 20:09
  • I hate to tell you, but updating the screen is 1000x times more expensive than creating a String object. I suggest you profile your application and work on the things which are slowing down your application rather than worry about each line of code. Commented Sep 25, 2013 at 20:10
  • @StackOverflowException looks like you're overdue for an upgrade :) Commented Sep 25, 2013 at 20:14
  • 1
    If you want to avoid creating String objects, I would not copy the string literal, it just wastes code and time. Commented Sep 25, 2013 at 20:14

3 Answers 3

5

Worrying about two new objects a second is premature optimisation.

Your JVM will likely spend less that a tenth of a millisecond doing that.

Write code that is clear and obvious first, and optimise for performance later.

Sign up to request clarification or add additional context in comments.

3 Comments

OK I'm fairly new to java and the app involves you clicking a button ans every time you click it will generate 4 or five strings to graphically represent numbers. Every string will be allocated memory so it means my app will just be creating garbage all the time. Its not a problem and I'm not running into errors I just wanted to know if there was a way around creating all the garbage.
@gcoulby: Garbage is much cheaper than you seem to assume. (And the amount of garbage generated by four or five modestly sized strings, multiplied by the fastest rate any human could click a mouse, is tiny.)
@LouisWasserman This is exactly my problem. I'm just learning about Java and in doing so, I am always hearing about people saying you can't manually manage memory creating new objects creates new memory allocations etc etc. So I was concerned. However, I think for the size of my application I have nothing to worry about. My objects are created on user input where as the problems I am anticipating would be if such objects were created in computer controlled loops, and many of them could cause a problem in time. Garbage is hard to understand. does eclipse have any tools to monitor the garbage?
2

You're supposed to turn the StringBuilder into a String when you're done building the string. That's exactly how it's supposed to be used.

Comments

1

How about:

static String locationOneStrTemplate = "res{number}.png";

//...

public static StringBuilder locationOneNumber(StringBuilder forCharConv,  String locationOneStrTemplate) {
    //...
    String actualURL = locationOneStrTemplate.replace("{number}", forCharConv);
    //...
}

2 Comments

Why are you copying a String literal with new String( ?
Just copy pasted from the author's, code - nevetheless, you are right, I'll edit.

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.