共计 6013 个字符,预计需要花费 16 分钟才能阅读完成。
主动拆装箱
对于根本类型和包装类型之间的转换,通过 xxxValue()和 valueOf()两个办法实现主动拆装箱,应用 jad 进行反编译能够看到该过程:
public class Demo {public static void main(String[] args) {int x = new Integer(10); // 主动拆箱 | |
Integer y = x; // 主动装箱 | |
} | |
} |
反编译后后果:
public class Demo | |
{public Demo(){} | |
public static void main(String args[]) | |
{int i = (new Integer(10)).intValue(); // intValue()拆箱 | |
Integer integer = Integer.valueOf(i); // valueOf()装箱} | |
} |
foreach 语法糖
在遍历迭代时能够 foreach 语法糖,对于数组类型间接转换成 for 循环:
// 原始代码 | |
int[] arr = {1, 2, 3, 4, 5}; | |
for(int item: arr) {System.out.println(item); | |
} | |
} | |
// 反编译后代码 | |
int ai[] = {1, 2, 3, 4, 5}; | |
int ai1[] = ai; | |
int i = ai1.length; | |
// 转换成 for 循环 | |
for(int j = 0; j < i; j++) | |
{int k = ai1[j]; | |
System.out.println(k); | |
} |
对于容器类的遍历会应用 iterator 进行迭代:
import java.io.PrintStream; | |
import java.util.*; | |
public class Demo | |
{public Demo() {} | |
public static void main(String args[]) | |
{ArrayList arraylist = new ArrayList(); | |
arraylist.add(Integer.valueOf(1)); | |
arraylist.add(Integer.valueOf(2)); | |
arraylist.add(Integer.valueOf(3)); | |
Integer integer; | |
// 应用的 for 循环 +Iterator,相似于链表迭代:// for (ListNode cur = head; cur != null; System.out.println(cur.val)){ | |
// cur = cur.next; | |
// } | |
for(Iterator iterator = arraylist.iterator(); iterator.hasNext(); System.out.println(integer)) | |
integer = (Integer)iterator.next();} | |
} |
Arrays.asList(T…)
相熟 Arrays.asList(T…)用法的小伙伴都应该晓得,asList()办法传入的参数不能是根本类型的数组,必须包装成包装类型再应用,否则对应生成的列表的大小永远是 1:
import java.util.*; | |
public class Demo {public static void main(String[] args) {int[] arr1 = {1, 2, 3}; | |
Integer[] arr2 = {1, 2, 3}; | |
List lists1 = Arrays.asList(arr1); | |
List lists2 = Arrays.asList(arr2); | |
System.out.println(lists1.size()); // 1 | |
System.out.println(lists2.size()); // 3 | |
} | |
} |
从反编译后果来解释,为什么传入根本类型的数组后,返回的 List 大小是 1:
// 反编译后文件 | |
import java.io.PrintStream; | |
import java.util.Arrays; | |
import java.util.List; | |
public class Demo | |
{public Demo() {} | |
public static void main(String args[]) | |
{int ai[] = {1, 2, 3}; | |
// 应用包装类型,全副元素由 int 包装为 Integer | |
Integer ainteger[] = {Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3) | |
}; | |
// 留神这里被反编译成二维数组,而且是一个 1 行三列的二维数组 | |
// list.size()当然返回 1 | |
List list = Arrays.asList(new int[][] { ai}); | |
List list1 = Arrays.asList(ainteger); | |
System.out.println(list.size()); | |
System.out.println(list1.size()); | |
} | |
} |
从下面后果能够看到,传入根本类型的数组后,会被转换成一个二维数组,而且是 new int1 这样的数组,调用 list.size()当然返回 1。
注解
Java 中的类、接口、枚举、注解都能够看做是类类型。应用 jad 来看一下 @interface 被转换成什么:
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
@Retention(RetentionPolicy.RUNTIME) | |
public @interface Foo{String[] value(); | |
boolean bar();} |
查看反编译代码能够看出:
- 自定义的注解类 Foo 被转换成接口 Foo,并且继承 Annotation 接口
- 原来自定义接口中的 value()和 bar()被转换成形象办法
import java.lang.annotation.Annotation; | |
public interface Foo | |
extends Annotation | |
{public abstract String[] value(); | |
public abstract boolean bar();} |
注解通常和反射配合应用,而且既然自定义的注解最终被转换成接口,注解中的属性被转换成接口中的形象办法,那么通过反射之后拿到接口实例,在通过接口实例天然可能调用对应的形象办法:
import java.util.Arrays; | |
@Foo(value={"sherman", "decompiler"}, bar=true) | |
public class Demo{public static void main(String[] args) {Foo foo = Demo.class.getAnnotation(Foo.class); | |
System.out.println(Arrays.toString(foo.value())); // [sherman, decompiler] | |
System.out.println(foo.bar()); // true | |
} | |
} |
枚举
通过 jad 反编译能够很好地了解枚举类。
空枚举
先定义一个空的枚举类:
public enum DummyEnum {}
应用 jad 反编译查看后果:
- 自定义枚举类被转换成 final 类,并且继承 Enum
- 提供了两个参数(name,odinal)的公有结构器,并且调用了父类的结构器。留神即便没有提供任何参数,也会有该该结构器,其中 name 就是枚举实例的名称,odinal 是枚举实例的索引号
- 初始化了一个 private static final 自定义类型的空数组 $VALUES
-
提供了两个 public static 办法:
- values()办法通过 clone()办法返回外部 $VALUES 的浅拷贝。这个办法联合公有结构器能够完满实现单例模式,想一想 values()办法是不是和单例模式中 getInstance()办法性能相似
- valueOf(String s):调用父类 Enum 的 valueOf 办法并强转返回
public final class DummyEnum extends Enum | |
{// 性能和单例模式的 getInstance()办法雷同 | |
public static DummyEnum[] values() | |
{return (DummyEnum[])$VALUES.clone();} | |
// 调用父类的 valueOf 办法,并墙砖返回 | |
public static DummyEnum valueOf(String s) | |
{return (DummyEnum)Enum.valueOf(DummyEnum, s); | |
} | |
// 默认提供一个公有的公有两个参数的结构器,并调用父类 Enum 的结构器 | |
private DummyEnum(String s, int i) | |
{super(s, i); | |
} | |
// 初始化一个 private static final 的本类空数组 | |
private static final DummyEnum $VALUES[] = new DummyEnum[0]; | |
} |
蕴含形象办法的枚举
枚举类中也能够蕴含形象办法,然而必须定义枚举实例并且立刻重写形象办法,就像上面这样:
public enum DummyEnum { | |
DUMMY1 {public void dummyMethod() {System.out.println("[1]: implements abstract method in enum class"); | |
} | |
}, | |
DUMMY2 {public void dummyMethod() {System.out.println("[2]: implements abstract method in enum class"); | |
} | |
}; | |
abstract void dummyMethod();} |
再来反编译看看有哪些变动:
- 原来 final class 变成了 abstract class:这很好了解,有形象办法的类天然是抽象类
- 多了两个 public static final 的成员 DUMMY1、DUMMY2,这两个实例的初始化过程被放到了 static 代码块中,并且实例过程中间接重写了形象办法,相似于匿名外部类的模式。
- 数组 $VALUES[] 初始化时放入枚举实例
还有其它变动么?
在反编译后的 DummyEnum 类中,是存在形象办法的,而枚举实例在动态代码块中初始化过程中重写了形象办法。在 Java 中,形象办法和形象办法重写同时放在一个类中,只能通过外部类模式实现。因而下面第二点应该说成就是以内部类模式初始化。
能够看一下 DummyEnum.class 寄存的地位,应该多了两个文件:
- DummyEnum$1.class
- DummyEnum$2.class
Java 中.class 文件呈现 $ 符号示意有外部类存在,就像 OutClass$InnerClass,这两个文件呈现也应证了下面的匿名外部类初始化的说法。
import java.io.PrintStream; | |
public abstract class DummyEnum extends Enum | |
{public static DummyEnum[] values() | |
{return (DummyEnum[])$VALUES.clone();} | |
public static DummyEnum valueOf(String s) | |
{return (DummyEnum)Enum.valueOf(DummyEnum, s); | |
} | |
private DummyEnum(String s, int i) | |
{super(s, i); | |
} | |
// 形象办法 | |
abstract void dummyMethod(); | |
// 两个 pubic static final 实例 | |
public static final DummyEnum DUMMY1; | |
public static final DummyEnum DUMMY2; | |
private static final DummyEnum $VALUES[]; | |
// static 代码块进行初始化 | |
static | |
{DUMMY1 = new DummyEnum("DUMMY1", 0) {public void dummyMethod() | |
{System.out.println("[1]: implements abstract method in enum class"); | |
} | |
} | |
; | |
DUMMY2 = new DummyEnum("DUMMY2", 1) {public void dummyMethod() | |
{System.out.println("[2]: implements abstract method in enum class"); | |
} | |
} | |
; | |
// 对本类数组进行初始化 | |
$VALUES = (new DummyEnum[] {DUMMY1, DUMMY2}); | |
} | |
} |
失常的枚举类
理论开发中,枚举类通常的模式是有两个参数(int code,Sring msg)的结构器,能够作为状态码进行返回。Enum 类实际上也是提供了蕴含两个参数且是 protected 的结构器,这里为了防止歧义,将枚举类的结构器设置为三个,应用 jad 反编译:
最大的变动是:当初的 private 结构器从 2 个参数变成 5 个,而且在外部依然将前两个参数通过 super 传递给父类,残余的三个参数才是真正本人提供的参数。能够设想,如果自定义的枚举类只提供了一个参数,最终生成底层代码中 private 结构器应该有三个参数,前两个仍然通过 super 传递给父类。
public final class CustomEnum extends Enum | |
{public static CustomEnum[] values() | |
{return (CustomEnum[])$VALUES.clone();} | |
public static CustomEnum valueOf(String s) | |
{return (CustomEnum)Enum.valueOf(CustomEnum, s); | |
} | |
private CustomEnum(String s, int i, int j, String s1, Object obj) | |
{super(s, i); | |
code = j; | |
msg = s1; | |
data = obj; | |
} | |
public static final CustomEnum FIRST; | |
public static final CustomEnum SECOND; | |
public static final CustomEnum THIRD; | |
private int code; | |
private String msg; | |
private Object data; | |
private static final CustomEnum $VALUES[]; | |
static | |
{FIRST = new CustomEnum("FIRST", 0, 10010, "first", Long.valueOf(100L)); | |
SECOND = new CustomEnum("SECOND", 1, 10020, "second", "Foo"); | |
THIRD = new CustomEnum("THIRD", 2, 10030, "third", new Object()); | |
$VALUES = (new CustomEnum[] {FIRST, SECOND, THIRD}); | |
} | |
} |