java元素符号是什么,在java中@符号是什么意思?

Java注解用于为变量、方法、类等语言元素添加特殊属性,供程序其他部分检查和使用。例如,@Override确保方法覆盖父类方法,@SuppressWarnings可消除编译警告。自定义注解结合反射,可以实现更复杂的元数据功能。
摘要由CSDN通过智能技术生成

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)

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值