views:

158

answers:

5

do they by annotation mean a comment in a code with // or /* */?

+8  A: 

No, an annotation is not a comment. An annotation is added to a field, class or method, using the syntax @Annotation. One of the best known annotations is @Override, used to signal a method is overriding one from a super class. For example:

public class MyClass {
  @Override
  public boolean equals(Object other) {
    //...
  }
}

See http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html for more info.

Jorn
+3  A: 

No.

An annotation is a special construct introduced with java 1.5. An annotation adds some meta information to a java class, method or variable. This meta information can be evaluated at compile time (e.g. for generating some extra code with apt) or at runtime (e.g. to match a class to a database table).

Example for a built in annotation:

@Deprecated // this is an annotation
public void myMethod() {
    ...
}
tangens
+3  A: 

No, annotations take the form:

@Annotation(property="A")
public class {
   @Annotation(property="B")
   Object field;

   @Annotation(property="C")
   public void method() {
   }
}

Annotations can be placed on classes, methods or fields. They can provide information at runtime via reflection or compile time via apt (short for Annotation Processing Tool and not the apt package manager).

They are defined as:

@interface Annotation {
    String property();
}

See http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html for more

cyborg
the article is very confusing...instead of describing what it is for it delves into all these syntaxes without really explaining what you do and what it´s good for...have to find another good article with a reference to all annotations and when and why to use them. so you define one annotation in one source file (.java)? and then you use it in your classes?
weng
See the JUnit framework for an example of an API using annotations vs one that is not in the same package.
cyborg
+1  A: 

Actually, before Java5 (i.e. 1.3 or 1.4), comments (// or /* */) were the only way to add annotation (i.e. "metadata") to be acted upon.

One classic example is the way the unit-testing framework TestNg propose all its Java5 @Annotations as comments if you are using TestNg with Java 1.4.
But that means, for Testng to launch the proper test suite, it had to access the sources of your program, not just the compiled binary.

Unlike Javadoc tags, Java annotations can be reflective in that they can be embedded in class files generated by the compiler and may be retained by the Java VM to be made retrievable at run-time.

VonC
A: 

Annotations are not just for java they also exist in c++, they are somehow similar with those from java.

// MyCode.h
# include <CodeAnalysis/SourceAnnotations.h>
using namespace vc_attributes;
class CMyClass
{
public:
       void f ( [Pre ( Valid = Yes )] int *pWidth );
// code ...
};

// MyCode.cpp
#include "MyCode.h"
void CMyClass::f ( [Pre (Valid = Yes)] int pWidth )
{
}

You can check the MSDN for more information: http://msdn.microsoft.com/en-us/library/ms182036(VS.80).aspx

Adrian Faciu