Java符号化代码,java中的符号

Java 代码中 @ 符号是什么意思?

annotation。

柯桥ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!

Annotation,是Java5的新特性,下面是Sun的Tutorial的描述,因为是英文,这里我翻译下,希望能够比较清晰的描述一下Annotation的语法以及思想。Annotation:Release 5.0 of the JDK introduced a metadata facility called annotations. Annotations provide data about a program that is not part of the program, such as naming the author of a piece of code or instructing the compiler to suppress specific errors. An annotation has no effect on how the code performs. Annotations use the form @annotation and may be applied to a program's declarations: its classes, fields, methods, and so on. The annotation appears first and often (by convention) on its own line, and may include optional arguments: JDK5引入了Metedata(元数据)很容易的就能够调用Annotations.Annotations提供一些本来不属于程序的数据,比如:一段代码的作者或者告诉编译器禁止一些特殊的错误。An annotation 对代码的执行没有什么影响。Annotations使用@annotation的形势应用于代码:类(class),属性(field),方法(method)等等。一个Annotation出现在上面提到的开始位置,而且一般只有一行,也可以包含有任意的参数。@Author("MyName")class myClass() { }

or @SuppressWarnings("unchecked")void MyMethod() { }

Defining your own annotation is an advanced technique that won't be described here, but there are three built-in annotations that every Java programmer should know: @Deprecated, @Override, and @SuppressWarnings. The following example illustrates all three annotation types, applied to methods:

定义自己的Annotation是一个比较高级的技巧,这里我们不做讨论,这里我们仅仅讨论每一个Java programer都应该知道的内置的annotations:@Deprecated, @Override, and @SuppressWarnings。下面的程序阐述了这三种annotation如何应用于methods。import java.util.List;

class Food {}

class Hay extends Food {}

class Animal {

Food getPreferredFood() {

return null; } /** * @deprecated document why the method was deprecated */

@Deprecated

static void deprecatedMethod() { }

}

class Horse extends Animal {

Horse() {

return;

}

@Override

Hay getPreferredFood() {

return new Hay();

}

@SuppressWarnings("deprecation")

void useDeprecatedMethod() {

Animal.deprecateMethod(); //deprecation warning - suppressed }}

}

}

@DeprecatedThe @Deprecated annotation indicates that the marked method should no longer be used. The compiler generates a warning whenever a program uses a deprecated method, class, or variable. When an element is deprecated, it should be documented using the corresponding @deprecated tag, as shown in the preceding example. Notice that the tag starts with a lowercase "d" and the annotation starts with an uppercase "D". In general, you should avoid using deprecated methods — consult the documentation to see what to use instead.

@Deprecated@Deprecated annotation标注一个method不再被使用。编译器在一个program(程序?)使用了不赞成的方法,类,变量的时候会产生警告(warning)。如果一个元素(element:method, class, or variable)不赞成被使用,应该像前面的例子里使用相应的@deprecated 标签,并且注意标签的首字母是小写的"d",而annotation时大写的"D"。一般情况下,我们应该避免使用不赞成使用的方法(deprecated methods),而应该考虑替代的方法。

@OverrideThe @Override annotation informs the compiler that the element is meant to override an element declared in a superclass. In the preceding example, the override annotation is used to indicate that the getPreferredFood method in the Horse class overrides the same method in the Animal class. If a method marked with @Override fails to override a method in one of its superclasses, the compiler generates an error. While it's not required to use this annotation when overriding a method, it can be useful to call the fact out explicitly, especially when the method returns a subtype of the return type of the overridden method. This practice, called covariant return types, is used in the previous example: Animal.getPreferredFood returns a Food instance. Horse.getPreferredFood (Horse is a subclass of Animal) returns an instance of Hay (a subclass of Food). For more information, see Overriding and Hiding Methods.

@Override@Override annotation 告诉编译器当前元素是重写(override)自父类的一个元素。在前面的例子中,override annotation用来说明Horse类中的getPreferredFood这个方法重写(override)自Animal类中相同的方法。如果一个方法被标注了@Override,但是其父类中没有这个方法时,编译器将会报错。但是并不是说我们一定要使用这个annotation,但是它能够很明显的给出实际行为,尤其是在方法返回一个被重写的方法返回类型的子类型的时候。上面的例子中,Animal.getPreferredFood 返回一个 Food实例,Horse.getPreferredFood 返回一个Hay实例,这里Horse是Animal的子类,Hay是Food的子类。

