0

I have a JAVA Program which is to convert EPOCH timestamp to Date, we have two proposed solutions. One is only 3 lines worth of code, the other about 10 lines. Both produce the correct result and the number of operations are the same except for the following:

Code 1:

 Date d = new Date;
 d = setTime(arg1*1000);

Code 2:

 Date d = new Date;
 long timestamp = arg1 * 1000;
 d = setTime(timestamp);

Is there a difference in here? Probably in efficiency? Or memory usage? I'm not entirely sure if Code 2 uses more memory since it declares one more variable, 'timestamp'. Input needed. Thanks!

3
  • 6
    There is, in theory a difference, but I believe most modern Java compilers will optimize out the second case such that the byte code for both cases will appear the same. I actually prefer the second case because it makes your code more readable. But that's just me and I'm old. Commented Jul 27, 2015 at 2:05
  • 1
    I'm old too, but I prefer option 1. In option 2 I'll always be waiting to see where timestamp is used somewhere else in the method. On the other hand, option 2 makes it easier to debug since I can look at or print the variable. So I vote "whatever". Commented Jul 27, 2015 at 2:08
  • 1
    I'm old and don't care which. The second one is longer, but if the code it's in is so long you can't tell where else timestamp is used it's a different issue--code's too long. Intermediate variables are a common technique, but in this case, it's not overly helpful. Of course, neither is a variable named arg1 :( Commented Jul 27, 2015 at 2:11

4 Answers 4

1

if timestamp variable is not used anywhere else then both aprroaches are equally optimal.As the calculation is not too big to make it unreadable and java has to compute it once only.

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

Comments

0

JVM Optimization

The comments and answers are correct, no need to worry about such efficiencies because the JVM compilers and runtimes are so amazingly good at optimizing. Keeping your code simple gives the most opportunities for such optimizations. In other words, write dumb code for the smart JVM.

Creating intermediate variables with wisely chosen names is generally encouraged:

  • Makes your code more self-documenting.
  • The modern JVMs are adept at efficiently handling and disposing of any such short-lived objects. And the compiler may have optimized away the extra variable anyways and inlined the value.
  • Most importantly, debugging is so much easier. When tracing thorough code you can see each incremental change/operation as it occurs. The named variables make for a catalog of values for you to review in the debugger tool.

Readable Code

So focus on readability, making your code easy for humans to understand. To that end, I would:

  • Change the order of your statements. Generally better to prepare your data before using it, so don't begin instantiating the Date object until your milliseconds have been calculated.
  • Take advantage of a java.util.Date constructor to remove a line of code.
  • Change the variable names to be self-documenting.
  • Insert underscore in numeric literal to make number more readable.
  • Append an uppercase L to numeric literal. While appending may not be technically necessary in this particular case as the compiler can upsize the int to a long, the L reinforces to the reader that we are working to create a long instead of the more usual int.

Example code

long millisecondsSinceEpoch = ( secondsSinceEpoch * 1_000L ) ;
Date date = new Date( millisecondsSinceEpoch ) ;

java.time

Even better is to entirely avoid the old java.util.Date/.Calendar classes as they are notoriously troublesome. In Java 8 and later those classes have been supplanted by the new java.time package.

Instant instant = Instant.ofEpochSecond( secondsSinceEpoch ) ;

Comments

0

There is no significant difference, just which style you prefer. I typically see the second style used if the value is used more than once and the first style used otherwise. It comes down to readability and how easy it is to change the code later.

Comments

0

I have a rule I follow. If the piece of code can throw an exception, then I put it on its own line. I hate troubleshooting a problem where the stacktrace points to a line that have more than one potential source of the Java exception. In your example, the multiplication would not throw an exception, hence according to my rule, can go on the same line as the setTime() method call.

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.