Java Annotations

前端 未结 8 1101
天命终不由人
天命终不由人 2020-12-02 05:32

What is the purpose of annotations in Java? I have this fuzzy idea of them as somewhere in between a comment and actual code. Do they affect the program at run time?

8条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-02 06:06

    Anders gives a good summary, and here's an example of a JUnit annotation

    @Test(expected=IOException.class)
    public void flatfileMissing() throws IOException {
        readFlatFile("testfiles"+separator+"flatfile_doesnotexist.dat");
    }
    

    Here the @Test annotation is telling JUnit that the flatfileMissing method is a test that should be executed and that the expected result is a thrown IOException. Thus, when you run your tests, this method will be called and the test will pass or fail based on whether an IOException is thrown.

提交回复
热议问题