2

So say I have an array with columns {item, quantity}. I am getting these values from an sql table, then sorting them by item. I am using a temporary variable "quantity" to manipulate the double value:

while(rs.next()){
   item = rs.getString(1);
   for(int i = 0 ; i < array.size ; i++){
     if(item.equals(array[i][0]){
       double quantity = rs.getDouble(2);
       quantity = quantity + Double.parseDouble(array[i][1]);
       array[i][1] = quantity;
     }
   }
 //add code for adding items if not found in array
}

My question is will using this temporary variable be slower/create garbage while it iterates the array? Does Java properly dispose of this variable at the end of the if statement? I am asking because I am having issues with heap space, and I do not want to have to rewrite everything to accomodate this.

My other option is:

if(item.equals(array[i][0]){
   array[i][1] = String.valueOf(Double.parseDouble(array[i][1]) + rs.getDouble(2));
}

Thanks for reading

trincot
  • 317,000
  • 35
  • 244
  • 286
HagstromV
  • 41
  • 9
  • 2
    Possible duplicate of [Declaring variables inside or outside of a loop](http://stackoverflow.com/questions/8803674/declaring-variables-inside-or-outside-of-a-loop) – acm Oct 21 '16 at 16:00
  • 2
    A *variable* will not create garbage, and will not be slower. It may clarify the code, especially in your case, since the value is otherwise unnamed, so a variable named `quantity` is better is this case. I mean, look at your second example. It is very uninformative about what's going on. A comment might help that, but so does well-named variables. --- *FYI:* Your first example is missing the `String.valueOf()` call. – Andreas Oct 21 '16 at 16:01
  • It may be more precise to say "A _variable that is a primitive_ will not create garbage". A variable that is an object will have its memory allocated on the heap, and will need to be garbage collected. – StvnBrkdll Oct 21 '16 at 16:05
  • @mangotang Incorrect. A reference variable will refer to an object on the heap, but that object does not get *created* by being assigned to the variable. The variable may simply refer to objects already on the heap, so using a variable will not change anything. – Andreas Oct 21 '16 at 16:07
  • @Andreas, I am trying to avoid the second example, as it is messier looking to me; you are correct about the String.valueOf() call in the first example though. – HagstromV Oct 21 '16 at 16:16
  • If @mangotang is correct in that primitives will not create garbage, it looks like my only option is to find a way to cache, or something.; – HagstromV Oct 21 '16 at 16:16
  • @Andreas, so if the variable refers to a pre existing value (in other words), having the `quantity` variable will not change the amount in the heap? – HagstromV Oct 21 '16 at 16:18
  • @Andreas you are correct, I should have looked more closely at the code. In this case the object has already been created on the heap, and placed in the result set. So the temporary variable creates no additional heap allocation. – StvnBrkdll Oct 21 '16 at 16:18
  • @HagstromV Why? Your first example is good. Use it. No cache needed. – Andreas Oct 21 '16 at 16:18
  • Can you show as how _array_ and _array[n]_ are getting allocated? If you are using _new_ to do the allocation, the memory is coming from the heap, and will need to be garbage collected. – StvnBrkdll Oct 21 '16 at 16:23

1 Answers1

0

It will always override quantity with new value, when old value will be keep of course in array. Quantity won't have impact on your memory especially that this is primitive.

If you will deal with complex type and you will do something like that, you will override with every loop the reference to object, so in array you will have all the time reference to same object.

Kamil Banaszczyk
  • 1,133
  • 1
  • 6
  • 23
  • That makes very little sense. The variable named `quantity` is declared *inside* the if-block, so it's value will not be *overridden* during the loop, given that it only exists inside the block. The array value is explicitly being overridden, so saying "old value will be keep" is wrong. – Andreas Oct 21 '16 at 16:06
  • Andreas is correct in that the variable should be destroyed when the if-block is exited. I am wondering if this variable will somehow add garbage. mangotang is saying that a primitive will not create garbage, and only an object will; if this is the case, I will need to rewrite to find a way around my head space issue. – HagstromV Oct 21 '16 at 16:14
  • You are right, i used wrong sentence and it can be confusing. For heap size problem i can add, that primitive variables inside blocks are on stack memory, not in heap, so it certainely will not affect your problem. – Kamil Banaszczyk Oct 21 '16 at 16:19