2019年10月11日 | Leave a comment https://mp.weixin.qq.com/s/O3vj-Pcy6eCtw6p4dgcNdw 让代码性能更高 需要 Map 的主键和取值时,应该迭代 entrySet() 当循环中只需要 Map 的主键时,迭代 keySet() 是正确的。但是,当需要主键和取值时,迭代 entrySet() 才是更高效的做法,比先迭代 keySet() 后再去 get 取值性能更佳。 反例: <span class="code-snippet_outer"><span class="code-snippet__built_in">Map</span><<span class="code-snippet__built_in">String</span>, <span class="code-snippet__built_in">String</span>> map = ...;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">for</span> (<span class="code-snippet__built_in">String</span> key : map.keySet()) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__built_in">String</span> value = map.get(key);</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer">}</span> 1 <span class="code-snippet_outer"><span class="code-snippet__built_in">Map</span><<span class="code-snippet__built_in">String</span>, <span class="code-snippet__built_in">String</span>> map = ...;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">for</span> (<span class="code-snippet__built_in">String</span> key : map.keySet()) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__built_in">String</span> value = map.get(key);</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer">}</span> 正例: <span class="code-snippet_outer"><span class="code-snippet__built_in">Map</span><<span class="code-snippet__built_in">String</span>, <span class="code-snippet__built_in">String</span>> map = ...;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">for</span> (<span class="code-snippet__built_in">Map</span>.Entry<<span class="code-snippet__built_in">String</span>, <span class="code-snippet__built_in">String</span>> entry : map.entrySet()) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__built_in">String</span> key = entry.getKey();</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__built_in">String</span> value = entry.getValue();</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer">}</span> 1 <span class="code-snippet_outer"><span class="code-snippet__built_in">Map</span><<span class="code-snippet__built_in">String</span>, <span class="code-snippet__built_in">String</span>> map = ...;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">for</span> (<span class="code-snippet__built_in">Map</span>.Entry<<span class="code-snippet__built_in">String</span>, <span class="code-snippet__built_in">String</span>> entry : map.entrySet()) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__built_in">String</span> key = entry.getKey();</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__built_in">String</span> value = entry.getValue();</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer">}</span> 应该使用Collection.isEmpty()检测空 使用 Collection.size() 来检测空逻辑上没有问题,但是使用 Collection.isEmpty()使得代码更易读,并且可以获得更好的性能。任何 Collection.isEmpty() 实现的时间复杂度都是 O(1) ,但是某些 Collection.size() 实现的时间复杂度可能是 O(n) 。 反例: <span class="code-snippet_outer"><span class="code-snippet__keyword">if</span> (collection.size() == <span class="code-snippet__number">0</span>) {</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer">}</span> 1 <span class="code-snippet_outer"><span class="code-snippet__keyword">if</span> (collection.size() == <span class="code-snippet__number">0</span>) {</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer">}</span> 正例: <span class="code-snippet_outer"><span class="code-snippet__keyword">if</span> (collection.isEmpty()) {</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer">}</span> 1 <span class="code-snippet_outer"><span class="code-snippet__keyword">if</span> (collection.isEmpty()) {</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer">}</span> 如果需要还需要检测 null ,可采用CollectionUtils.isEmpty(collection)和CollectionUtils.isNotEmpty(collection)。 不要把集合对象传给自己 此外,由于某些方法要求参数在执行期间保持不变,因此将集合传递给自身可能会导致异常行为。 反例: <span class="code-snippet_outer"><span class="code-snippet__keyword">List</span><String> <span class="code-snippet__keyword">list</span> = <span class="code-snippet__keyword">new</span> ArrayList<>();</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">list</span>.add(<span class="code-snippet__string">"Hello"</span>);</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">list</span>.add(<span class="code-snippet__string">"World"</span>);</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">if</span> (<span class="code-snippet__keyword">list</span>.containsAll(<span class="code-snippet__keyword">list</span>)) { <span class="code-snippet__comment">// 无意义,总是返回true</span></span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">list</span>.removeAll(<span class="code-snippet__keyword">list</span>); <span class="code-snippet__comment">// 性能差, 直接使用clear()</span></span> 1 <span class="code-snippet_outer"><span class="code-snippet__keyword">List</span><String> <span class="code-snippet__keyword">list</span> = <span class="code-snippet__keyword">new</span> ArrayList<>();</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">list</span>.add(<span class="code-snippet__string">"Hello"</span>);</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">list</span>.add(<span class="code-snippet__string">"World"</span>);</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">if</span> (<span class="code-snippet__keyword">list</span>.containsAll(<span class="code-snippet__keyword">list</span>)) { <span class="code-snippet__comment">// 无意义,总是返回true</span></span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">list</span>.removeAll(<span class="code-snippet__keyword">list</span>); <span class="code-snippet__comment">// 性能差, 直接使用clear()</span></span> 集合初始化尽量指定大小 java 的集合类用起来十分方便,但是看源码可知,集合也是有大小限制的。每次扩容的时间复杂度很有可能是 O(n) ,所以尽量指定可预知的集合大小,能减少集合的扩容次数。 反例: <span class="code-snippet_outer"><span class="code-snippet__keyword">int</span>[] arr = <span class="code-snippet__keyword">new</span> <span class="code-snippet__keyword">int</span>[]{<span class="code-snippet__number">1</span>, <span class="code-snippet__number">2</span>, <span class="code-snippet__number">3</span>};</span></code><code><span class="code-snippet_outer">List<Integer> <span class="code-snippet__built_in">list</span> = <span class="code-snippet__keyword">new</span> ArrayList<>();</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">for</span> (<span class="code-snippet__keyword">int</span> i : arr) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__built_in">list</span>.add(i);</span></code><code><span class="code-snippet_outer">}</span> 1 <span class="code-snippet_outer"><span class="code-snippet__keyword">int</span>[] arr = <span class="code-snippet__keyword">new</span> <span class="code-snippet__keyword">int</span>[]{<span class="code-snippet__number">1</span>, <span class="code-snippet__number">2</span>, <span class="code-snippet__number">3</span>};</span></code><code><span class="code-snippet_outer">List<Integer> <span class="code-snippet__built_in">list</span> = <span class="code-snippet__keyword">new</span> ArrayList<>();</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">for</span> (<span class="code-snippet__keyword">int</span> i : arr) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__built_in">list</span>.add(i);</span></code><code><span class="code-snippet_outer">}</span> 正例: <span class="code-snippet_outer"><span class="code-snippet__keyword">int</span>[] arr = <span class="code-snippet__keyword">new</span> <span class="code-snippet__keyword">int</span>[]{<span class="code-snippet__number">1</span>, <span class="code-snippet__number">2</span>, <span class="code-snippet__number">3</span>};</span></code><code><span class="code-snippet_outer">List<Integer> <span class="code-snippet__built_in">list</span> = <span class="code-snippet__keyword">new</span> ArrayList<>(arr.length);</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">for</span> (<span class="code-snippet__keyword">int</span> i : arr) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__built_in">list</span>.add(i);</span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer">}</span> 123 <span class="code-snippet_outer"><span class="code-snippet__keyword">int</span>[] arr = <span class="code-snippet__keyword">new</span> <span class="code-snippet__keyword">int</span>[]{<span class="code-snippet__number">1</span>, <span class="code-snippet__number">2</span>, <span class="code-snippet__number">3</span>};</span></code><code><span class="code-snippet_outer">List<Integer> <span class="code-snippet__built_in">list</span> = <span class="code-snippet__keyword">new</span> ArrayList<>(arr.length);</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">for</span> (<span class="code-snippet__keyword">int</span> i : arr) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__built_in">list</span>.add(i);</span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer">}</span> 字符串拼接使用 StringBuilder 一般的字符串拼接在编译期 java 会进行优化,但是在循环中字符串拼接, java 编译期无法做到优化,所以需要使用 StringBuilder 进行替换。 反例: <span class="code-snippet_outer"><span class="code-snippet__built_in">String</span> s = <span class="code-snippet__string">""</span>;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">for</span> (int i = <span class="code-snippet__number">0</span>; i < <span class="code-snippet__number">10</span>; i++) {</span></code><code><span class="code-snippet_outer"> s += i;</span></code><code><span class="code-snippet_outer">}</span> 1 <span class="code-snippet_outer"><span class="code-snippet__built_in">String</span> s = <span class="code-snippet__string">""</span>;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">for</span> (int i = <span class="code-snippet__number">0</span>; i < <span class="code-snippet__number">10</span>; i++) {</span></code><code><span class="code-snippet_outer"> s += i;</span></code><code><span class="code-snippet_outer">}</span> 正例: <span class="code-snippet_outer"><span class="code-snippet__built_in">String</span> a = <span class="code-snippet__string">"a"</span>;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__built_in">String</span> b = <span class="code-snippet__string">"b"</span>;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__built_in">String</span> c = <span class="code-snippet__string">"c"</span>;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__built_in">String</span> s = a + b + c; <span class="code-snippet__comment">// 没问题,java编译器会进行优化</span></span></code><code><span class="code-snippet_outer">StringBuilder sb = <span class="code-snippet__keyword">new</span> StringBuilder();</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">for</span> (int i = <span class="code-snippet__number">0</span>; i < <span class="code-snippet__number">10</span>; i++) {</span></code><code><span class="code-snippet_outer"> sb.append(i); <span class="code-snippet__comment">// 循环中,java编译器无法进行优化,所以要手动使用StringBuilder</span></span></code><code><span class="code-snippet_outer">}</span> 1 <span class="code-snippet_outer"><span class="code-snippet__built_in">String</span> a = <span class="code-snippet__string">"a"</span>;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__built_in">String</span> b = <span class="code-snippet__string">"b"</span>;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__built_in">String</span> c = <span class="code-snippet__string">"c"</span>;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__built_in">String</span> s = a + b + c; <span class="code-snippet__comment">// 没问题,java编译器会进行优化</span></span></code><code><span class="code-snippet_outer">StringBuilder sb = <span class="code-snippet__keyword">new</span> StringBuilder();</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">for</span> (int i = <span class="code-snippet__number">0</span>; i < <span class="code-snippet__number">10</span>; i++) {</span></code><code><span class="code-snippet_outer"> sb.append(i); <span class="code-snippet__comment">// 循环中,java编译器无法进行优化,所以要手动使用StringBuilder</span></span></code><code><span class="code-snippet_outer">}</span> List 的随机访问 大家都知道数组和链表的区别:数组的随机访问效率更高。当调用方法获取到 List 后,如果想随机访问其中的数据,并不知道该数组内部实现是链表还是数组,怎么办呢?可以判断它是否实现* RandomAccess *接口。 正例: <span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer">// 调用别人的服务获取到list</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">List</span><Integer> <span class="code-snippet__keyword">list</span> = otherService.getList();</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">if</span> (<span class="code-snippet__keyword">list</span> <span class="code-snippet__keyword">instanceof</span> RandomAccess) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__comment">// 内部数组实现,可以随机访问</span></span></code><code><span class="code-snippet_outer"> System.out.println(<span class="code-snippet__keyword">list</span>.get(<span class="code-snippet__keyword">list</span>.size() - <span class="code-snippet__number">1</span>));</span></code><code><span class="code-snippet_outer">} <span class="code-snippet__keyword">else</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__comment">// 内部可能是链表实现,随机访问效率低</span></span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer"> </span> 1234 <span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer">// 调用别人的服务获取到list</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">List</span><Integer> <span class="code-snippet__keyword">list</span> = otherService.getList();</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">if</span> (<span class="code-snippet__keyword">list</span> <span class="code-snippet__keyword">instanceof</span> RandomAccess) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__comment">// 内部数组实现,可以随机访问</span></span></code><code><span class="code-snippet_outer"> System.out.println(<span class="code-snippet__keyword">list</span>.get(<span class="code-snippet__keyword">list</span>.size() - <span class="code-snippet__number">1</span>));</span></code><code><span class="code-snippet_outer">} <span class="code-snippet__keyword">else</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__comment">// 内部可能是链表实现,随机访问效率低</span></span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer"></span> 频繁调用 Collection.contains 方法请使用 Set 在 java 集合类库中,List 的 contains 方法普遍时间复杂度是 O(n) ,如果在代码中需要频繁调用 contains 方法查找数据,可以先将 list 转换成 HashSet 实现,将 O(n) 的时间复杂度降为 O(1) 。 反例: <span class="code-snippet_outer">ArrayList<Integer> <span class="code-snippet__built_in">list</span> = otherService.getList();</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">for</span> (<span class="code-snippet__keyword">int</span> i = <span class="code-snippet__number">0</span>; i <= Integer.MAX_VALUE; i++) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__comment">// 时间复杂度O(n)</span></span></code><code><span class="code-snippet_outer"> <span class="code-snippet__built_in">list</span>.contains(i);</span></code><code><span class="code-snippet_outer">}</span> 1 <span class="code-snippet_outer">ArrayList<Integer> <span class="code-snippet__built_in">list</span> = otherService.getList();</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">for</span> (<span class="code-snippet__keyword">int</span> i = <span class="code-snippet__number">0</span>; i <= Integer.MAX_VALUE; i++) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__comment">// 时间复杂度O(n)</span></span></code><code><span class="code-snippet_outer"> <span class="code-snippet__built_in">list</span>.contains(i);</span></code><code><span class="code-snippet_outer">}</span> 正例: <span class="code-snippet_outer">ArrayList<Integer> <span class="code-snippet__built_in">list</span> = otherService.getList();</span></code><code><span class="code-snippet_outer">Set<Integer> <span class="code-snippet__built_in">set</span> = <span class="code-snippet__keyword">new</span> HashSet(<span class="code-snippet__built_in">list</span>);</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">for</span> (<span class="code-snippet__keyword">int</span> i = <span class="code-snippet__number">0</span>; i <= Integer.MAX_VALUE; i++) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__comment">// 时间复杂度O(1)</span></span></code><code><span class="code-snippet_outer"> <span class="code-snippet__built_in">set</span>.contains(i);</span></code><code><span class="code-snippet_outer">}</span> 1 <span class="code-snippet_outer">ArrayList<Integer> <span class="code-snippet__built_in">list</span> = otherService.getList();</span></code><code><span class="code-snippet_outer">Set<Integer> <span class="code-snippet__built_in">set</span> = <span class="code-snippet__keyword">new</span> HashSet(<span class="code-snippet__built_in">list</span>);</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">for</span> (<span class="code-snippet__keyword">int</span> i = <span class="code-snippet__number">0</span>; i <= Integer.MAX_VALUE; i++) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__comment">// 时间复杂度O(1)</span></span></code><code><span class="code-snippet_outer"> <span class="code-snippet__built_in">set</span>.contains(i);</span></code><code><span class="code-snippet_outer">}</span> 让代码更优雅 长整型常量后添加大写 L 在使用长整型常量值时,后面需要添加 L ,必须是大写的 L ,不能是小写的 l ,小写 l 容易跟数字 1 混淆而造成误解。 反例: <span class="code-snippet_outer"><span class="code-snippet__keyword">long</span> <span class="code-snippet__keyword">value</span> = <span class="code-snippet__number">1l</span>;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">long</span> max = Math.max(<span class="code-snippet__number">1L</span>, <span class="code-snippet__number">5</span>);</span> 1 <span class="code-snippet_outer"><span class="code-snippet__keyword">long</span> <span class="code-snippet__keyword">value</span> = <span class="code-snippet__number">1l</span>;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">long</span> max = Math.max(<span class="code-snippet__number">1L</span>, <span class="code-snippet__number">5</span>);</span> 正例: <span class="code-snippet_outer"><span class="code-snippet__keyword">long</span> <span class="code-snippet__keyword">value</span> = <span class="code-snippet__number">1L</span>;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">long</span> max = Math.max(<span class="code-snippet__number">1L</span>, <span class="code-snippet__number">5L</span>);</span> 1 <span class="code-snippet_outer"><span class="code-snippet__keyword">long</span> <span class="code-snippet__keyword">value</span> = <span class="code-snippet__number">1L</span>;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">long</span> max = Math.max(<span class="code-snippet__number">1L</span>, <span class="code-snippet__number">5L</span>);</span> 不要使用魔法值 当你编写一段代码时,使用魔法值可能看起来很明确,但在调试时它们却不显得那么明确了。这就是为什么需要把魔法值定义为可读取常量的原因。但是,-1、0 和 1不被视为魔法值。 反例: <span class="code-snippet_outer"><span class="code-snippet__keyword">for</span> (<span class="code-snippet__keyword">int</span> i = <span class="code-snippet__number">0</span>; i < <span class="code-snippet__number">100</span>; i++){</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">if</span> (a == <span class="code-snippet__number">100</span>) {</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer">}</span> 1 <span class="code-snippet_outer"><span class="code-snippet__keyword">for</span> (<span class="code-snippet__keyword">int</span> i = <span class="code-snippet__number">0</span>; i < <span class="code-snippet__number">100</span>; i++){</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">if</span> (a == <span class="code-snippet__number">100</span>) {</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer">}</span> 正例: <span class="code-snippet_outer"><span class="code-snippet__keyword">private</span> <span class="code-snippet__keyword">static</span> <span class="code-snippet__keyword">final</span> <span class="code-snippet__keyword">int</span> MAX_COUNT = <span class="code-snippet__number">100</span>;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">for</span> (<span class="code-snippet__keyword">int</span> i = <span class="code-snippet__number">0</span>; i < MAX_COUNT; i++){</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">if</span> (count == MAX_COUNT) {</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer">}</span> 1 <span class="code-snippet_outer"><span class="code-snippet__keyword">private</span> <span class="code-snippet__keyword">static</span> <span class="code-snippet__keyword">final</span> <span class="code-snippet__keyword">int</span> MAX_COUNT = <span class="code-snippet__number">100</span>;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">for</span> (<span class="code-snippet__keyword">int</span> i = <span class="code-snippet__number">0</span>; i < MAX_COUNT; i++){</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">if</span> (count == MAX_COUNT) {</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer">}</span> 不要使用集合实现来赋值静态成员变量 对于集合类型的静态成员变量,不要使用集合实现来赋值,应该使用静态代码块赋值。 反例: <span class="code-snippet_outer"><span class="code-snippet__keyword">private</span> <span class="code-snippet__keyword">static</span> Map<<span class="code-snippet__built_in">String</span>, Integer> map = <span class="code-snippet__keyword">new</span> HashMap<<span class="code-snippet__built_in">String</span>, Integer>() {</span></code><code><span class="code-snippet_outer"> {</span></code><code><span class="code-snippet_outer"> put(<span class="code-snippet__string">"a"</span>, <span class="code-snippet__number">1</span>);</span></code><code><span class="code-snippet_outer"> put(<span class="code-snippet__string">"b"</span>, <span class="code-snippet__number">2</span>);</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer">};</span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">private</span> <span class="code-snippet__keyword">static</span> List<<span class="code-snippet__built_in">String</span>> list = <span class="code-snippet__keyword">new</span> ArrayList<<span class="code-snippet__built_in">String</span>>() {</span></code><code><span class="code-snippet_outer"> {</span></code><code><span class="code-snippet_outer"> add(<span class="code-snippet__string">"a"</span>);</span></code><code><span class="code-snippet_outer"> add(<span class="code-snippet__string">"b"</span>);</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer">};</span> 123 <span class="code-snippet_outer"><span class="code-snippet__keyword">private</span> <span class="code-snippet__keyword">static</span> Map<<span class="code-snippet__built_in">String</span>, Integer> map = <span class="code-snippet__keyword">new</span> HashMap<<span class="code-snippet__built_in">String</span>, Integer>() {</span></code><code><span class="code-snippet_outer"> {</span></code><code><span class="code-snippet_outer"> put(<span class="code-snippet__string">"a"</span>, <span class="code-snippet__number">1</span>);</span></code><code><span class="code-snippet_outer"> put(<span class="code-snippet__string">"b"</span>, <span class="code-snippet__number">2</span>);</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer">};</span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">private</span> <span class="code-snippet__keyword">static</span> List<<span class="code-snippet__built_in">String</span>> list = <span class="code-snippet__keyword">new</span> ArrayList<<span class="code-snippet__built_in">String</span>>() {</span></code><code><span class="code-snippet_outer"> {</span></code><code><span class="code-snippet_outer"> add(<span class="code-snippet__string">"a"</span>);</span></code><code><span class="code-snippet_outer"> add(<span class="code-snippet__string">"b"</span>);</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer">};</span> 正例: <span class="code-snippet_outer"><span class="code-snippet__keyword">private</span> <span class="code-snippet__keyword">static</span> Map<String, Integer> <span class="code-snippet__built_in">map</span> = <span class="code-snippet__keyword">new</span> HashMap<>();</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">static</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__built_in">map</span>.put(<span class="code-snippet__string">"a"</span>, <span class="code-snippet__number">1</span>);</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__built_in">map</span>.put(<span class="code-snippet__string">"b"</span>, <span class="code-snippet__number">2</span>);</span></code><code><span class="code-snippet_outer">};</span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">private</span> <span class="code-snippet__keyword">static</span> List<String> <span class="code-snippet__built_in">list</span> = <span class="code-snippet__keyword">new</span> ArrayList<>();</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">static</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__built_in">list</span>.add(<span class="code-snippet__string">"a"</span>);</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__built_in">list</span>.add(<span class="code-snippet__string">"b"</span>);</span></code><code><span class="code-snippet_outer">};</span> 123 <span class="code-snippet_outer"><span class="code-snippet__keyword">private</span> <span class="code-snippet__keyword">static</span> Map<String, Integer> <span class="code-snippet__built_in">map</span> = <span class="code-snippet__keyword">new</span> HashMap<>();</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">static</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__built_in">map</span>.put(<span class="code-snippet__string">"a"</span>, <span class="code-snippet__number">1</span>);</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__built_in">map</span>.put(<span class="code-snippet__string">"b"</span>, <span class="code-snippet__number">2</span>);</span></code><code><span class="code-snippet_outer">};</span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">private</span> <span class="code-snippet__keyword">static</span> List<String> <span class="code-snippet__built_in">list</span> = <span class="code-snippet__keyword">new</span> ArrayList<>();</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">static</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__built_in">list</span>.add(<span class="code-snippet__string">"a"</span>);</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__built_in">list</span>.add(<span class="code-snippet__string">"b"</span>);</span></code><code><span class="code-snippet_outer">};</span> 建议使用 try-with-resources 语句 Java 7 中引入了 try-with-resources 语句,该语句能保证将相关资源关闭,优于原来的 try-catch-finally 语句,并且使程序代码更安全更简洁。 反例: <span class="code-snippet_outer"><span class="code-snippet__keyword">private</span> <span class="code-snippet__built_in">void</span> handle(<span class="code-snippet__built_in">String</span> fileName) {</span></code><code><span class="code-snippet_outer"> BufferedReader reader = <span class="code-snippet__literal">null</span>;</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">try</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__built_in">String</span> line;</span></code><code><span class="code-snippet_outer"> reader = <span class="code-snippet__keyword">new</span> BufferedReader(<span class="code-snippet__keyword">new</span> FileReader(fileName));</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">while</span> ((line = reader.readLine()) != <span class="code-snippet__literal">null</span>) {</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"> } <span class="code-snippet__keyword">catch</span> (Exception e) {</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer"> } <span class="code-snippet__keyword">finally</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">if</span> (reader != <span class="code-snippet__literal">null</span>) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">try</span> {</span></code><code><span class="code-snippet_outer"> reader.close();</span></code><code><span class="code-snippet_outer"> } <span class="code-snippet__keyword">catch</span> (IOException e) {</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer">}</span> 1 <span class="code-snippet_outer"><span class="code-snippet__keyword">private</span> <span class="code-snippet__built_in">void</span> handle(<span class="code-snippet__built_in">String</span> fileName) {</span></code><code><span class="code-snippet_outer"> BufferedReader reader = <span class="code-snippet__literal">null</span>;</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">try</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__built_in">String</span> line;</span></code><code><span class="code-snippet_outer"> reader = <span class="code-snippet__keyword">new</span> BufferedReader(<span class="code-snippet__keyword">new</span> FileReader(fileName));</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">while</span> ((line = reader.readLine()) != <span class="code-snippet__literal">null</span>) {</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"> } <span class="code-snippet__keyword">catch</span> (Exception e) {</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer"> } <span class="code-snippet__keyword">finally</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">if</span> (reader != <span class="code-snippet__literal">null</span>) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">try</span> {</span></code><code><span class="code-snippet_outer"> reader.close();</span></code><code><span class="code-snippet_outer"> } <span class="code-snippet__keyword">catch</span> (IOException e) {</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer">}</span> 正例: <span class="code-snippet_outer"><span class="code-snippet__keyword">private</span> <span class="code-snippet__built_in">void</span> handle(<span class="code-snippet__built_in">String</span> fileName) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">try</span> (BufferedReader reader = <span class="code-snippet__keyword">new</span> BufferedReader(<span class="code-snippet__keyword">new</span> FileReader(fileName))) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__built_in">String</span> line;</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">while</span> ((line = reader.readLine()) != <span class="code-snippet__literal">null</span>) {</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"> } <span class="code-snippet__keyword">catch</span> (Exception e) {</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer">}</span> 1 <span class="code-snippet_outer"><span class="code-snippet__keyword">private</span> <span class="code-snippet__built_in">void</span> handle(<span class="code-snippet__built_in">String</span> fileName) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">try</span> (BufferedReader reader = <span class="code-snippet__keyword">new</span> BufferedReader(<span class="code-snippet__keyword">new</span> FileReader(fileName))) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__built_in">String</span> line;</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">while</span> ((line = reader.readLine()) != <span class="code-snippet__literal">null</span>) {</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"> } <span class="code-snippet__keyword">catch</span> (Exception e) {</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer">}</span> 删除未使用的私有方法和字段 删除未使用的私有方法和字段,使代码更简洁更易维护。若有需要再使用,可以从历史提交中找回。 反例: <span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">class</span> <span class="code-snippet__title">DoubleDemo1</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">private</span> <span class="code-snippet__keyword">int</span> unusedField = <span class="code-snippet__number">100</span>;</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__function"><span class="code-snippet__keyword">private</span> <span class="code-snippet__keyword">void</span> <span class="code-snippet__title">unusedMethod</span>()</span> {</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">int</span> <span class="code-snippet__title">sum</span>(<span class="code-snippet__params"><span class="code-snippet__keyword">int</span> a, <span class="code-snippet__keyword">int</span> b</span>)</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> a + b;</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer">}</span> 1 <span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">class</span> <span class="code-snippet__title">DoubleDemo1</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">private</span> <span class="code-snippet__keyword">int</span> unusedField = <span class="code-snippet__number">100</span>;</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__function"><span class="code-snippet__keyword">private</span> <span class="code-snippet__keyword">void</span> <span class="code-snippet__title">unusedMethod</span>()</span> {</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">int</span> <span class="code-snippet__title">sum</span>(<span class="code-snippet__params"><span class="code-snippet__keyword">int</span> a, <span class="code-snippet__keyword">int</span> b</span>)</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> a + b;</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer">}</span> 正例: <span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">class</span> <span class="code-snippet__title">DoubleDemo1</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">int</span> <span class="code-snippet__title">sum</span>(<span class="code-snippet__params"><span class="code-snippet__keyword">int</span> a, <span class="code-snippet__keyword">int</span> b</span>)</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> a + b;</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer">}</span> 1 <span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">class</span> <span class="code-snippet__title">DoubleDemo1</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">int</span> <span class="code-snippet__title">sum</span>(<span class="code-snippet__params"><span class="code-snippet__keyword">int</span> a, <span class="code-snippet__keyword">int</span> b</span>)</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> a + b;</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer">}</span> 删除未使用的局部变量 删除未使用的局部变量,使代码更简洁更易维护。 反例: <span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">int</span> <span class="code-snippet__title">sum</span>(<span class="code-snippet__params"><span class="code-snippet__keyword">int</span> a, <span class="code-snippet__keyword">int</span> b</span>)</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">int</span> c = <span class="code-snippet__number">100</span>;</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> a + b;</span></code><code><span class="code-snippet_outer">}</span> 1 <span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">int</span> <span class="code-snippet__title">sum</span>(<span class="code-snippet__params"><span class="code-snippet__keyword">int</span> a, <span class="code-snippet__keyword">int</span> b</span>)</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">int</span> c = <span class="code-snippet__number">100</span>;</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> a + b;</span></code><code><span class="code-snippet_outer">}</span> 正例: <span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">int</span> <span class="code-snippet__title">sum</span>(<span class="code-snippet__params"><span class="code-snippet__keyword">int</span> a, <span class="code-snippet__keyword">int</span> b</span>)</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> a + b;</span></code><code><span class="code-snippet_outer">}</span> 1 <span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">int</span> <span class="code-snippet__title">sum</span>(<span class="code-snippet__params"><span class="code-snippet__keyword">int</span> a, <span class="code-snippet__keyword">int</span> b</span>)</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> a + b;</span></code><code><span class="code-snippet_outer">}</span> 删除未使用的方法参数 未使用的方法参数具有误导性,删除未使用的方法参数,使代码更简洁更易维护。但是,由于重写方法是基于父类或接口的方法定义,即便有未使用的方法参数,也是不能删除的。 反例: <span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">int</span> <span class="code-snippet__title">sum</span>(<span class="code-snippet__params"><span class="code-snippet__keyword">int</span> a, <span class="code-snippet__keyword">int</span> b, <span class="code-snippet__keyword">int</span> c</span>)</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> a + b;</span></code><code><span class="code-snippet_outer">}</span> 1 <span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">int</span> <span class="code-snippet__title">sum</span>(<span class="code-snippet__params"><span class="code-snippet__keyword">int</span> a, <span class="code-snippet__keyword">int</span> b, <span class="code-snippet__keyword">int</span> c</span>)</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> a + b;</span></code><code><span class="code-snippet_outer">}</span> 正例: <span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">int</span> <span class="code-snippet__title">sum</span>(<span class="code-snippet__params"><span class="code-snippet__keyword">int</span> a, <span class="code-snippet__keyword">int</span> b</span>)</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> a + b;</span></code><code><span class="code-snippet_outer">}</span> 1 <span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">int</span> <span class="code-snippet__title">sum</span>(<span class="code-snippet__params"><span class="code-snippet__keyword">int</span> a, <span class="code-snippet__keyword">int</span> b</span>)</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> a + b;</span></code><code><span class="code-snippet_outer">}</span> 删除表达式的多余括号 对应表达式中的多余括号,有人认为有助于代码阅读,也有人认为完全没有必要。对于一个熟悉 Java 语法的人来说,表达式中的多余括号反而会让代码显得更繁琐。 反例: <span class="code-snippet_outer"><span class="code-snippet__keyword">return</span> (x);</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">return</span> (x + <span class="code-snippet__number">2</span>);</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">int</span> x = (y * <span class="code-snippet__number">3</span>) + <span class="code-snippet__number">1</span>;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">int</span> m = (n * <span class="code-snippet__number">4</span> + <span class="code-snippet__number">2</span>);</span> 1 <span class="code-snippet_outer"><span class="code-snippet__keyword">return</span> (x);</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">return</span> (x + <span class="code-snippet__number">2</span>);</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">int</span> x = (y * <span class="code-snippet__number">3</span>) + <span class="code-snippet__number">1</span>;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">int</span> m = (n * <span class="code-snippet__number">4</span> + <span class="code-snippet__number">2</span>);</span> 正例: <span class="code-snippet_outer"><span class="code-snippet__keyword">return</span> x;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">return</span> x + <span class="code-snippet__number">2</span>;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">int</span> x = y * <span class="code-snippet__number">3</span> + <span class="code-snippet__number">1</span>;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">int</span> m = n * <span class="code-snippet__number">4</span> + <span class="code-snippet__number">2</span>;</span> 1 <span class="code-snippet_outer"><span class="code-snippet__keyword">return</span> x;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">return</span> x + <span class="code-snippet__number">2</span>;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">int</span> x = y * <span class="code-snippet__number">3</span> + <span class="code-snippet__number">1</span>;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">int</span> m = n * <span class="code-snippet__number">4</span> + <span class="code-snippet__number">2</span>;</span> 工具类应该屏蔽构造函数 工具类是一堆静态字段和函数的集合,不应该被实例化。但是,Java 为每个没有明确定义构造函数的类添加了一个隐式公有构造函数。所以,为了避免 java “小白”使用有误,应该显式定义私有构造函数来屏蔽这个隐式公有构造函数。 反例: <span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__class"><span class="code-snippet__keyword">class</span> <span class="code-snippet__title">MathUtils</span> </span>{</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> <span class="code-snippet__keyword">final</span> <span class="code-snippet__keyword">double</span> PI = <span class="code-snippet__number">3.1415926</span>D;</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> <span class="code-snippet__keyword">int</span> <span class="code-snippet__title">sum(<span class="code-snippet__keyword">int</span> a, <span class="code-snippet__keyword">int</span> b)</span> </span>{</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> a + b;</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer">}</span> 1 <span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__class"><span class="code-snippet__keyword">class</span> <span class="code-snippet__title">MathUtils</span> </span>{</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> <span class="code-snippet__keyword">final</span> <span class="code-snippet__keyword">double</span> PI = <span class="code-snippet__number">3.1415926</span>D;</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> <span class="code-snippet__keyword">int</span> <span class="code-snippet__title">sum(<span class="code-snippet__keyword">int</span> a, <span class="code-snippet__keyword">int</span> b)</span> </span>{</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> a + b;</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer">}</span> 正例: <span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__class"><span class="code-snippet__keyword">class</span> <span class="code-snippet__title">MathUtils</span> </span>{</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> <span class="code-snippet__keyword">final</span> <span class="code-snippet__keyword">double</span> PI = <span class="code-snippet__number">3.1415926</span>D;</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__function"><span class="code-snippet__keyword">private</span> <span class="code-snippet__title">MathUtils()</span> </span>{}</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> <span class="code-snippet__keyword">int</span> <span class="code-snippet__title">sum(<span class="code-snippet__keyword">int</span> a, <span class="code-snippet__keyword">int</span> b)</span> </span>{</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> a + b;</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer">}</span> 1 <span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__class"><span class="code-snippet__keyword">class</span> <span class="code-snippet__title">MathUtils</span> </span>{</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> <span class="code-snippet__keyword">final</span> <span class="code-snippet__keyword">double</span> PI = <span class="code-snippet__number">3.1415926</span>D;</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__function"><span class="code-snippet__keyword">private</span> <span class="code-snippet__title">MathUtils()</span> </span>{}</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> <span class="code-snippet__keyword">int</span> <span class="code-snippet__title">sum(<span class="code-snippet__keyword">int</span> a, <span class="code-snippet__keyword">int</span> b)</span> </span>{</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> a + b;</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer">}</span> 删除多余的异常捕获并抛出 用 catch 语句捕获异常后,什么也不进行处理,就让异常重新抛出,这跟不捕获异常的效果一样,可以删除这块代码或添加别的处理。 反例: <span class="code-snippet_outer"><span class="code-snippet__keyword">private</span> <span class="code-snippet__keyword">static</span> <span class="code-snippet__built_in">String</span> readFile(<span class="code-snippet__built_in">String</span> fileName) throws IOException {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">try</span> (BufferedReader reader = <span class="code-snippet__keyword">new</span> BufferedReader(<span class="code-snippet__keyword">new</span> FileReader(fileName))) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__built_in">String</span> line;</span></code><code><span class="code-snippet_outer"> StringBuilder builder = <span class="code-snippet__keyword">new</span> StringBuilder();</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">while</span> ((line = reader.readLine()) != <span class="code-snippet__literal">null</span>) {</span></code><code><span class="code-snippet_outer"> builder.append(line);</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> builder.toString();</span></code><code><span class="code-snippet_outer"> } <span class="code-snippet__keyword">catch</span> (Exception e) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">throw</span> e;</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer">}</span> 1 <span class="code-snippet_outer"><span class="code-snippet__keyword">private</span> <span class="code-snippet__keyword">static</span> <span class="code-snippet__built_in">String</span> readFile(<span class="code-snippet__built_in">String</span> fileName) throws IOException {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">try</span> (BufferedReader reader = <span class="code-snippet__keyword">new</span> BufferedReader(<span class="code-snippet__keyword">new</span> FileReader(fileName))) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__built_in">String</span> line;</span></code><code><span class="code-snippet_outer"> StringBuilder builder = <span class="code-snippet__keyword">new</span> StringBuilder();</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">while</span> ((line = reader.readLine()) != <span class="code-snippet__literal">null</span>) {</span></code><code><span class="code-snippet_outer"> builder.append(line);</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> builder.toString();</span></code><code><span class="code-snippet_outer"> } <span class="code-snippet__keyword">catch</span> (Exception e) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">throw</span> e;</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer">}</span> 正例: <span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">private</span> <span class="code-snippet__keyword">static</span> <span class="code-snippet__built_in">String</span> readFile(<span class="code-snippet__built_in">String</span> fileName) throws IOException {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">try</span> (BufferedReader reader = <span class="code-snippet__keyword">new</span> BufferedReader(<span class="code-snippet__keyword">new</span> FileReader(fileName))) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__built_in">String</span> line;</span></code><code><span class="code-snippet_outer"> StringBuilder builder = <span class="code-snippet__keyword">new</span> StringBuilder();</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">while</span> ((line = reader.readLine()) != <span class="code-snippet__literal">null</span>) {</span></code><code><span class="code-snippet_outer"> builder.append(line);</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> builder.toString();</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer">}</span> 123 <span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"><span class="code-snippet__keyword">private</span> <span class="code-snippet__keyword">static</span> <span class="code-snippet__built_in">String</span> readFile(<span class="code-snippet__built_in">String</span> fileName) throws IOException {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">try</span> (BufferedReader reader = <span class="code-snippet__keyword">new</span> BufferedReader(<span class="code-snippet__keyword">new</span> FileReader(fileName))) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__built_in">String</span> line;</span></code><code><span class="code-snippet_outer"> StringBuilder builder = <span class="code-snippet__keyword">new</span> StringBuilder();</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">while</span> ((line = reader.readLine()) != <span class="code-snippet__literal">null</span>) {</span></code><code><span class="code-snippet_outer"> builder.append(line);</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> builder.toString();</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer">}</span> 公有静态常量应该通过类访问 虽然通过类的实例访问公有静态常量是允许的,但是容易让人它误认为每个类的实例都有一个公有静态常量。所以,公有静态常量应该直接通过类访问。 反例: <span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">class</span> User {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> final <span class="code-snippet__built_in">String</span> CONST_NAME = <span class="code-snippet__string">"name"</span>;</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer">User user = <span class="code-snippet__keyword">new</span> User();</span></code><code><span class="code-snippet_outer"><span class="code-snippet__built_in">String</span> nameKey = user.CONST_NAME;</span> 123 <span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">class</span> User {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> final <span class="code-snippet__built_in">String</span> CONST_NAME = <span class="code-snippet__string">"name"</span>;</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer">User user = <span class="code-snippet__keyword">new</span> User();</span></code><code><span class="code-snippet_outer"><span class="code-snippet__built_in">String</span> nameKey = user.CONST_NAME;</span> 正例: <span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">class</span> User {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> final <span class="code-snippet__built_in">String</span> CONST_NAME = <span class="code-snippet__string">"name"</span>;</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"><span class="code-snippet__built_in">String</span> nameKey = User.CONST_NAME;</span> 123 <span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">class</span> User {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> final <span class="code-snippet__built_in">String</span> CONST_NAME = <span class="code-snippet__string">"name"</span>;</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"><span class="code-snippet__built_in">String</span> nameKey = User.CONST_NAME;</span> 不要用NullPointerException判断空 空指针异常应该用代码规避(比如检测不为空),而不是用捕获异常的方式处理。 反例: <span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> String getUserName(User user) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">try</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> user.getName();</span></code><code><span class="code-snippet_outer"> } <span class="code-snippet__keyword">catch</span> (NullPointerException e) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> <span class="code-snippet__literal">null</span>;</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer">}</span> 1 <span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> String getUserName(User user) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">try</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> user.getName();</span></code><code><span class="code-snippet_outer"> } <span class="code-snippet__keyword">catch</span> (NullPointerException e) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> <span class="code-snippet__literal">null</span>;</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer">}</span> 正例: <span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> String getUserName(User user) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">if</span> (Objects.isNull(user)) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> <span class="code-snippet__literal">null</span>;</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> user.getName();</span></code><code><span class="code-snippet_outer">}</span> 1 <span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> String getUserName(User user) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">if</span> (Objects.isNull(user)) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> <span class="code-snippet__literal">null</span>;</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> user.getName();</span></code><code><span class="code-snippet_outer">}</span> 使用String.valueOf(value)代替””+value 当要把其它对象或类型转化为字符串时,使用 String.valueOf(value) 比””+value 的效率更高。 反例: <span class="code-snippet_outer"><span class="code-snippet__attribute">int</span> i = <span class="code-snippet__number">1</span>;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__attribute">String</span> s = <span class="code-snippet__string">""</span> + i;</span> 1 <span class="code-snippet_outer"><span class="code-snippet__attribute">int</span> i = <span class="code-snippet__number">1</span>;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__attribute">String</span> s = <span class="code-snippet__string">""</span> + i;</span> 正例: <span class="code-snippet_outer">int i = <span class="code-snippet__number">1</span>;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__built_in">String</span> s = <span class="code-snippet__built_in">String</span>.valueOf(i);</span> 1 <span class="code-snippet_outer">int i = <span class="code-snippet__number">1</span>;</span></code><code><span class="code-snippet_outer"><span class="code-snippet__built_in">String</span> s = <span class="code-snippet__built_in">String</span>.valueOf(i);</span> 过时代码添加 @Deprecated 注解 当一段代码过时,但为了兼容又无法直接删除,不希望以后有人再使用它时,可以添加 @Deprecated 注解进行标记。在文档注释中添加 @deprecated 来进行解释,并提供可替代方案。 正例: <span class="code-snippet_outer">/**</span></code><code><span class="code-snippet_outer"> * 保存</span></code><code><span class="code-snippet_outer"> *</span></code><code><span class="code-snippet_outer"> * <span class="code-snippet__doctag">@deprecated</span> 此方法效率较低,请使用{<span class="code-snippet__doctag">@link</span> newSave()}方法替换它</span></code><code><span class="code-snippet_outer"> */</span></code><code><span class="code-snippet_outer">@Deprecated</span></code><code><span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">void</span> <span class="code-snippet__title">save()</span></span>{</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__comment">// do something</span></span></code><code><span class="code-snippet_outer">}</span> 1 <span class="code-snippet_outer">/**</span></code><code><span class="code-snippet_outer"> * 保存</span></code><code><span class="code-snippet_outer"> *</span></code><code><span class="code-snippet_outer"> * <span class="code-snippet__doctag">@deprecated</span> 此方法效率较低,请使用{<span class="code-snippet__doctag">@link</span> newSave()}方法替换它</span></code><code><span class="code-snippet_outer"> */</span></code><code><span class="code-snippet_outer">@Deprecated</span></code><code><span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">void</span> <span class="code-snippet__title">save()</span></span>{</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__comment">// do something</span></span></code><code><span class="code-snippet_outer">}</span> 让代码远离 bug 禁止使用构造方法 BigDecimal(double) BigDecimal(double) 存在精度损失风险,在精确计算或值比较的场景中可能会导致业务逻辑异常。 反例: <span class="code-snippet_outer">BigDecimal <span class="code-snippet__keyword">value</span> = <span class="code-snippet__keyword">new</span> BigDecimal(<span class="code-snippet__number">0.1</span>D); <span class="code-snippet__comment">// 0.100000000000000005551115...</span></span> 1 <span class="code-snippet_outer">BigDecimal <span class="code-snippet__keyword">value</span> = <span class="code-snippet__keyword">new</span> BigDecimal(<span class="code-snippet__number">0.1</span>D); <span class="code-snippet__comment">// 0.100000000000000005551115...</span></span> 正例: <span class="code-snippet_outer">BigDecimal <span class="code-snippet__keyword">value</span> = BigDecimal.valueOf(<span class="code-snippet__number">0.1</span>D);; <span class="code-snippet__comment">// 0.1</span></span> 1 <span class="code-snippet_outer">BigDecimal <span class="code-snippet__keyword">value</span> = BigDecimal.valueOf(<span class="code-snippet__number">0.1</span>D);; <span class="code-snippet__comment">// 0.1</span></span> 返回空数组和空集合而不是 null 返回 null ,需要调用方强制检测 null ,否则就会抛出空指针异常。返回空数组或空集合,有效地避免了调用方因为未检测 null 而抛出空指针异常,还可以删除调用方检测 null 的语句使代码更简洁。 反例: <span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> Result[] <span class="code-snippet__title">getResults</span>()</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> <span class="code-snippet__literal">null</span>;</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> List<Result> <span class="code-snippet__title">getResultList</span>()</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> <span class="code-snippet__literal">null</span>;</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> Map<String, Result> <span class="code-snippet__title">getResultMap</span>()</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> <span class="code-snippet__literal">null</span>;</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> <span class="code-snippet__keyword">void</span> <span class="code-snippet__title">main</span>(<span class="code-snippet__params">String[] args</span>)</span> {</span></code><code><span class="code-snippet_outer"> Result[] results = getResults();</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">if</span> (results != <span class="code-snippet__literal">null</span>) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">for</span> (Result result : results) {</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> List<Result> resultList = getResultList();</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">if</span> (resultList != <span class="code-snippet__literal">null</span>) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">for</span> (Result result : resultList) {</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> Map<String, Result> resultMap = getResultMap();</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">if</span> (resultMap != <span class="code-snippet__literal">null</span>) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">for</span> (Map.Entry<String, Result> resultEntry : resultMap) {</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer">}</span> 1234567891011 <span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> Result[] <span class="code-snippet__title">getResults</span>()</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> <span class="code-snippet__literal">null</span>;</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> List<Result> <span class="code-snippet__title">getResultList</span>()</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> <span class="code-snippet__literal">null</span>;</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> Map<String, Result> <span class="code-snippet__title">getResultMap</span>()</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> <span class="code-snippet__literal">null</span>;</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> <span class="code-snippet__keyword">void</span> <span class="code-snippet__title">main</span>(<span class="code-snippet__params">String[] args</span>)</span> {</span></code><code><span class="code-snippet_outer"> Result[] results = getResults();</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">if</span> (results != <span class="code-snippet__literal">null</span>) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">for</span> (Result result : results) {</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"> List<Result> resultList = getResultList();</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">if</span> (resultList != <span class="code-snippet__literal">null</span>) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">for</span> (Result result : resultList) {</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"> Map<String, Result> resultMap = getResultMap();</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">if</span> (resultMap != <span class="code-snippet__literal">null</span>) {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">for</span> (Map.Entry<String, Result> resultEntry : resultMap) {</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer">}</span> 正例: <span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> Result[] <span class="code-snippet__title">getResults</span>()</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> <span class="code-snippet__keyword">new</span> Result[<span class="code-snippet__number">0</span>];</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> List<Result> <span class="code-snippet__title">getResultList</span>()</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> Collections.emptyList();</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> Map<String, Result> <span class="code-snippet__title">getResultMap</span>()</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> Collections.emptyMap();</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> <span class="code-snippet__keyword">void</span> <span class="code-snippet__title">main</span>(<span class="code-snippet__params">String[] args</span>)</span> {</span></code><code><span class="code-snippet_outer"> Result[] results = getResults();</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">for</span> (Result result : results) {</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> List<Result> resultList = getResultList();</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">for</span> (Result result : resultList) {</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> Map<String, Result> resultMap = getResultMap();</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">for</span> (Map.Entry<String, Result> resultEntry : resultMap) {</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer">}</span> 1234567891011 <span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> Result[] <span class="code-snippet__title">getResults</span>()</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> <span class="code-snippet__keyword">new</span> Result[<span class="code-snippet__number">0</span>];</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> List<Result> <span class="code-snippet__title">getResultList</span>()</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> Collections.emptyList();</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> Map<String, Result> <span class="code-snippet__title">getResultMap</span>()</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> Collections.emptyMap();</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">static</span> <span class="code-snippet__keyword">void</span> <span class="code-snippet__title">main</span>(<span class="code-snippet__params">String[] args</span>)</span> {</span></code><code><span class="code-snippet_outer"> Result[] results = getResults();</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">for</span> (Result result : results) {</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"> List<Result> resultList = getResultList();</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">for</span> (Result result : resultList) {</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"> Map<String, Result> resultMap = getResultMap();</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">for</span> (Map.Entry<String, Result> resultEntry : resultMap) {</span></code><code><span class="code-snippet_outer"> ...</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer">}</span> 优先使用常量或确定值来调用 equals 方法 对象的 equals 方法容易抛空指针异常,应使用常量或确定有值的对象来调用 equals 方法。当然,使用 java.util.Objects.equals() 方法是最佳实践。 反例: <span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">void</span> <span class="code-snippet__title">isFinished</span>(<span class="code-snippet__params">OrderStatus status</span>)</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> status.<span class="code-snippet__keyword">equals</span>(OrderStatus.FINISHED); <span class="code-snippet__comment">// 可能抛空指针异常</span></span></code><code><span class="code-snippet_outer">}</span> 1 <span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">void</span> <span class="code-snippet__title">isFinished</span>(<span class="code-snippet__params">OrderStatus status</span>)</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> status.<span class="code-snippet__keyword">equals</span>(OrderStatus.FINISHED); <span class="code-snippet__comment">// 可能抛空指针异常</span></span></code><code><span class="code-snippet_outer">}</span> 正例: <span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">void</span> <span class="code-snippet__title">isFinished</span>(<span class="code-snippet__params">OrderStatus status</span>)</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> OrderStatus.FINISHED.<span class="code-snippet__keyword">equals</span>(status);</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">void</span> <span class="code-snippet__title">isFinished</span>(<span class="code-snippet__params">OrderStatus status</span>)</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> Objects.<span class="code-snippet__keyword">equals</span>(status, OrderStatus.FINISHED);</span></code><code><span class="code-snippet_outer">}</span> 123 <span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">void</span> <span class="code-snippet__title">isFinished</span>(<span class="code-snippet__params">OrderStatus status</span>)</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> OrderStatus.FINISHED.<span class="code-snippet__keyword">equals</span>(status);</span></code><code><span class="code-snippet_outer">}</span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">void</span> <span class="code-snippet__title">isFinished</span>(<span class="code-snippet__params">OrderStatus status</span>)</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> Objects.<span class="code-snippet__keyword">equals</span>(status, OrderStatus.FINISHED);</span></code><code><span class="code-snippet_outer">}</span> 枚举的属性字段必须是私有不可变 枚举通常被当做常量使用,如果枚举中存在公共属性字段或设置字段方法,那么这些枚举常量的属性很容易被修改。理想情况下,枚举中的属性字段是私有的,并在私有构造函数中赋值,没有对应的 Setter 方法,最好加上 final 修饰符。 反例: <span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">enum</span> UserStatus {</span></code><code><span class="code-snippet_outer"> DISABLED(<span class="code-snippet__number">0</span>, <span class="code-snippet__string">"禁用"</span>),</span></code><code><span class="code-snippet_outer"> ENABLED(<span class="code-snippet__number">1</span>, <span class="code-snippet__string">"启用"</span>);</span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">int</span> <span class="code-snippet__keyword">value</span>;</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">private</span> String description;</span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> <span class="code-snippet__function"><span class="code-snippet__keyword">private</span> <span class="code-snippet__title">UserStatus</span>(<span class="code-snippet__params"><span class="code-snippet__keyword">int</span> <span class="code-snippet__keyword">value</span>, String description</span>)</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">this</span>.<span class="code-snippet__keyword">value</span> = <span class="code-snippet__keyword">value</span>;</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">this</span>.description = description;</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> <span class="code-snippet__function"><span class="code-snippet__keyword">public</span> String <span class="code-snippet__title">getDescription</span>()</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> description;</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> <span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">void</span> <span class="code-snippet__title">setDescription</span>(<span class="code-snippet__params">String description</span>)</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">this</span>.description = description;</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer">}</span> 123456789 <span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">enum</span> UserStatus {</span></code><code><span class="code-snippet_outer"> DISABLED(<span class="code-snippet__number">0</span>, <span class="code-snippet__string">"禁用"</span>),</span></code><code><span class="code-snippet_outer"> ENABLED(<span class="code-snippet__number">1</span>, <span class="code-snippet__string">"启用"</span>);</span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">int</span> <span class="code-snippet__keyword">value</span>;</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">private</span> String description;</span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"> <span class="code-snippet__function"><span class="code-snippet__keyword">private</span> <span class="code-snippet__title">UserStatus</span>(<span class="code-snippet__params"><span class="code-snippet__keyword">int</span> <span class="code-snippet__keyword">value</span>, String description</span>)</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">this</span>.<span class="code-snippet__keyword">value</span> = <span class="code-snippet__keyword">value</span>;</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">this</span>.description = description;</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"> <span class="code-snippet__function"><span class="code-snippet__keyword">public</span> String <span class="code-snippet__title">getDescription</span>()</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> description;</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"> <span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">void</span> <span class="code-snippet__title">setDescription</span>(<span class="code-snippet__params">String description</span>)</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">this</span>.description = description;</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer">}</span> 正例: <span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">enum</span> UserStatus {</span></code><code><span class="code-snippet_outer"> DISABLED(<span class="code-snippet__number">0</span>, <span class="code-snippet__string">"禁用"</span>),</span></code><code><span class="code-snippet_outer"> ENABLED(<span class="code-snippet__number">1</span>, <span class="code-snippet__string">"启用"</span>);</span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">private</span> final <span class="code-snippet__keyword">int</span> <span class="code-snippet__keyword">value</span>;</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">private</span> final String description;</span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> <span class="code-snippet__function"><span class="code-snippet__keyword">private</span> <span class="code-snippet__title">UserStatus</span>(<span class="code-snippet__params"><span class="code-snippet__keyword">int</span> <span class="code-snippet__keyword">value</span>, String description</span>)</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">this</span>.<span class="code-snippet__keyword">value</span> = <span class="code-snippet__keyword">value</span>;</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">this</span>.description = description;</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> <span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">int</span> <span class="code-snippet__title">getValue</span>()</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> <span class="code-snippet__keyword">value</span>;</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> </span></code><code><span class="code-snippet_outer"> <span class="code-snippet__function"><span class="code-snippet__keyword">public</span> String <span class="code-snippet__title">getDescription</span>()</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> description;</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer">}</span> 123456789 <span class="code-snippet_outer"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">enum</span> UserStatus {</span></code><code><span class="code-snippet_outer"> DISABLED(<span class="code-snippet__number">0</span>, <span class="code-snippet__string">"禁用"</span>),</span></code><code><span class="code-snippet_outer"> ENABLED(<span class="code-snippet__number">1</span>, <span class="code-snippet__string">"启用"</span>);</span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">private</span> final <span class="code-snippet__keyword">int</span> <span class="code-snippet__keyword">value</span>;</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">private</span> final String description;</span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"> <span class="code-snippet__function"><span class="code-snippet__keyword">private</span> <span class="code-snippet__title">UserStatus</span>(<span class="code-snippet__params"><span class="code-snippet__keyword">int</span> <span class="code-snippet__keyword">value</span>, String description</span>)</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">this</span>.<span class="code-snippet__keyword">value</span> = <span class="code-snippet__keyword">value</span>;</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">this</span>.description = description;</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"> <span class="code-snippet__function"><span class="code-snippet__keyword">public</span> <span class="code-snippet__keyword">int</span> <span class="code-snippet__title">getValue</span>()</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> <span class="code-snippet__keyword">value</span>;</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"></span></code><code><span class="code-snippet_outer"> <span class="code-snippet__function"><span class="code-snippet__keyword">public</span> String <span class="code-snippet__title">getDescription</span>()</span> {</span></code><code><span class="code-snippet_outer"> <span class="code-snippet__keyword">return</span> description;</span></code><code><span class="code-snippet_outer"> }</span></code><code><span class="code-snippet_outer">}</span> 小心String.split(String regex) 字符串 String 的 split 方法,传入的分隔字符串是正则表达式!部分关键字(比如.[]()\| 等)需要转义。 反例: <span class="code-snippet_outer"><span class="code-snippet__string">"a.ab.abc"</span>.<span class="code-snippet__keyword">split</span>(<span class="code-snippet__string">"."</span>); <span class="code-snippet__regexp">//</span> 结果为[]</span></code><code><span class="code-snippet_outer"><span class="code-snippet__string">"a|ab|abc"</span>.<span class="code-snippet__keyword">split</span>(<span class="code-snippet__string">"|"</span>); <span class="code-snippet__regexp">//</span> 结果为[<span class="code-snippet__string">"a"</span>, <span class="code-snippet__string">"|"</span>, <span class="code-snippet__string">"a"</span>, <span class="code-snippet__string">"b"</span>, <span class="code-snippet__string">"|"</span>, <span class="code-snippet__string">"a"</span>, <span class="code-snippet__string">"b"</span>, <span class="code-snippet__string">"c"</span>]</span> 1 <span class="code-snippet_outer"><span class="code-snippet__string">"a.ab.abc"</span>.<span class="code-snippet__keyword">split</span>(<span class="code-snippet__string">"."</span>); <span class="code-snippet__regexp">//</span> 结果为[]</span></code><code><span class="code-snippet_outer"><span class="code-snippet__string">"a|ab|abc"</span>.<span class="code-snippet__keyword">split</span>(<span class="code-snippet__string">"|"</span>); <span class="code-snippet__regexp">//</span> 结果为[<span class="code-snippet__string">"a"</span>, <span class="code-snippet__string">"|"</span>, <span class="code-snippet__string">"a"</span>, <span class="code-snippet__string">"b"</span>, <span class="code-snippet__string">"|"</span>, <span class="code-snippet__string">"a"</span>, <span class="code-snippet__string">"b"</span>, <span class="code-snippet__string">"c"</span>]</span> 正例: <span class="code-snippet_outer"><span class="code-snippet__string">"a.ab.abc"</span>.<span class="code-snippet__keyword">split</span>(<span class="code-snippet__string">"\\."</span>); <span class="code-snippet__regexp">//</span> 结果为[<span class="code-snippet__string">"a"</span>, <span class="code-snippet__string">"ab"</span>, <span class="code-snippet__string">"abc"</span>]</span></code><code><span class="code-snippet_outer"><span class="code-snippet__string">"a|ab|abc"</span>.<span class="code-snippet__keyword">split</span>(<span class="code-snippet__string">"\\|"</span>); <span class="code-snippet__regexp">//</span> 结果为[<span class="code-snippet__string">"a"</span>, <span class="code-snippet__string">"ab"</span>, <span class="code-snippet__string">"abc"</span>]</span> 1 <span class="code-snippet_outer"><span class="code-snippet__string">"a.ab.abc"</span>.<span class="code-snippet__keyword">split</span>(<span class="code-snippet__string">"\\."</span>); <span class="code-snippet__regexp">//</span> 结果为[<span class="code-snippet__string">"a"</span>, <span class="code-snippet__string">"ab"</span>, <span class="code-snippet__string">"abc"</span>]</span></code><code><span class="code-snippet_outer"><span class="code-snippet__string">"a|ab|abc"</span>.<span class="code-snippet__keyword">split</span>(<span class="code-snippet__string">"\\|"</span>); <span class="code-snippet__regexp">//</span> 结果为[<span class="code-snippet__string">"a"</span>, <span class="code-snippet__string">"ab"</span>, <span class="code-snippet__string">"abc"</span>]</span> 总结 这篇文章,可以说是从事 Java 开发的经验总结,分享出来以供大家参考。希望能帮大家避免踩坑,让代码更加高效优雅。