Skip to main content
added 110 characters in body
Source Link
user207421
  • 311.9k
  • 45
  • 324
  • 493

How many objects will be created

At runtime, 4, i.e. the four computed values of str, excluding the initial value which comes from the constant pool.

and how many objects will be available for garbage collection?

At the end of this code but before str goes out of scope, three, i.e. the three intermediate values of str.

Note that I am counting Strings. Each String will have an associated char[] which is another object.

However if the surrounding code is such that the JVM can determine that str cannot change between these lines of code it could be as low as one and zero respectively.

How many objects will be created

At runtime, 4, i.e. the four computed values of str, excluding the initial value which comes from the constant pool.

and how many objects will be available for garbage collection?

At the end of this code but before str goes out of scope, three, i.e. the three intermediate values of str.

However if the surrounding code is such that the JVM can determine that str cannot change between these lines of code it could be as low as one and zero respectively.

How many objects will be created

At runtime, 4, i.e. the four computed values of str, excluding the initial value which comes from the constant pool.

and how many objects will be available for garbage collection?

At the end of this code but before str goes out of scope, three, i.e. the three intermediate values of str.

Note that I am counting Strings. Each String will have an associated char[] which is another object.

However if the surrounding code is such that the JVM can determine that str cannot change between these lines of code it could be as low as one and zero respectively.

Source Link
user207421
  • 311.9k
  • 45
  • 324
  • 493

How many objects will be created

At runtime, 4, i.e. the four computed values of str, excluding the initial value which comes from the constant pool.

and how many objects will be available for garbage collection?

At the end of this code but before str goes out of scope, three, i.e. the three intermediate values of str.

However if the surrounding code is such that the JVM can determine that str cannot change between these lines of code it could be as low as one and zero respectively.