总结
在清楚了各个大厂的面试重点之后,就能很好的提高你刷题以及面试准备的效率,接下来小编也为大家准备了最新的互联网大厂资料。
6: checkcast #4 // class Color
9: areturn
static {};
Code:
0: new #4 // class Color
3: dup
4: ldc #7 // String Red
6: iconst_0
7: invokespecial #8 // Method “”:(Ljava/lang/String;I)V
10: putstatic #9 // Field Red:LColor;
13: new #4 // class Color
16: dup
17: ldc #10 // String Blue
19: iconst_1
20: invokespecial #8 // Method “”:(Ljava/lang/String;I)V
23: putstatic #11 // Field Blue:LColor;
26: new #4 // class Color
29: dup
30: ldc #12 // String Green
32: iconst_2
33: invokespecial #8 // Method “”:(Ljava/lang/String;I)V
36: putstatic #13 // Field Green:LColor;
39: iconst_3
40: anewarray #4 // class Color
43: dup
44: iconst_0
45: getstatic #9 // Field Red:LColor;
48: aastore
49: dup
50: iconst_1
51: getstatic #11 // Field Blue:LColor;
54: aastore
55: dup
56: iconst_2
57: getstatic #13 // Field Green:LColor;
60: aastore
61: putstatic #1 // Field $VALUES:[LColor;
64: return
}
还原后的代码如下:
public final class Color extends java.lang.Enum {
//定义的枚举成员
public static final Color Red;
public static final Color Blue;
public static final Color Green;
//编译器自动生成的 javap -c 还查不出来,疑惑
public static final /* synthetic */ Color[] $VALUES;//编译器自动生成的
public static Color[] values(){
/**
-
0: getstatic #1 // Field $VALUES:[LColor;
-
3: invokevirtual #2 // Method “[LColor;”.clone:()Ljava/lang/Object;
-
6: checkcast #3 // class “[LColor;”
-
9: areturn
*/
return $VALUES.clone();
}
public static Color valueOf(java.lang.String s){
/**
-
0: ldc #4 // class Color
-
2: aload_0
-
3: invokestatic #5 // Method java/lang/Enum.valueOf:(Ljava/lang/Class;Ljava/lang/String;)Ljav
-
a/lang/Enum;
-
6: checkcast #4 // class Color
-
9: areturn
*/
return Enum.valueOf(Color.class,s);
}
protected Color(String name, int ordinal) {
super(name, ordinal);
}
static {
/**
-
0: new #4 // class Color
-
3: dup
-
4: ldc #7 // String Red
-
6: iconst_0
-
7: invokespecial #8 // Method “”:(Ljava/lang/String;I)V
-
10: putstatic #9 // Field Red:LColor;
*/
Red=new Color(“Red”,0);
/**
-
13: new #4 // class Color
-
16: dup
-
17: ldc #10 // String Blue
-
19: iconst_1
-
20: invokespecial #8 // Method “”:(Ljava/lang/String;I)V
-
23: putstatic #11 // Field Blue:LColor;
*/
Blue=new Color(“Blue”,1);
/**
-
26: new #4 // class Color
-
29: dup
-
30: ldc #12 // String Green
-
32: iconst_2
-
33: invokespecial #8 // Method “”:(Ljava/lang/String;I)V
-
36: putstatic #13 // Field Green:LColor;
*/
Green=new Color(“Green”,2);
/**
-
39: iconst_3
-
40: anewarray #4 // class Color
-
43: dup
-
44: iconst_0
-
45: getstatic #9 // Field Red:LColor;
-
48: aastore
-
49: dup
-
50: iconst_1
-
51: getstatic #11 // Field Blue:LColor;
-
54: aastore
-
55: dup
-
56: iconst_2
-
57: getstatic #13 // Field Green:LColor;
-
60: aastore
-
61: putstatic #1 // Field $VALUES:[LColor;
*/
$VALUES=new Color[]{Red,Blue,Green};
};
}
通过上面还原后的代码可知,在类的static代码块
中编译器帮我们生成枚举中的每个成员,实际上就是生成对象并赋值给静态变量。
枚举其实就是编译帮我们在静态代码块中创建一个或多个枚举成员对象,如果我们只定义一个枚举成员,这样就是一个单例对象
public enum Singleton {
INSTANCE;
public void doSometing(){
System.out.println(“doing”);
}
}
代码如此简单,不仅如此,这样的方式还可以解决 反射攻击 和 反序列化 导致的单例失败的问题。
在获取实例时进行了枚举判断,如果是枚举类型的则直接抛出异常。
Jdk1.8Class.clss
文件的416..417
行
public T newInstance(Object … initargs)
throws InstantiationException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException{
…
if ((clazz.getModifiers() & Modifier.ENUM) != 0)
throw new IllegalArgumentException(“Cannot reflectively create enum objects”);
…
return inst;
}
最后总结我的面试经验
2021年的金三银四一眨眼就到了,对于很多人来说是跳槽的好机会,大厂面试远没有我们想的那么困难,摆好心态,做好准备,你也可以的。
另外,面试中遇到不会的问题不妨尝试讲讲自己的思路,因为有些问题不是考察我们的编程能力,而是逻辑思维表达能力;最后平时要进行自我分析与评价,做好职业规划,不断摸索,提高自己的编程能力和抽象思维能力。
BAT面试经验
实战系列:Spring全家桶+Redis等
其他相关的电子书:源码+调优
面试真题:
其他相关的电子书:源码+调优
[外链图片转存中…(img-dHPAE1GV-1715587526207)]
面试真题:
[外链图片转存中…(img-dDv8CAUt-1715587526207)]
[外链图片转存中…(img-YBeSbnu4-1715587526208)]