In this code:
Properties prop = new Properties();
prop.load(new FileInputStream("config.properties"));
Some properties are loaded, but does the fileinputstream need to be closed or does it somehow take care of that itself?
Do I need to create a variable, new file inputstream, then close the variable?
I was also wondering, if I create a variable, say, String a = null and int b;
Do they consume memory when they hold nothing?
and if I have that inside a method or a loop, does it still consume memory when out of scope?
I think someone once said it’s loaded into memory but not ‘active’?
Streams: Yes. Java doesn’t have destructors, so objects can’t take care of their own cleanup. Some amount of cleanup is done at garbage collection time (finalizers), but it’s not good programming practice to rely on that.
One of the reason “finally” blocks exist in Java is to take care of resource deallocation.
Memory allocation: looks like it does’t. I created the following program:
Compiled it, then decompiled using javap -c, and got:
Looks like nothing really happens, except for initializing my main class.
Then i changed the code to say:
compiled, decompiled and got:
You can clearly see the additional instructions in the “main” method, where the memory is allocated.
I have a feeling that different versions the Java compiler might handle this differently.