From 0b9fc91cb8d47578baf23a6702410f3e18c2a7af Mon Sep 17 00:00:00 2001 From: RyanSmith <875354601@qq.com> Date: Sat, 25 Jun 2016 18:47:59 +0800 Subject: [PATCH] =?UTF-8?q?[=E6=B7=BB=E5=8A=A0]=E8=AF=AD=E6=B3=95=E5=9F=BA?= =?UTF-8?q?=E7=A1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../EnumMap.md" | 53 +++++++++++++++++ .../Java\345\274\202\345\270\270.md" | 4 ++ .../forEach.md" | 26 +++++++++ .../switch.md" | 57 +++++++++++++++++++ .../try{} catch(){} finally{}.md" | 14 +++++ 5 files changed, 154 insertions(+) create mode 100644 "main/JAVA/\350\257\255\346\263\225\345\237\272\347\241\200\344\270\216\347\273\206\350\212\202/EnumMap.md" create mode 100644 "main/JAVA/\350\257\255\346\263\225\345\237\272\347\241\200\344\270\216\347\273\206\350\212\202/Java\345\274\202\345\270\270.md" create mode 100644 "main/JAVA/\350\257\255\346\263\225\345\237\272\347\241\200\344\270\216\347\273\206\350\212\202/forEach.md" create mode 100644 "main/JAVA/\350\257\255\346\263\225\345\237\272\347\241\200\344\270\216\347\273\206\350\212\202/switch.md" create mode 100644 "main/JAVA/\350\257\255\346\263\225\345\237\272\347\241\200\344\270\216\347\273\206\350\212\202/try{} catch(){} finally{}.md" diff --git "a/main/JAVA/\350\257\255\346\263\225\345\237\272\347\241\200\344\270\216\347\273\206\350\212\202/EnumMap.md" "b/main/JAVA/\350\257\255\346\263\225\345\237\272\347\241\200\344\270\216\347\273\206\350\212\202/EnumMap.md" new file mode 100644 index 0000000..03e8dd5 --- /dev/null +++ "b/main/JAVA/\350\257\255\346\263\225\345\237\272\347\241\200\344\270\216\347\273\206\350\212\202/EnumMap.md" @@ -0,0 +1,53 @@ +# EnumMap + +**一种键值属于枚举类型的映射表。** + + +使用场景:要传给前端一些有限情况的数据,且这个数据是抽象,不容易理解的,如:返回错误号:004。这个时候如果再传一个错误码的解释会好很多。如:004,数据不存在。 + +下面代码,004和名字反过来了,懒得改,凑活看吧 + +```java + public enum ErrMsgEnum { + required_item_id("商品id为必填项"), + invalid_app_id("应用标识错误"), + invalid_date("时间格式错误"); + + private String value; + + private ErrMsgEnum(String value) { + this.setValue(value); + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + } +``` +EnumMap +```java +import java.util.EnumMap; +import java.util.Map; + + public class TestEnumMap { + public static EnumMap errMsgMap = new EnumMap(ErrMsgEnum.class); + static{ + errMsgMap.put(ErrMsgEnum.required_item_id, "004"); + errMsgMap.put(ErrMsgEnum.invalid_app_id, "005"); + errMsgMap.put(ErrMsgEnum.invalid_date, "008"); + } + } +``` + +注意事项 + +> 1. 使用EnumMap时,必须指定枚举类型。All of the keys in an enum map must come from a single enum type that is specified, explicitly or implicitly, when the map is created. + +> 2. key不能为null Null keys are not permitted + +> 3. EnumMap内部以数组实现,性能更好。Enum maps are represented internally as arrays. This representation is extremely compact and efficient. \ No newline at end of file diff --git "a/main/JAVA/\350\257\255\346\263\225\345\237\272\347\241\200\344\270\216\347\273\206\350\212\202/Java\345\274\202\345\270\270.md" "b/main/JAVA/\350\257\255\346\263\225\345\237\272\347\241\200\344\270\216\347\273\206\350\212\202/Java\345\274\202\345\270\270.md" new file mode 100644 index 0000000..33823aa --- /dev/null +++ "b/main/JAVA/\350\257\255\346\263\225\345\237\272\347\241\200\344\270\216\347\273\206\350\212\202/Java\345\274\202\345\270\270.md" @@ -0,0 +1,4 @@ +### 参考文献: +- [Java异常处理和设计](http://www.cnblogs.com/dolphin0520/p/3769804.html) +- [Java EE项目中异常设计及处理总结 ](http://blog.csdn.net/luqin1988/article/details/7970782) +- [Java异常机制及异常处理分析 ](http://blog.csdn.net/chenxiang0207/article/details/8201654) \ No newline at end of file diff --git "a/main/JAVA/\350\257\255\346\263\225\345\237\272\347\241\200\344\270\216\347\273\206\350\212\202/forEach.md" "b/main/JAVA/\350\257\255\346\263\225\345\237\272\347\241\200\344\270\216\347\273\206\350\212\202/forEach.md" new file mode 100644 index 0000000..11fc63c --- /dev/null +++ "b/main/JAVA/\350\257\255\346\263\225\345\237\272\347\241\200\344\270\216\347\273\206\350\212\202/forEach.md" @@ -0,0 +1,26 @@ + +在平时Java程序中,应用比较多的就是对Collection集合类的foreach遍历,foreach之所以能工作,是因为这些集合类都实现了Iterablei接口,该接口中定义了Iterator迭代器的 + +产生方法,并且foreach就是通过Iterable接口在序列中进行移动。 +```java +//代码片段 + for(String s : strings){ + System.out.println(s); + } + + Iterator iterator = strings.iterator(); + while(iterator.hasNext()){ + String s = iterator.next(); + System.out.println(s); + } +``` + +两个效果一样。foreach是语法糖,编译器会自动优化 + + +效率: +与for相比,基本相同,除了遍历linkLsit时,for会慢很多,原因不作赘述。 + +一般遍历,建议直接使用foreach,不会有边界问题,代码也简洁很多 + + diff --git "a/main/JAVA/\350\257\255\346\263\225\345\237\272\347\241\200\344\270\216\347\273\206\350\212\202/switch.md" "b/main/JAVA/\350\257\255\346\263\225\345\237\272\347\241\200\344\270\216\347\273\206\350\212\202/switch.md" new file mode 100644 index 0000000..f0f2a7d --- /dev/null +++ "b/main/JAVA/\350\257\255\346\263\225\345\237\272\347\241\200\344\270\216\347\273\206\350\212\202/switch.md" @@ -0,0 +1,57 @@ +switch表达式中使用字符串。 +```java +/** * Java Program to demonstrate how string in switch functionality is implemented in * Java SE 7 release. */ +public class StringInSwitchCase { + public static void main(String[] args) { + String mode = args[0]; + switch (mode) { + case "ACTIVE": + System.out.println("Application is running on Active mode"); + break; + case "PASSIVE": + System.out.println("Application is running on Passive mode"); + break; + case "SAFE": + System.out.println("Application is running on Safe mode"); + } + } +} +``` +[7款开源Java反编译工具](http://www.admin10000.com/document/5064.html) + +反编译后的代码: + +``` +/** * Reverse Engineered code to show how String in Switch works in Java. */ +import java.io.PrintStream; + +public class StringInSwitchCase{ + public StringInSwitchCase() { } + + public static void main(string args[]) { + String mode = args[0]; + String s; switch ((s = mode).hashCode()) { + default: break; + case -74056953: + if (s.equals("PASSIVE")) { + System.out.println("Application is running on Passive mode"); + } + break; + case 2537357: + if (s.equals("SAFE")) { + System.out.println("Application is running on Safe mode"); + } + break; + case 1925346054: + if (s.equals("ACTIVE")) { + System.out.println("Application is running on Active mode"); + } + break; + } + } +} +``` + +看到这个代码,你知道原来字符串的switch是通过equals和hashCode()方法来实现的。记住,switch中只能使用整型,比如byte。short,char以及int。还好hashCode()方法返回的是int,而不是long。通过这个很容易记住hashCode返回的是int这个事实。 + + 仔细看下可以发现,进行switch的实际是哈希值,然后通过使用equals方法比较进行安全检查,这个检查是必要的,因为哈希可能会发生碰撞。因此它的性能是不如使用枚举进行switch或者使用纯整数常量,但这也不是很差。因为Java编译器只增加了一个equals方法,如果你比较的是字符串字面量的话会非常快,比如”abc” ==”abc”。如果你把hashCode()方法的调用也考虑进来了,那么还会再多一次的调用开销,因为字符串一旦创建了,它就会把哈希值缓存起来,这个可以参考 [为什么Java中的字符串是不可变的](http://blog.csdn.net/renfufei/article/details/16808775)。 \ No newline at end of file diff --git "a/main/JAVA/\350\257\255\346\263\225\345\237\272\347\241\200\344\270\216\347\273\206\350\212\202/try{} catch(){} finally{}.md" "b/main/JAVA/\350\257\255\346\263\225\345\237\272\347\241\200\344\270\216\347\273\206\350\212\202/try{} catch(){} finally{}.md" new file mode 100644 index 0000000..6195972 --- /dev/null +++ "b/main/JAVA/\350\257\255\346\263\225\345\237\272\347\241\200\344\270\216\347\273\206\350\212\202/try{} catch(){} finally{}.md" @@ -0,0 +1,14 @@ + +### 参考连接: +[关于 Java 中 finally 语句块的深度辨析](https://www.ibm.com/developerworks/cn/java/j-lo-finally/) + +#### 为什么要用finally + +finally不管有没有异常一定会被执行,所以finally可以写一些一定要执行的代码。例如:释放资源,断开io连接等等。 +不用finally会导致,代码多个地方写多个相同的代码。使代码不易维护,也不过美观。 + +#### 为什么要用finally +[关于 Java 中 finally 语句块的深度辨析](https://www.ibm.com/developerworks/cn/java/j-lo-finally/) + +精华总结:(详细参考上面链接) +> Java 虚拟机会把 finally 语句块作为 subroutine(对于这个 subroutine 不知该如何翻译为好,干脆就不翻译了,免得产生歧义和误解。)直接插入到 try 语句块或者 catch 语句块的控制转移语句(**return**)之前。但是,还有另外一个不可忽视的因素,那就是在执行 subroutine(也就是 finally 语句块)之前,try 或者 catch 语句块会保留其返回值到本地变量表(Local Variable Table)中。待 subroutine 执行完毕之后,再恢复保留的返回值到操作数栈中,然后通过 return 或者 throw 语句将其返回给该方法的调用者(invoker) \ No newline at end of file -- Gitee