5,336
edits
(Created page with "Java is a popular object-oriented programming language.") |
No edit summary |
||
Line 1: | Line 1: | ||
Java is a popular object-oriented programming language. | Java is a popular object-oriented programming language. | ||
==Usage== | |||
==Features== | |||
===AutoCloseable=== | |||
[https://stackoverflow.com/questions/28329419/when-does-a-local-string-scanner-get-garbage-collected StackOverflow Reference]<br> | |||
[https://docs.oracle.com/javase/8/docs/api/java/lang/AutoCloseable.html AutoCloseable Java 8]<br> | |||
Since Java does not have destructors and thus no RAII objects, you will need to close anything you open manually.<br> | |||
One way to automate this is to use autocloseable classes introduced in Java 7.<br> | |||
These classes, which implement that AutoCloseable interface, can be wrapped in a try block | |||
This is similar to <code>using</code> in Python and C#.<br> | |||
<syntaxhighlight lang="java"> | |||
try (Scanner scanner = new Scanner(s)) { | |||
while (scanner.hasNext()) { | |||
// do something | |||
} | |||
} // close will be implicitly called | |||
</syntaxhighlight> |