in java what does the @ symbol mean?

2019-03-11 19:15发布

I know what it means in a comment for documentation purposes, but outside of that what does it mean? (I would normally just google this but every non letter symbol shows up in results)

标签: java symbols
6条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-03-11 19:34

The @ symbol denotes Annotations. They provide information about a class, its field or method (above which they appear). They cannot perform operations. The compilers or special annotation processors use this information to make writing code less verbose.

In Java Persistence API you use them to map a Java class with database tables.

For example @Table() Used to map the particular Java class to the date base table.

@Entity Represents that the class is an entity class.

Similarly you can use many annotations to map individual columns, generate ids, generate version, relationships etc.

查看更多
Anthone
3楼-- · 2019-03-11 19:39

As some other suggests, it is Java's annotation. It helps the compiler to validate your code and to notify the programmer as well.

Very simple code example:

public class SomeClass {

    @Override
    public String toString() {
        return "SomeClass";
    }

    @Deprecated
    public void doSomeOperation() {
        // some operation...
    }
}

The annotation from SomeClass#toString which is @Override helps the compiler to determine that it is an overridden function from the implicit inheritance to the class Object.

While the annotation from SomeClass#doSomeOperation will warn the programmer that the function itself is deprecated already and should be avoided to use.

查看更多
\"骚年 ilove
4楼-- · 2019-03-11 19:42

The @ symbol is used for annotations. In my experience, the most common annotation is @Override, which indicates that a method is declared in a superclass. Other common annotations are @Deprecated, indicating that a method should no longer be used but still exists for backwards compatibility, and @SupressWarnings, to prevent warnings from showing up in the compiler.

Note that it's actually possible to get annotations which are not included in the core Java libraries and to declare your own annotations.

查看更多
三岁会撩人
5楼-- · 2019-03-11 19:47

The annotations are for the reader or compiler, not executable code.

查看更多
劳资没心,怎么记你
6楼-- · 2019-03-11 19:48

The @ symbol denotes a Java Annotation. What a Java annotation does, is that it adds a special attribute to the variable, method, class, interface, or other language elements. (This can be configured when you declare the annotation) When you add an annotation to something, other parts of the program can check whether something has an annotation or not. It then can use this information to do whatever stuff they need.

Let me give you some examples:

The @Override annotation:

public class SuperClass {
    public void someMethod () {
        System.out.println ("Superclass!");
    }
}

public class DerivedClass extends SuperClass {
    @Override
    public void someMethod () {
        System.out.println ("Derived class!");
    }
}

And when you do this:

SuperClass sc = new DerivedClass ();
sc.someMethod ();

It will execute the someMethod in SuperClass, right? No. It will print "Derived class!". This is because in the derived class, there is this @Override thingy. So the derived class overrides the super class's someMethod.

The @SuppressWarnings Annotation:

Here is a method:

public void someMethod () {
    int i;
}

There will be a compiler warning saying that i is never used. So you can add the @SuppressWarnings to the method to suppress the warning:

@SuppressWarnings ("unused")
public void someMethod () {
    int i;
}

Note that there is a parameter to the @SuppressWarnings annotation. Some annotations have parameters and you can look for the them in the javadoc. But for those that don't have parameters you don't need to add () like a method.

You can also declare your own annotations and use reflection to check for them. The above 2 annotations will be checked by the compiler.

查看更多
Anthone
7楼-- · 2019-03-11 19:51

The @ sign is used to specify Java Annotation.

https://en.wikipedia.org/wiki/Java_annotation

There are built-in Java Annotation and user defined Custom Annotation.

Annotations are used in various ways, such as suppress warning, associate method to URI (Servlet), associate variables to resource (JNDI) etc

查看更多
登录 后发表回答