Java Annotations

前端 未结 8 809
天命终不由人
天命终不由人 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 05:56

    When do you use Java's @Override annotation and why? The link refers to a question on when one should use the override annotation(@override).. This might help understand the concept of annotation better.Check out.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题