1

In my program I have a long if condition which looks something like this:

if((items.size() > 0 && !k.getText().equals(last)) || cr.isLast() == true)

Now I thought it is easier to read if I use a variable for the first statement so I changed my code into this:

boolean textChanged = items.size() > 0 && !k.getText().equals(last);
if(fachChanged == true || cr.isLast() == true)

Now my question is: Does the second code need more memory because I used a temporary variable or is this optimized from the compiler? I think today it is not so important if one boolean less or more is stored in the memory but there is the wish to create an optimized and memory friendly program.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Cilenco
  • 6,951
  • 17
  • 72
  • 152
  • 2
    Your code will probably be optimized by the compiler, but you shouldn't prematurely optimize - focus on readability. Also `if(fachChanged || cr.isLast())` no need to explicitly test for equality to true... `true == true`. – Elliott Frisch Feb 21 '14 at 18:30
  • The amount of storage and number of instructions used in both cases will most likely be identical. `textChanged` will never actually be "stored in memory". – Hot Licks Feb 21 '14 at 18:36

4 Answers4

4

"The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet." — Michael A. Jackson

The compiler will optimize what can be optimized, you should care about the more difficult task: try to write clean and readable code.

exception1
  • 1,239
  • 8
  • 17
1
if((items.size() > 0 && !k.getText().equals(last)) || cr.isLast() == true)

This is resolved to a boolean value by a compiler, and that boolean has to go somewhere. Once the if statement is finished, the boolean is disposed of. If you create a local variable, that boolean is maintained in memory for its life time (here it looks like up until the end of the method).

That said, the compiler may notice this, and provided that boolean isn't used anywhere else, it will probably evaluate to your first example anyway. Either way, this is quite a stringent optimisation, and something that Java can definitely handle either way.

christopher
  • 26,815
  • 5
  • 55
  • 89
1

Use your extracted solution - it is more readable, you can debug it properly and the compiler optimizes it anyway. Also, only locally scoped variables (and boolean primitives above all) wont get stored on the heap and are therefore quickly disposed anyway.

Smutje
  • 17,733
  • 4
  • 24
  • 41
1

I 100% agree with all the other answers so far, but I'm also compelled to actually answer the question.

my question is: Does the second code need more memory because I used a temporary variable

Probably not. It's most likely that in both cases, the compiler will put the boolean in a register and it will never hit memory at all. It depends on what other code is happening around the code you've provided.

If you reference that variable later on, you might run out of registers and it would end up on the stack. In either case it would never go in the heap, so your memory profile will be identical either way.

Ted Bigham
  • 4,237
  • 1
  • 26
  • 31