{"id":38847,"date":"2024-11-26T09:30:41","date_gmt":"2024-11-26T01:30:41","guid":{"rendered":"https:\/\/fwq.ai\/blog\/38847\/"},"modified":"2024-11-26T09:30:41","modified_gmt":"2024-11-26T01:30:41","slug":"java%e4%b8%ad%e6%95%b0%e7%bb%84%e9%95%bf%e5%ba%a6%e6%80%8e%e4%b9%88%e8%b6%85int","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/38847\/","title":{"rendered":"JAVA\u4e2d\u6570\u7ec4\u957f\u5ea6\u600e\u4e48\u8d85int"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d\u6570\u7ec4\u957f\u5ea6\u8d85\u8fc7 int \u8303\u56f4\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 long \u7c7b\u578b\u6765\u89e3\u51b3\u3002\u8fd9\u5141\u8bb8\u521b\u5efa\u975e\u5e38\u5927\u7684\u6570\u7ec4\uff0c\u907f\u514d arrayindexoutofboundsexception \u5f02\u5e38\uff0c\u4f46\u9700\u8981\u6ce8\u610f long \u7c7b\u578b\u5360\u7528\u7684\u5185\u5b58\u5f00\u9500\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/15\/2024111507183552199.jpg\" class=\"aligncenter\" title=\"JAVA\u4e2d\u6570\u7ec4\u957f\u5ea6\u600e\u4e48\u8d85int\u63d2\u56fe\" alt=\"JAVA\u4e2d\u6570\u7ec4\u957f\u5ea6\u600e\u4e48\u8d85int\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u4e2d\u6570\u7ec4\u957f\u5ea6\u7684\u7c7b\u578b\u8f6c\u6362<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong>JAVA\u4e2d\u6570\u7ec4\u957f\u5ea6\u600e\u4e48\u8d85int\uff1f<\/p>\n<p><strong>\u56de\u7b54\uff1a<\/strong>\u53ef\u4ee5\u4f7f\u7528 long \u7c7b\u578b\u6765\u4ee3\u66ff int \u7c7b\u578b\u3002<\/p>\n<p><strong>\u8be6\u7ec6\u89e3\u7b54\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>Java \u4e2d\u7684\u6570\u7ec4\u957f\u5ea6\u901a\u5e38\u4f7f\u7528 int \u7c7b\u578b\u8868\u793a\uff0cint \u7c7b\u578b\u7684\u6700\u5927\u503c\u662f 2<sup>31<\/sup> &#8211; 1\uff0c\u7ea6\u4e3a 21 \u4ebf\u3002\u5982\u679c\u6570\u7ec4\u957f\u5ea6\u8d85\u8fc7\u8fd9\u4e2a\u8303\u56f4\uff0c\u4f1a\u5bfc\u81f4 ArrayIndexOutOfBoundsException \u5f02\u5e38\u3002<\/p>\n<p>\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528 long \u7c7b\u578b\u4ee3\u66ff int \u7c7b\u578b\uff0clong \u7c7b\u578b\u7684\u6700\u5927\u503c\u662f 2<sup>63<\/sup> &#8211; 1\uff0c\u7ea6\u4e3a 922 \u4ebf\u4ebf\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u4f7f\u7528 long \u7c7b\u578b\u58f0\u660e\u6570\u7ec4\u957f\u5ea6\nlong[] longArray = new long[10000000000L];\n\n\/\/ \u83b7\u53d6\u6570\u7ec4\u957f\u5ea6\nlong length = longArray.length;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<ul>\n<li>\u5141\u8bb8\u521b\u5efa\u975e\u5e38\u5927\u7684\u6570\u7ec4\u3002<\/li>\n<li>\u6d88\u9664 ArrayIndexOutOfBoundsException \u5f02\u5e38\u7684\u98ce\u9669\u3002<\/li>\n<\/ul>\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<ul>\n<li>long \u7c7b\u578b\u5360\u7528 64 \u4f4d\u5185\u5b58\uff0c\u800c int \u7c7b\u578b\u53ea\u5360\u7528 32 \u4f4d\u3002<\/li>\n<li>\u5728\u4f7f\u7528 long \u7c7b\u578b\u65f6\uff0c\u8981\u6ce8\u610f\u6f5c\u5728\u7684\u6027\u80fd\u5f00\u9500\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fJAVA\u4e2d\u6570\u7ec4\u957f\u5ea6\u600e\u4e48\u8d85int\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>java \u4e2d\u6570\u7ec4\u957f\u5ea6\u8d85\u8fc7 int \u8303\u56f4\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 long \u7c7b\u578b\u6765\u89e3\u51b3\u3002\u8fd9\u5141\u8bb8\u521b\u5efa\u975e\u5e38\u5927\u7684\u6570\u7ec4\uff0c\u907f\u514d arrayindexoutofboundsexception \u5f02\u5e38\uff0c\u4f46\u9700\u8981\u6ce8\u610f long \u7c7b\u578b\u5360\u7528\u7684\u5185\u5b58\u5f00\u9500\u3002 Java \u4e2d\u6570\u7ec4\u957f\u5ea6\u7684\u7c7b\u578b\u8f6c\u6362 \u95ee\u9898\uff1aJAVA\u4e2d\u6570\u7ec4\u957f\u5ea6\u600e\u4e48\u8d85int\uff1f \u56de\u7b54\uff1a\u53ef\u4ee5\u4f7f\u7528 long \u7c7b\u578b\u6765\u4ee3\u66ff int \u7c7b\u578b\u3002 \u8be6\u7ec6\u89e3\u7b54\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b Java \u4e2d\u7684\u6570\u7ec4\u957f\u5ea6\u901a\u5e38\u4f7f\u7528 int \u7c7b\u578b\u8868\u793a\uff0cint \u7c7b\u578b\u7684\u6700\u5927\u503c\u662f 231 &#8211; 1\uff0c\u7ea6\u4e3a 21 \u4ebf\u3002\u5982\u679c\u6570\u7ec4\u957f\u5ea6\u8d85\u8fc7\u8fd9\u4e2a\u8303\u56f4\uff0c\u4f1a\u5bfc\u81f4 ArrayIndexOutOfBoundsException \u5f02\u5e38\u3002 \u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528 long \u7c7b\u578b\u4ee3\u66ff int \u7c7b\u578b\uff0clong \u7c7b\u578b\u7684\u6700\u5927\u503c\u662f 263 &#8211; 1\uff0c\u7ea6\u4e3a 922 \u4ebf\u4ebf\u3002 \u4ee3\u7801\u793a\u4f8b\uff1a \/\/ \u4f7f\u7528 long \u7c7b\u578b\u58f0\u660e\u6570\u7ec4\u957f\u5ea6 long[] longArray = new long[10000000000L]; \/\/ \u83b7\u53d6\u6570\u7ec4\u957f\u5ea6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-38847","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38847","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=38847"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38847\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=38847"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=38847"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=38847"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}