7

I wrote the following function to convert a time in milliseconds to a string of the format mins:seconds. Being a former C programmer I assumed that "ans" would have to be static in order to work properly, but putting static before String appears to not be allowed.

My question is - will this function work - and if not, what change will make it work.

public String time_to_string(long t) // time in milliseconds
{
    String ans;
    int mins,secs;
    if (t < 0)
    {
        return "-";
    }
    else
    {
        secs = (int)(t/1000);
        mins = secs/60;
        secs = secs - (mins * 60);

        ans = ""+mins+":"+String.format("%02d", secs);

        return ans;
    }
}
7
  • 1
    Can'y you tell if it works? Just try it. Commented Jan 25, 2012 at 16:08
  • You may want to consider changing the name of your method. Java method names are almost always in camel case e.g. timeToString(...). It's by no means enforced, but naming standards are very standardised in Java world. Commented Jan 25, 2012 at 16:08
  • returned 22125128:29 using System.currentTimeMillis() Commented Jan 25, 2012 at 16:10
  • @skaffman: I'm chasing a bug at the moment and this going wrong was a potential cause. I needed to check it was kosher. Commented Jan 25, 2012 at 16:11
  • @Mick: So write a unit test for it. Commented Jan 25, 2012 at 16:11

2 Answers 2

11

Your code is fine. There's no problem with returning Strings in this manner.

In Java, a String is a reference to an immutable object. This, coupled with garbage collection, takes care of much of the potential complexity: you can simply pass a String around without worrying that it would disapper on you, or that someone somewhere would modify it.

If you don't mind me making a couple of stylistic suggestions, I'd modify the code like so:

public String time_to_string(long t) // time in milliseconds
{
    if (t < 0)
    {
        return "-";
    }
    else
    {
        int secs = (int)(t/1000);
        int mins = secs/60;
        secs = secs - (mins * 60);
        return String.format("%d:%02d", mins, secs);
    }
}

As you can see, I've pushed the variable declarations as far down as I could (this is the preferred style in C++ and Java). I've also eliminated ans and have replaced the mix of string concatenation and String.format() with a single call to String.format().

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

Comments

3

In Java, a String is a reference to heap-allocated storage. Returning "ans" only returns the reference so there is no need for stack-allocated storage. In fact, there is no way in Java to allocate objects in stack storage.

I would change to this, though. You don't need "ans" at all.

return String.format("%d:%d", mins, secs);

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.