Java has no lifetime for an object, this is managed by the garbage collector. And if I use some IO classes without closing it, or some DBConnection
And if I use some IO classes without closing it, or some DBConnection, will
this considered a resource leak?
Saying using same IO classes is wrong terminology. If you create a resource/connection and then use it's reference for some other resource without actually closing the original resource then if there are no active references to original resource then it will be eligible for GC.
However till the time that original resource is GCed all the OS resources (file handles etc) will not be freed. When the object will be subjected to GC(finalize() method is called on that object/resource) close() is called first due to which OS related resources are freed and then the heap memory.
For example consider FileInputStream finalize() method is as follows
protected void finalize() throws IOException {
if ((fd != null) && (fd != FileDescriptor.in)) {
/*
* Finalizer should not release the FileDescriptor if another
* stream is still using it. If the user directly invokes
* close() then the FileDescriptor is also released.
*/
runningFinalize.set(Boolean.TRUE);
try {
close();
} finally {
runningFinalize.set(Boolean.FALSE);
}
}
}
You see close() is called first.
So though GC takes care of memory management for you it is a good programming practice to close the resource in the finally statement when you don't need it.