@SuppressWarningsThe @SuppressWarnings annotation tells the compiler to suppress specific warnings that it would otherwise generate. In the previous example, the useDeprecatedMethod calls a deprecated method of Animal. Normally, the compiler generates a warning but, in this case, it is suppressed. Every compiler warning belongs to a category. The Java Language Specification lists two categories: "deprecation" and "unchecked". The "unchecked" warning can occur when interfacing with legacy code written before the advent of generics. To suppress more than one category of warnings, use the following syntax: @SuppressWarnings@SuppressWarnings annotation 告诉编译器禁止别的元素产生的特殊的警告(warnings),在前面的例子里,useDeprecatedMethod调用了Animal的不赞成使用的一个方法。一般情况下,编译器会给出一个警告(warning),但是在这种情况下,不会产生这个警告,也就是说被suppress。每个编译器的警告都属于一个类型。Java Language Specification列出了两种类型:"deprecation" 和 "unchecked"。"unchecked" warning 发生在使用非generic的旧代码交互的generic collection类时。为了禁止不止一种的警告时,使用下面的语法:@SuppressWarnings({"unchecked", "deprecation"})

java中说找不到符号,代码如下

你需要导入util包,可以直接写import java.util.*,这样就不用每个util下的包都每次导入了

java源代码中各符号意义

深圳远标(ITJOB)帮你:

Java代码规范之一 ——标识符命名规范

转载 2015-08-19 12:06:22

1. 标识符命名规范

1.1 概述

标识符的命名力求做到统一、达意和简洁。

1.1.1

统一

统一是指,对于同一个概念,在程序中用同一种表示方法,比如对于供应商,既可以用supplier,也可以用provider,但是我们只能选定一个使用,至少在一个Java项目中保持统一。统一是作为重要的,如果对同一概念有不同的表示方法,会使代码混乱难以理解。即使不能取得好的名称,但是只要统一,阅读起来也不会太困难,因为阅读者只要理解一次。

1.1.2

达意

达意是指,标识符能准确的表达出它所代表的意义,比如: newSupplier,

OrderPaymentGatewayService等;而 supplier1,

service2,idtts等则不是好的命名方式。准确有两成含义,一是正确,而是丰富。如果给一个代表供应商的变量起名是

order,显然没有正确表达。同样的,supplier1, 远没有targetSupplier意义丰富。

1.1.3

简洁

简洁是指,在统一和达意的前提下,用尽量少的标识符。如果不能达意,宁愿不要简洁。比如:theOrderNameOfTheTargetSupplierWhichIsTransfered 太长,

transferedTargetSupplierOrderName则较好,但是transTgtSplOrdNm就不好了。省略元音的缩写方式不要使用,我们的英语往往还没有好到看得懂奇怪的缩写。

1.1.4

骆驼法则

Java中,除了包名,静态常量等特殊情况,大部分情况下标识符使用骆驼法则,即单词之间不使用特殊符号分割,而是通过首字母大写来分割。比如:

SupplierName, addNewContract,而不是 supplier_name,

add_new_contract。

java 特殊符号输出绝对基础?

稍微深入一点的分析会认为该程序应该打印16,因为两个Unicode转义字符每一个在源文件中都需要用6个字符来表示,但是它们只表示字符串中的一个字符。因此这个字符串应该比它的外表看其来要短10个字符。 如果你运行这个程序,就会发现事情远不是这么回事。它打印的既不是26也不是16,而是2。 理解这个谜题的关键是要知道:Java对在字符串字面常量中的Unicode转义字符没有提供任何特殊处理。编译器在将程序解析成各种符号之前,先将Unicode转义字符转换成为它们所表示的字符[JLS 3.2]。因此,程序中的第一个Unicode转义字符将作为一个单字符字符串字面常量("a")的结束引号,而第二个Unicode转义字符将作为另一个单字符字符串字面常量("b")的开始引号。程序打印的是表达式"a".length()+"b".length(),即2。

JAVA中转义字符:

1.八进制转义序列:\ + 1到3位5数字;范围'\000'~'\377'

\0:空字符

2.Unicode转义字符:\u + 四个十六进制数字;0~65535

\u0000:空字符

3.特殊字符:就3个

\":双引号

\':单引号

\\:反斜线

4.控制字符:5个

\' 单引号字符

\\ 反斜杠字符

\r 回车

\n 换行

\f 走纸换页

\t 横向跳格

\b 退格

点的转义:. == u002E

美元符号的转义:$ == u0024

乘方符号的转义:^ == u005E

左大括号的转义:{ == u007B

左方括号的转义:[ == u005B

左圆括号的转义:( == u0028

竖线的转义:| == u007C

右圆括号的转义:) == u0029

星号的转义:* == u002A

加号的转义:+ == u002B

问号的转义:? == u003F

反斜杠的转义: == u005C


文章名称:Java符号化代码,java中的符号
当前URL:http://csdahua.cn/article/heipip.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流