Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does java have an equivalent to the C# "using" clause

Tags:

I've seen reference in some C# posted questions to a "using" clause. Does java have the equivalent?

like image 930
dacracot Avatar asked Sep 26 '08 18:09

dacracot


People also ask

Is Java similar to C?

C is a middle-level language as it binds the bridges between machine-level and high-level languages. Java is a high-level language as the translation of Java code takes place into machine language, using a compiler or interpreter. C is only compiled and not interpreted. Java is both compiled and interpreted.

Is Java similar to C or C++?

These two languages are very similar in terms of syntax and language features. They are so similar that if you're shown some portion of C++ code from a project and asked whether it's C++ or Java code, you may confuse yourself.

How does Java differ from C?

Java is a high level language and is more data oriented also known globally as Object-Oriented language. On other hand C is a middle-level language and is more procedure-oriented also known globally as Procedural Programming Language.

Should I use C or Java?

C is a procedural, low level, and compiled language. Java is an object-oriented, high level, and interpreted language. Java uses objects, while C uses functions. Java is easier to learn and use because it's high level, while C can do more and perform faster because it's closer to machine code.


3 Answers

Yes. Java 1.7 introduced the try-with-resources construct allowing you to write:

try(InputStream is1 = new FileInputStream("/tmp/foo");
    InputStream is2 =  new FileInputStream("/tmp/bar")) {
         /* do stuff with is1 and is2 */
}

... just like a using statement.

Unfortunately, before Java 1.7, Java programmers were forced to use try{ ... } finally { ... }. In Java 1.6:

InputStream is1 = new FileInputStream("/tmp/foo");
try{

    InputStream is2 =  new FileInputStream("/tmp/bar");
    try{
         /* do stuff with is1 and is 2 */

    } finally {
        is2.close();
    }
} finally {
    is1.close();
}
like image 181
Aaron Maenpaa Avatar answered Sep 26 '22 14:09

Aaron Maenpaa


Yes, since Java 7 you can rewrite:

InputStream is1 = new FileInputStream("/tmp/foo");
try{

    InputStream is2 =  new FileInputStream("/tmp/bar");
    try{
         /* do stuff with is1 and is2 */

    } finally {
        is2.close();
    }
} finally {
    is1.close();
}

As

try(InputStream is1 = new FileInputStream("/tmp/foo");
    InputStream is2 =  new FileInputStream("/tmp/bar")) {
         /* do stuff with is1 and is2 */
}

The objects passed as parameters to the try statement should implement java.lang.AutoCloseable.Have a look at the official docs.

For older versions of Java checkout this answer and this answer.

like image 21
Lodewijk Bogaards Avatar answered Sep 25 '22 14:09

Lodewijk Bogaards


The nearest equivalent within the language is to use try-finally.

using (InputStream in as FileInputStream("myfile")) {
    ... use in ...
}

becomes

final InputStream in = FileInputStream("myfile");
try {
    ... use in ...
} finally {
    in.close();
}

Note the general form is always:

acquire;
try {
    use;
} finally {
    release;
}

If acquisition is within the try block, you will release in the case that the acquisition fails. In some cases you might be able to hack around with unnecessary code (typically testing for null in the above example), but in the case of, say, ReentrantLock bad things will happen.

If you're doing the same thing often, you can use the "execute around" idiom. Unfortunately Java's syntax is verbose, so there is a lot of bolier plate.

fileInput("myfile", new FileInput<Void>() {
   public Void read(InputStream in) throws IOException {
       ... use in ...
       return null;
   }
});

where

public static <T> T fileInput(FileInput<T> handler) throws IOException {
    final InputStream in = FileInputStream("myfile");
    try {
        handler.read(in);
    } finally {
        in.close();
    }
}

More complicated example my, for instance, wrap exceptions.

like image 22
Tom Hawtin - tackline Avatar answered Sep 24 '22 14:09

Tom Hawtin - tackline