I'm a C++ developer moving into Java. Drawing parallels between the two languages, I have some doubts about heap usage in java.
If I don't need to persist a variable beyond the scope of a function in C++, I would just allocate it on the stack, and let the call stack take care of cleaning it up. Thus this code is perfectly legal in C++,
std::unordered_map<std::string, int> map;
In Java though, if I write a similar code,
HashMap<String, Integer> map;
The compiler gives me a warning, that value of map is uninitialized. To suppress the warning, I have to use,
HashMap<String, Integer> map = new HashMap<>();
My questions is, is it a Java convention to new up every object, even if I don't need to persist it beyond the call stack?