G
Guest
This might sound stupid, but I really need to know why we need another
language with garbage collector?
With C++ destructor, everything get destroyed without to worry about them.
And because memory is not the only system resources, the concept of
destructor fits well into object oriented programming practise.
For instance, if Java with reference counting and without GC exists:
// With destructor and reference counting
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.query("SELECT * FROM table");
// Do something
} catch (Exception e) {
// handle exception
// rs and stmt went out of scope and destroyed
}
// With garbage collector
Statement stmt = null;
ResultSet rs = null;
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.query("SELECT * FROM table");
// Do something
} catch (Exception e) {
// handle exception
} finally {
rs.close(); // What if createStatement() throws an exception and rs is
null???
stmt.close(); // Can you be sure that stmt is not null???
}
As you can see, GC only free programmers from handling memory but force
programmers to worry about other resources like file, database connection,
socket connection, etc. Why don't they implement a reference counting
language instead? We don't need another language like Java.
language with garbage collector?
With C++ destructor, everything get destroyed without to worry about them.
And because memory is not the only system resources, the concept of
destructor fits well into object oriented programming practise.
For instance, if Java with reference counting and without GC exists:
// With destructor and reference counting
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.query("SELECT * FROM table");
// Do something
} catch (Exception e) {
// handle exception
// rs and stmt went out of scope and destroyed
}
// With garbage collector
Statement stmt = null;
ResultSet rs = null;
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.query("SELECT * FROM table");
// Do something
} catch (Exception e) {
// handle exception
} finally {
rs.close(); // What if createStatement() throws an exception and rs is
null???
stmt.close(); // Can you be sure that stmt is not null???
}
As you can see, GC only free programmers from handling memory but force
programmers to worry about other resources like file, database connection,
socket connection, etc. Why don't they implement a reference counting
language instead? We don't need another language like Java.