Java 基础汇总(补)

1、Switch语句中表达式类型:

2363252-afc5ce826172743f.png

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()


2363252-873ea4eee7db5d4b.png

示例展示:

2363252-894150b47cb865da.png

3、判别字符类型总结

2363252-601ceaf2bcbe8eb5.png

4、转义字符

2363252-5b3c9b9e45fa48c1.png

\f 换页

5、泛型编程中的注意事项:否则会报编译错误。

2363252-5581ae727f003fa8.png

     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')。

2363252-8e84a7047faad1c9.png

        @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.

 参见Repeating Annotation 。

(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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值