Your code works fine. There is no problem in returning the strings in this manner.
In Java, a String is a reference to an immutable object which is coupled with garbage collection and takes care of much of the potential complexity by itself without you worrying that string would disappear on you, or that someone somewhere would modify it.
You can make these changes to your code for stylistic reasons:
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 have pushed the variable declarations as later as I could. I have also eliminated ans and have replaced it with the mix of string concatenation and String.format() with a single call to String.format().
Want to learn Java? Check out the Java certification from Intellipaat.