0

In a java loop like this:

for (File f : files) {
    String s = f.getName();
}

Is it possible to make any claim about the relative locations in memory that will be allotted for String s in each iteration of the loop?

Does the compiler know that we will be allocating space for a String s repeatedly? If yes, does it allocate some fixed space for it or make other relevant optimisations with this knowledge?

Ojasvi Monga
  • 4,437
  • 2
  • 18
  • 35
  • 1
    Well, `String s = ...` is just a reference to the string and the strings themselves probably already have been allocated when creating `files`. – Thomas Oct 23 '19 at 09:15
  • As for the jit compiler: since it is part of the JVM that would probably depend on which JVM you're using (there are multiple different JVM implementations). – Thomas Oct 23 '19 at 09:18
  • You need to differ between memory for the variable and memory for the (String) object. See this question (and the link under the question there) for further information: https://stackoverflow.com/questions/34996026/memory-allocation-of-a-variable-in-java – Tom Oct 23 '19 at 12:35

0 Answers0