﻿{"id":455,"date":"2017-08-29T20:36:59","date_gmt":"2017-08-29T12:36:59","guid":{"rendered":"http:\/\/www.chenweikang.top\/?p=455"},"modified":"2017-09-19T11:36:08","modified_gmt":"2017-09-19T03:36:08","slug":"java%e8%af%ad%e6%b3%95%e7%b3%96%e7%9a%84%e5%91%b3%e9%81%93","status":"publish","type":"post","link":"https:\/\/www.chenweikang.top\/?p=455","title":{"rendered":"java\u8bed\u6cd5\u7cd6\u7684\u5473\u9053"},"content":{"rendered":"<blockquote><p>\u8bed\u6cd5\u7cd6\uff08Syntactic Sugar\uff09\uff1a\u4e5f\u79f0\u7cd6\u8863\u8bed\u6cd5\uff0c\u6307\u5728\u8ba1\u7b97\u673a\u8bed\u8a00\u4e2d\u6dfb\u52a0\u7684\u67d0\u79cd\u8bed\u6cd5\uff0c\u8fd9\u79cd\u8bed\u6cd5\u5bf9\u8bed\u8a00\u7684\u529f\u80fd\u6ca1\u6709\u5f71\u54cd\uff0c\u4f46\u662f\u66f4\u65b9\u4fbf\u7a0b\u5e8f\u5458\u4f7f\u7528\u3002\u901a\u5e38\u6765\u8bf4\uff0c\u4f7f\u7528\u8bed\u6cd5\u7cd6\u80fd\u591f\u589e\u52a0\u7a0b\u5e8f\u7684\u53ef\u8bfb\u6027\uff0c\u51cf\u5c11\u7a0b\u5e8f\u4ee3\u7801\u51fa\u9519\u7684\u673a\u4f1a\u3002<\/p>\n<p>Java\u4e2d\u7684\u8bed\u6cd5\u7cd6\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\u4ee5\u4e0b10\u9897\uff1a\u6cdb\u578b\u4e0e\u7c7b\u578b\u64e6\u9664\u3001\u81ea\u52a8\u88c5\u7bb1\u548c\u62c6\u7bb1\u3001\u904d\u5386\u5faa\u73af\u3001\u53d8\u957f\u53c2\u6570\u3001\u6761\u4ef6\u7f16\u8bd1\u3001\u5185\u90e8\u7c7b\u3001\u679a\u4e3e\u7c7b\u3001\u65ad\u8a00\u8bed\u53e5\u3001\u5bf9\u679a\u4e3e\u548c\u5b57\u7b26\u4e32\u7684switch\u652f\u6301\u3001\u5728try\u8bed\u53e5\u4e2d\u5b9a\u4e49\u548c\u5173\u95ed\u8d44\u6e90\u3002<\/p><\/blockquote>\n<h2>1.\u6cdb\u578b\u4e0e\u7c7b\u578b\u64e6\u9664<\/h2>\n<h3>\u6e90\u4ee3\u7801\uff1a<\/h3>\n<pre class=\"prettyprint linenums\">public static void main(String[] args){\r\n\tMap&lt;String,Integer&gt; map = new HashMap&lt;String,Integer&gt;();\r\n\tmap.put(\"hello\" , 1);\r\n\tmap.put(\"world\" , 2);\r\n\tSystem.out.println(map.get(\"hello\"));\r\n\tSystem.out.println(map.get(\"world\"));\r\n}<\/pre>\n<h3>\u7f16\u8bd1\u540e\u7684class\u6587\u4ef6\uff1a<\/h3>\n<pre class=\"prettyprint linenums\">  public static void main(String[] paramArrayOfString)\r\n  {\r\n    HashMap localHashMap = new HashMap();\r\n    localHashMap.put(\"hello\", Integer.valueOf(1));\r\n    localHashMap.put(\"world\", Integer.valueOf(2));\r\n    System.out.println(localHashMap.get(\"hello\"));\r\n    System.out.println(localHashMap.get(\"world\"));\r\n  }\r\n<\/pre>\n<p>\u5728\u7f16\u8bd1\u540e\u7684\u5b57\u8282\u7801\u4e2d\uff0c\u5df2\u7ecf\u88ab\u66ff\u6362\u4e3a\u539f\u6765\u7684\u539f\u751f\u7c7b\u578b\u4e86\uff0c\u6240\u4ee5\u6709\u4eba\u8bf4java\u7684\u6cdb\u578b\u662f\u4f2a\u6cdb\u578b<\/p>\n<h2>2.\u81ea\u52a8\u62c6\u88c5\u7bb1<\/h2>\n<h3>\u6e90\u4ee3\u7801\uff1a<\/h3>\n<pre class=\"prettyprint linenums\">public static void main(String[] args) {\r\n\tInteger a = 1;\r\n\tInteger b = 2;\r\n\tInteger c = 3;\r\n\tInteger d = 3;\r\n\tInteger e = 321;\r\n\tInteger f = 321;\r\n\tLong g = 3L;\r\n\t\t\r\n\tSystem.out.println(c ==d ); \/\/true\r\n\tSystem.out.println(e ==f ); \/\/false\r\n\tSystem.out.println(c == (a+b)); \/\/true\r\n\tSystem.out.println(c.equals(a+b)); \/\/true\r\n\tSystem.out.println(g == (a+b)); \/\/true\r\n\tSystem.out.println(g.equals(a + b)); \/\/false\r\n}\r\n<\/pre>\n<h3>\u7f16\u8bd1\u540e\u7684class\u6587\u4ef6\uff1a<\/h3>\n<pre class=\"prettyprint linenums\">  public static void main(String[] args)\r\n  {\r\n    Integer a = Integer.valueOf(1);\r\n    Integer b = Integer.valueOf(2);\r\n    Integer c = Integer.valueOf(3);\r\n    Integer d = Integer.valueOf(3);\r\n    Integer e = Integer.valueOf(321);\r\n    Integer f = Integer.valueOf(321);\r\n    Long g = Long.valueOf(3L);\r\n    \r\n    System.out.println(c == d);\r\n    System.out.println(e == f);\r\n    System.out.println(c.intValue() == a.intValue() + b.intValue());\r\n    System.out.println(c.equals(Integer.valueOf(a.intValue() + b.intValue())));\r\n    System.out.println(g.longValue() == a.intValue() + b.intValue());\r\n    System.out.println(g.equals(Integer.valueOf(a.intValue() + b.intValue())));\r\n  }\r\n<\/pre>\n<p>\u5305\u88c5\u7c7b\u7684\"==\"\u8fd0\u7b97 \u5728\u4e0d\u9047\u5230\u7b97\u6570\u8fd0\u7b97\u7684\u60c5\u51b5\u4e0b\u4e0d\u4f1a\u81ea\u52a8\u62c6\u7bb1\uff0cequals\u4e5f\u4e0d\u4f1a\u5904\u7406\u6570\u636e\u8f6c\u578b<\/p>\n<h2>3.\u6761\u4ef6\u7f16\u8bd1<\/h2>\n<h3>\u6e90\u4ee3\u7801\uff1a<\/h3>\n<pre class=\"prettyprint linenums\">public static void main(String[] args){\r\n\tif(true){\r\n\t\tSystem.out.print(\"a\");\r\n\t}else{\r\n\t        System.out.print(\"b\");\r\n\t}\r\n}\r\n<\/pre>\n<h3>\u7f16\u8bd1\u540e\u7684class\u6587\u4ef6\uff1a<\/h3>\n<pre class=\"prettyprint linenums\"> public static void main(String[] paramArrayOfString) {\r\n    System.out.print(\"a\");\r\n  }\r\n}\r\n<\/pre>\n<p>\u6761\u4ef6\u7f16\u8bd1\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u6d88\u9664\u6e90\u4ee3\u7801\u4e2d\u7684\u4e00\u4e9b\u6b7b\u4ee3\u7801<\/p>\n<h2>4.\u53d8\u957f\u53c2\u6570<\/h2>\n<h3>\u6e90\u4ee3\u7801\uff1a<\/h3>\n<pre class=\"prettyprint linenums\">       public static void main(String[] args) {\r\n\t\tDemo5.printParams(1, 2, 3, 4, 5); \r\n\t}\r\n\tpublic static void printParams(Integer... a) {\r\n\t\tInteger[] as = a;\r\n\t\tfor(Integer i : as){\r\n\t\t\tSystem.out.println(i);\r\n\t\t}\r\n\t}\r\n<\/pre>\n<h3>\u7f16\u8bd1\u540e\u7684class\u6587\u4ef6\uff1a<\/h3>\n<pre class=\"prettyprint linenums\">  public static void main(String[] paramArrayOfString){\r\n    printParams(new Integer[] { Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3), Integer.valueOf(4), Integer.valueOf(5) });\r\n  }\r\n  \r\n  public static void printParams(Integer... paramVarArgs){\r\n    Integer[] arrayOfInteger1 = paramVarArgs;\r\n    for (Integer localInteger : arrayOfInteger1) {\r\n      System.out.println(localInteger);\r\n    }\r\n  }\r\n<\/pre>\n<h2>5.\u904d\u5386\u4e0e\u5faa\u73af<\/h2>\n<p>\u6e90\u4ee3\u7801\uff1a<\/p>\n<pre class=\"prettyprint linenums\"> public static void main(String[] args){\r\n\tList  list1 = Arrays.asList(1,2,3,4,5);\r\n\tint sum = 0;\r\n\tfor(int i : list1){\r\n\t      sum += i;\r\n\t}\r\n\tSystem.out.println(\"sum is :\"+sum);\r\n}\r\n<\/pre>\n<p>\u7f16\u8bd1\u540e\u7684class\u6587\u4ef6\uff1a<\/p>\n<pre class=\"prettyprint linenums\"> public static void main(String[] paramArrayOfString){\r\n    List localList = Arrays.asList(new Integer[] { Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3), Integer.valueOf(4), Integer.valueOf(5) });\r\n    int i = 0;\r\n    for (Iterator localIterator = localList.iterator(); localIterator.hasNext();){\r\n      int j = ((Integer)localIterator.next()).intValue();\r\n      i += j;\r\n    }\r\n    System.out.println(\"sum is :\" + i);\r\n  }\r\n<\/pre>\n<p>\u4ece\u7f16\u8bd1\u540e\u7684\u4ee3\u7801\u6765\u770bforeach\u5728\u904d\u5386\u65f6\u5b9e\u9645\u4e0a\u8fd8\u662f\u8c03\u7528\u4e86\u5e95\u5c42\u7684\u8fed\u4ee3\u65b9\u6cd5<\/p>\n<h2>6.\u5185\u90e8\u7c7b<\/h2>\n<p>\u6e90\u4ee3\u7801\uff1a<\/p>\n<pre class=\"prettyprint linenums\">public class Demo8 {\r\n       class Demo8_1{\r\n\t      private String name=\"\";\r\n\t\tDemo8_1(String name){\r\n\t\t\tthis.name = name;\r\n\t      }\r\n       }\t\r\n}\r\n<\/pre>\n<p>\u7f16\u8bd1\u540e\u7684class\u6587\u4ef6\uff1a<\/p>\n<pre class=\"prettyprint linenums\">\u6709\u5185\u90e8\u7c7b\u4f1a\u7f16\u8bd1\u51fa\u591a\u4e2aclass\u6587\u4ef6\r\n<strong>Demo8.class:<\/strong>\r\npublic class Demo8{\r\n  class Demo8_1{\r\n    private String name = \"\";\r\n    \r\n    Demo8_1(String name){\r\n      this.name = name;\r\n    }\r\n  }\r\n}\r\n<strong>Demo8$Demo8_1.class\uff1a<\/strong>\r\nclass Demo8$Demo8_1{\r\n  private String name = \"\";\r\n  Demo8$Demo8_1(Demo8 paramDemo8, String name){\r\n    this.name = name;\r\n  }\r\n}\r\n<\/pre>\n<h2>7.\u5bf9\u5b57\u7b26\u4e32\u7684switch\u652f\u6301(jdk1.7)<\/h2>\n<p>\u6e90\u4ee3\u7801\uff1a<\/p>\n<pre class=\"prettyprint linenums\">public static void stringSwitch() {\r\n\tString str = \"a\";\r\n\t    switch (str) {\r\n\t             case \"a\":\r\n\t\t\tSystem.out.println(\"a\");\r\n\t\t\tbreak;\r\n\t\t     case \"b\":\r\n\t\t\tSystem.out.println(\"b\");\r\n\t\t\tbreak;\r\n\t\t      default:\r\n\t\t\tSystem.out.println(\"default\");\r\n\t\t\tbreak;\r\n\t}\r\n}\r\n<\/pre>\n<p>\u7f16\u8bd1\u540e\u7684class\u6587\u4ef6\uff1a<\/p>\n<pre class=\"prettyprint linenums\">  public static void stringSwitch() {\r\n    String str = \"a\";\r\n    String str1 = str;\r\n    switch (str.hashCode()){\r\n    case 97: \r\n      if (str1.equals(\"a\")) {\r\n        break;\r\n      }\r\n      break;\r\n    case 98: \r\n      if (!str1.equals(\"b\")){\r\n        break label82;\r\n        System.out.println(\"a\");\r\n        return;\r\n      }else {\r\n        System.out.println(\"b\");\r\n      }\r\n      break;\r\n    }\r\n    label82:\r\n    System.out.println(\"default\");\r\n   }\r\n<\/pre>\n<h2>8.\u81ea\u52a8\u4e3atry\u4ee3\u7801\u5feb\u4e2d\u7684\u8d44\u6e90\u8fdb\u884c\u5173\u95ed(jdk7\u4e2d\u4e3a\u5927\u591a\u6570\u8d44\u6e90\u5bf9\u8c61\u5b9e\u73b0\u4e86AutoCloseable\u63a5\u53e3)<\/h2>\n<p>\u6e90\u4ee3\u7801\uff1a<\/p>\n<pre class=\"prettyprint linenums\">     public static String readFirstLineFromFile(String path) throws IOException{\r\n\t\ttry(BufferedReader br=new BufferedReader(new FileReader(path))){\r\n\t\t\treturn br.readLine();\r\n\t\t}\r\n\t}\r\n<\/pre>\n<p>\u7f16\u8bd1\u540e\u7684class\u6587\u4ef6\uff1a<\/p>\n<pre class=\"prettyprint linenums\">public static String readFirstLineFromFile(String path)\r\n    throws IOException{\r\n    Object localObject1 = null;Object localObject4 = null;\r\n    Object localObject3;\r\n    try{\r\n      BufferedReader br = new BufferedReader(new FileReader(path));\r\n      try {\r\n        return br.readLine();\r\n      }\r\n      finally {\r\n        if (br != null) {\r\n          br.close();\r\n        }\r\n      }\r\n    }\r\n    finally {\r\n      if (localObject2 == null) {\r\n        localObject3 = localThrowable;\r\n      } else if (localObject3 != localThrowable) {\r\n        localObject3.addSuppressed(localThrowable);\r\n      }\r\n    }\r\n  }\r\n<\/pre>\n<p>&nbsp;<\/p>\n<ul>\n<li>\u53c2\u8003\u8d44\u6599\uff1a\u300a\u6df1\u5165\u7406\u89e3Java\u865a\u62df\u673a\u300b<\/li>\n<li>\u53cd\u7f16\u8bd1\u5de5\u5177\uff1aJD-JUI<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1a<a href=\"https:\/\/www.chenweikang.top\">\u5de6\u624b\u4ee3\u7801\u53f3\u624b\u8bd7<\/a> &raquo; <a href=\"https:\/\/www.chenweikang.top\/?p=455\">java\u8bed\u6cd5\u7cd6\u7684\u5473\u9053<\/a><\/p><div class=\"__youshang\">\r\n            <div id=\"__youshang_popup\" class=\"wechat popup\" style=\"display: none;\">\r\n                <div class=\"head\">~\u8c22\u8c22\u6253\u8d4f~<\/div>\r\n                <div class=\"qrcode\"><div class=\"qrcode-li wechat\" ><img src=\"https:\/\/www.chenweikang.top\/wp-content\/uploads\/2019\/07\/wexin.png\" \/><\/div><div class=\"qrcode-li alipay\" style=\"display:none;\"><img src=\"https:\/\/www.chenweikang.top\/wp-content\/uploads\/2019\/07\/ali-pay.png\" \/><\/div><div class=\"qrcode-li hongbao\" style=\"display:none;\"><img src=\"https:\/\/www.chenweikang.top\/wp-content\/uploads\/2019\/07\/ali-hongbao.png\" \/><\/div><\/div>\r\n                <ul class=\"platform\"><li class=\"icon-wechat active\" data-bg-color=\"#05af4e\" data-thanks=\"~\u8c22\u8c22\u6253\u8d4f~\"><\/li><li class=\"icon-alipay \" data-bg-color=\"#00a2ea\" data-thanks=\"~\u8c22\u8c22\u6253\u8d4f~\"><\/li><li class=\"icon-hongbao \" data-bg-color=\"#dd5746\" data-thanks=\"<p>\u626b\u7801\u9886\u7ea2\u5305<\/p><p style='margin-top: 24px;'>\uff08\u4f59\u989d\u5b9d\u652f\u4ed8\u65f6\u53ef\u62b5\u73b0\uff09<\/p>\"><\/li><\/ul>\r\n            <\/div>\r\n            <a href=\"javascript:void(0);\" id=\"__youshang_btn\">\u8d4f<\/a>\r\n        <\/div>","protected":false},"excerpt":{"rendered":"<p>\u8bed\u6cd5\u7cd6\uff08Syntactic Sugar\uff09\uff1a\u4e5f\u79f0\u7cd6\u8863\u8bed\u6cd5\uff0c\u6307\u5728\u8ba1\u7b97\u673a\u8bed\u8a00\u4e2d\u6dfb\u52a0\u7684\u67d0\u79cd\u8bed\u6cd5\uff0c\u8fd9\u79cd\u8bed\u6cd5\u5bf9\u8bed\u8a00\u7684\u529f\u80fd\u6ca1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":137,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8,3],"tags":[20],"class_list":["post-455","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","category-note","tag-java"],"_links":{"self":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/posts\/455","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=455"}],"version-history":[{"count":0,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/posts\/455\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/media\/137"}],"wp:attachment":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=455"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}