The question is slightly stupid, just interesting, it is clear that no one does this, but still - will it eat resources, etc?

For example, there is such a method

public String getName(){ return name; } 

And I call it somewhere like this, without saving to a variable

 getName(); 

It should be so.

 String s = getName(); 

: D Yes, it turned out very silly, but still. What happens to a value if it hasn't gone anywhere?

  • It disappears as after the function call the stack is released, and if there was no assignment, the memory used in the call goes straight to the garbage collector (or where else in Java at the discretion of the collector) - Daniel Protopopov
  • name is a field or what? - Artem Konovalov

1 answer 1

In this case, in the memory area called "heap" (where all objects are created in general), an object of type String will simply be created. But there is no link to it; accordingly, we will never be able to use this object. Therefore, the garbage collector at the first need simply removes this object from memory.