Java 基础汇总(补)
1、Switch语句中表达式类型:
Unlike if-then and if-then-else statements, the switch statement can have a number of possible execution paths. A switch works with the byte,short,char, and int primitive data types. It also works with enumerated types(discussed in Enum Types), the String class, and a few special classes that wrap certain primitive types:Character,Byte,Short, and Integer (discussed in Numbers and Strings).
译自:http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
表达式可以是 byte , short , char 和 int 类型;也可以是枚举类型,String类;
还可以是 byte 、short 、 char 、 int 对应的包装类。
2、格式化输出 函数 format() , println()
示例展示:
3、判别字符类型总结
4、转义字符
\f 换页
5、泛型编程中的注意事项:否则会报编译错误。
if (list instanceof ArrayList) { // compile-time error }
if (list instanceof ArrayList) { // OK; instanceof requires a reifiable type }
Typically, you cannot cast to a parameterized type unless it is parameterized by unbounded wildcards. For example:
List li = new ArrayList<>();
List ln = (List) li; // compile-time error
However, in some cases the compiler knows that a type parameter is always valid and allows the cast. For example:
List l1 = ...;
ArrayList l2 = (ArrayList)l1; // OK
6、注解
(1)JavaSE API中预定义的注解
a. java 语言自己使用的注解:
@Deprecated
被@Deprecated标记过期的接口、类、方法、域 ,应该在Javadoc 注释中用过期注解使用 @deprecated 解释过期的原因及用什么替代了(注意:是小写的 'd')。
@Override
通知编译器,被Mark的类、接口、方法 将会被重写
@SuppressWarnings
@SafeVarargs :
when applied to a method or constructor, asserts that the code does not perform potentially unsafe operations on itsvarargsparameter. When this annotation type is used, unchecked warnings relating tovarargsusage are suppressed.
@FunctionalInterface : JavaSE 8 中引入的,该注解表示 类型声明 是个函数接口(functional interface )
b.用在其他注解上的注解(Annotations that Apply to Other Annotations)
这类注解也称为: meta-annotation,在java.lang.annotation 中,定义了好几个这样的注解:
@Retention: 用于指明被标记的注解的存储方式:
RetentionPolicy.SOURCE– The marked annotation is retained only in the source level and is ignored by the compiler.
源码级存储:在源码级保留,但是会被编译器忽略。
RetentionPolicy.CLASS– The marked annotation is retained by the compiler at compile time, but is ignored by the Java Virtual Machine (JVM).
类级存储:编译时会存储,但是会被JVM忽略。
RetentionPolicy.RUNTIME– The marked annotation is retained by the JVM so it can be used by the runtime environment.
运行时存储:会被JVM存储,将来在运行时会被用到。
@Documented
indicates that whenever the specified annotation is used those elements should be documented using the Javadoc tool. (By default, annotations are not included in Javadoc.)
表示被标记的元素会被用于Javadoc工具 。默认情况,注解是不会被Javadoc的。
@Target
用于标记限制其他注解该应用于什么样的Java element
ElementType.ANNOTATION_TYPEcan be applied to an annotation type.
ElementType.CONSTRUCTORcan be applied to a constructor.
ElementType.FIELDcan be applied to a field or property.
ElementType.LOCAL_VARIABLEcan be applied to a local variable.
ElementType.METHODcan be applied to a method-level annotation.
ElementType.PACKAGEcan be applied to a package declaration.
ElementType.PARAMETERcan be applied to the parameters of a method.
ElementType.TYPEcan be applied to any element of a class.
@Inherited:继承
用于指明该注解可以继承超类的注解(默认是不可以的,为false)。当用户查询注解类型时,若这个类没有定义这个类型的注解,那么这个类的超类会被查询。这个注解仅适用于类声明(class declaration)。
@Repeatable
Java SE 8 引入的,用于指明被标记的注解可以多次应用于同一个声明或同一种类型。
can be applied to the same declaration or type use.
(2)Type Annotations and Pluggable Type Systems
Before the Java SE 8 release, annotations could only be applied to declarations. As of the Java SE 8 release, annotations can also be applied to any type use. This means that annotations can be used anywhere you use a type.
这样的插件式注解可以自己实现也可以只用第三方已经实现的框架,推荐一个Checker Framework