{"id":2202,"date":"2025-03-02T10:25:13","date_gmt":"2025-03-02T02:25:13","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2202"},"modified":"2025-03-02T10:25:13","modified_gmt":"2025-03-02T02:25:13","slug":"java-number-math-%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/02\/java-number-math-%e7%b1%bb\/","title":{"rendered":"Java Number &amp; Math \u7c7b"},"content":{"rendered":"\n<p>\u5728 Java \u4e2d\uff0c<code>Number<\/code>&nbsp;\u7c7b\u548c&nbsp;<code>Math<\/code>&nbsp;\u7c7b\u90fd\u662f\u5e38\u7528\u7684\u7c7b\uff0c\u5206\u522b\u5904\u7406\u6570\u503c\u548c\u6570\u5b66\u8ba1\u7b97\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. <code>Number<\/code> \u7c7b<\/strong><\/h3>\n\n\n\n<p><code>Number<\/code> \u662f Java \u4e2d\u6240\u6709\u6570\u5b57\u7c7b\u578b\uff08<code>Byte<\/code>\u3001<code>Short<\/code>\u3001<code>Integer<\/code>\u3001<code>Long<\/code>\u3001<code>Float<\/code>\u3001<code>Double<\/code>\uff09\u7684\u7236\u7c7b\u3002\u5b83\u672c\u8eab\u662f\u4e00\u4e2a\u62bd\u8c61\u7c7b\uff0c\u56e0\u6b64\u4f60\u65e0\u6cd5\u76f4\u63a5\u521b\u5efa <code>Number<\/code> \u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u5b83\u4e3b\u8981\u5b9a\u4e49\u4e86\u6570\u5b57\u8f6c\u6362\u7684\u65b9\u6cd5\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u5728\u5176\u5b50\u7c7b\uff08\u5982 <code>Integer<\/code>\u3001<code>Double<\/code> \u7b49\uff09\u4e2d\u90fd\u6709\u5b9e\u73b0\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1.1 <code>Number<\/code> \u7c7b\u7684\u65b9\u6cd5<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>byteValue()<\/code>: \u8fd4\u56de\u6570\u5b57\u7684 <code>byte<\/code> \u7c7b\u578b\u3002<\/li>\n\n\n\n<li><code>shortValue()<\/code>: \u8fd4\u56de\u6570\u5b57\u7684 <code>short<\/code> \u7c7b\u578b\u3002<\/li>\n\n\n\n<li><code>intValue()<\/code>: \u8fd4\u56de\u6570\u5b57\u7684 <code>int<\/code> \u7c7b\u578b\u3002<\/li>\n\n\n\n<li><code>longValue()<\/code>: \u8fd4\u56de\u6570\u5b57\u7684 <code>long<\/code> \u7c7b\u578b\u3002<\/li>\n\n\n\n<li><code>floatValue()<\/code>: \u8fd4\u56de\u6570\u5b57\u7684 <code>float<\/code> \u7c7b\u578b\u3002<\/li>\n\n\n\n<li><code>doubleValue()<\/code>: \u8fd4\u56de\u6570\u5b57\u7684 <code>double<\/code> \u7c7b\u578b\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u4e9b\u65b9\u6cd5\u5141\u8bb8\u4f60\u5c06\u4e00\u4e2a <code>Number<\/code> \u7c7b\u578b\u7684\u5bf9\u8c61\u8f6c\u6362\u4e3a\u4e0d\u540c\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u5b9e\u4f8b\uff1a<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>public class NumberExample {\n    public static void main(String&#91;] args) {\n        Number num1 = new Integer(42);  \/\/ \u521b\u5efa Integer \u7c7b\u578b\u7684 Number\n        Number num2 = new Double(10.5); \/\/ \u521b\u5efa Double \u7c7b\u578b\u7684 Number\n\n        \/\/ \u8f6c\u6362\u4e3a\u4e0d\u540c\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\n        System.out.println(\"num1 as byte: \" + num1.byteValue());   \/\/ \u8f93\u51fa 42\n        System.out.println(\"num1 as short: \" + num1.shortValue()); \/\/ \u8f93\u51fa 42\n        System.out.println(\"num1 as int: \" + num1.intValue());     \/\/ \u8f93\u51fa 42\n        System.out.println(\"num1 as long: \" + num1.longValue());   \/\/ \u8f93\u51fa 42\n        System.out.println(\"num1 as float: \" + num1.floatValue()); \/\/ \u8f93\u51fa 42.0\n        System.out.println(\"num1 as double: \" + num1.doubleValue()); \/\/ \u8f93\u51fa 42.0\n\n        System.out.println(\"num2 as byte: \" + num2.byteValue());   \/\/ \u8f93\u51fa 10\n        System.out.println(\"num2 as short: \" + num2.shortValue()); \/\/ \u8f93\u51fa 10\n        System.out.println(\"num2 as int: \" + num2.intValue());     \/\/ \u8f93\u51fa 10\n        System.out.println(\"num2 as long: \" + num2.longValue());   \/\/ \u8f93\u51fa 10\n        System.out.println(\"num2 as float: \" + num2.floatValue()); \/\/ \u8f93\u51fa 10.5\n        System.out.println(\"num2 as double: \" + num2.doubleValue()); \/\/ \u8f93\u51fa 10.5\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>num1 as byte: 42\nnum1 as short: 42\nnum1 as int: 42\nnum1 as long: 42\nnum1 as float: 42.0\nnum1 as double: 42.0\nnum2 as byte: 10\nnum2 as short: 10\nnum2 as int: 10\nnum2 as long: 10\nnum2 as float: 10.5\nnum2 as double: 10.5<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u603b\u7ed3\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Number<\/code> \u7c7b\u63d0\u4f9b\u4e86\u5c06\u6570\u5b57\u7c7b\u578b\u8f6c\u6362\u4e3a\u5176\u4ed6\u57fa\u672c\u7c7b\u578b\u7684\u65b9\u6cd5\u3002\u5b83\u672c\u8eab\u4e0d\u80fd\u5b9e\u4f8b\u5316\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u5b50\u7c7b\uff08\u5982 <code>Integer<\/code>\u3001<code>Double<\/code>\uff09\u6765\u521b\u5efa\u5bf9\u8c61\uff0c\u5e76\u8c03\u7528\u5176\u8f6c\u6362\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. <code>Math<\/code> \u7c7b<\/strong><\/h3>\n\n\n\n<p><code>Math<\/code> \u662f\u4e00\u4e2a\u5305\u542b\u5927\u91cf\u5e38\u7528\u6570\u5b66\u51fd\u6570\u7684\u5de5\u5177\u7c7b\uff0c\u6240\u6709\u7684\u65b9\u6cd5\u90fd\u662f\u9759\u6001\u7684\uff0c\u56e0\u6b64\u4f60\u4e0d\u9700\u8981\u521b\u5efa <code>Math<\/code> \u5bf9\u8c61\u5c31\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.1 \u5e38\u7528\u65b9\u6cd5\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Math.abs(x)<\/code>: \u8fd4\u56de <code>x<\/code> \u7684\u7edd\u5bf9\u503c\u3002<\/li>\n\n\n\n<li><code>Math.max(x, y)<\/code>: \u8fd4\u56de <code>x<\/code> \u548c <code>y<\/code> \u4e2d\u8f83\u5927\u7684\u4e00\u4e2a\u3002<\/li>\n\n\n\n<li><code>Math.min(x, y)<\/code>: \u8fd4\u56de <code>x<\/code> \u548c <code>y<\/code> \u4e2d\u8f83\u5c0f\u7684\u4e00\u4e2a\u3002<\/li>\n\n\n\n<li><code>Math.pow(x, y)<\/code>: \u8fd4\u56de <code>x<\/code> \u7684 <code>y<\/code> \u6b21\u65b9\u3002<\/li>\n\n\n\n<li><code>Math.sqrt(x)<\/code>: \u8fd4\u56de <code>x<\/code> \u7684\u5e73\u65b9\u6839\u3002<\/li>\n\n\n\n<li><code>Math.random()<\/code>: \u8fd4\u56de\u4e00\u4e2a 0.0 \u5230 1.0 \u4e4b\u95f4\u7684\u968f\u673a\u6570\u3002<\/li>\n\n\n\n<li><code>Math.round(x)<\/code>: \u8fd4\u56de <code>x<\/code> \u56db\u820d\u4e94\u5165\u540e\u7684\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u4e9b\u65b9\u6cd5\u901a\u5e38\u7528\u4e8e\u57fa\u7840\u7684\u6570\u5b66\u8ba1\u7b97\u3001\u751f\u6210\u968f\u673a\u6570\u3001\u4ee5\u53ca\u6267\u884c\u6570\u5b66\u8fd0\u7b97\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u5b9e\u4f8b\uff1a<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>public class MathExample {\n    public static void main(String&#91;] args) {\n        \/\/ 1. Math.abs()\uff1a\u8fd4\u56de\u7edd\u5bf9\u503c\n        System.out.println(\"Math.abs(-10): \" + Math.abs(-10));  \/\/ \u8f93\u51fa 10\n        System.out.println(\"Math.abs(10): \" + Math.abs(10));    \/\/ \u8f93\u51fa 10\n\n        \/\/ 2. Math.max()\uff1a\u8fd4\u56de\u8f83\u5927\u7684\u503c\n        System.out.println(\"Math.max(10, 20): \" + Math.max(10, 20)); \/\/ \u8f93\u51fa 20\n        System.out.println(\"Math.max(-5, -10): \" + Math.max(-5, -10)); \/\/ \u8f93\u51fa -5\n\n        \/\/ 3. Math.min()\uff1a\u8fd4\u56de\u8f83\u5c0f\u7684\u503c\n        System.out.println(\"Math.min(10, 20): \" + Math.min(10, 20)); \/\/ \u8f93\u51fa 10\n        System.out.println(\"Math.min(-5, -10): \" + Math.min(-5, -10)); \/\/ \u8f93\u51fa -10\n\n        \/\/ 4. Math.pow()\uff1a\u8ba1\u7b97\u5e42\n        System.out.println(\"Math.pow(2, 3): \" + Math.pow(2, 3)); \/\/ \u8f93\u51fa 8.0\n        System.out.println(\"Math.pow(5, 2): \" + Math.pow(5, 2)); \/\/ \u8f93\u51fa 25.0\n\n        \/\/ 5. Math.sqrt()\uff1a\u8ba1\u7b97\u5e73\u65b9\u6839\n        System.out.println(\"Math.sqrt(16): \" + Math.sqrt(16)); \/\/ \u8f93\u51fa 4.0\n        System.out.println(\"Math.sqrt(25): \" + Math.sqrt(25)); \/\/ \u8f93\u51fa 5.0\n\n        \/\/ 6. Math.random()\uff1a\u751f\u6210\u968f\u673a\u6570\n        System.out.println(\"Math.random(): \" + Math.random()); \/\/ \u8f93\u51fa 0.0 \u5230 1.0 \u4e4b\u95f4\u7684\u968f\u673a\u6570\n\n        \/\/ 7. Math.round()\uff1a\u56db\u820d\u4e94\u5165\n        System.out.println(\"Math.round(10.4): \" + Math.round(10.4)); \/\/ \u8f93\u51fa 10\n        System.out.println(\"Math.round(10.6): \" + Math.round(10.6)); \/\/ \u8f93\u51fa 11\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Math.abs(-10): 10\nMath.abs(10): 10\nMath.max(10, 20): 20\nMath.max(-5, -10): -5\nMath.min(10, 20): 10\nMath.min(-5, -10): -10\nMath.pow(2, 3): 8.0\nMath.pow(5, 2): 25.0\nMath.sqrt(16): 4.0\nMath.sqrt(25): 5.0\nMath.random(): 0.123456789\nMath.round(10.4): 10\nMath.round(10.6): 11<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u603b\u7ed3\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Math<\/code> \u7c7b\u662f\u4e00\u4e2a\u5de5\u5177\u7c7b\uff0c\u63d0\u4f9b\u4e86\u5e38\u89c1\u7684\u6570\u5b66\u8fd0\u7b97\u65b9\u6cd5\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u5176\u9759\u6001\u65b9\u6cd5\u8fdb\u884c\u5404\u79cd\u6570\u5b66\u8ba1\u7b97\u3002<\/li>\n\n\n\n<li>\u8fd9\u4e9b\u65b9\u6cd5\u4e0d\u9700\u8981\u521b\u5efa <code>Math<\/code> \u7c7b\u7684\u5b9e\u4f8b\uff0c\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7 <code>Math<\/code> \u7c7b\u6765\u8c03\u7528\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u7ed3\u5408\u4f7f\u7528\u5b9e\u4f8b\uff1a<\/strong><\/h3>\n\n\n\n<p>\u5047\u8bbe\u4f60\u6b63\u5728\u5f00\u53d1\u4e00\u4e2a\u5e94\u7528\uff0c\u9700\u8981\u8ba1\u7b97\u67d0\u4e2a\u6570\u503c\u7684\u5e73\u65b9\u6839\uff0c\u5e76\u4e14\u56db\u820d\u4e94\u5165\u5230\u6700\u63a5\u8fd1\u7684\u6574\u6570\uff0c\u7136\u540e\u5c06\u8fd9\u4e2a\u7ed3\u679c\u8f6c\u6362\u4e3a <code>int<\/code> \u7c7b\u578b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class CombinedExample {\n    public static void main(String&#91;] args) {\n        Number num = new Double(25.5);\n\n        \/\/ 1. \u4f7f\u7528 Math.sqrt() \u8ba1\u7b97\u5e73\u65b9\u6839\n        double sqrtValue = Math.sqrt(num.doubleValue());\n        System.out.println(\"Square root of \" + num.doubleValue() + \" is: \" + sqrtValue);\n\n        \/\/ 2. \u4f7f\u7528 Math.round() \u56db\u820d\u4e94\u5165\n        long roundedValue = Math.round(sqrtValue);\n        System.out.println(\"Rounded value: \" + roundedValue);\n\n        \/\/ 3. \u8f6c\u6362\u4e3a int \u7c7b\u578b\n        int intValue = (int) roundedValue;\n        System.out.println(\"Final int value: \" + intValue);\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Square root of 25.5 is: 5.049752470138244\nRounded value: 5\nFinal int value: 5<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>Number<\/code> \u7c7b<\/strong>\uff1a\u662f\u6240\u6709\u6570\u5b57\u7c7b\u7684\u7236\u7c7b\uff0c\u63d0\u4f9b\u4e86\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u4e0d\u540c\u7c7b\u578b\u7684\u529f\u80fd\u3002<\/li>\n\n\n\n<li><strong><code>Math<\/code> \u7c7b<\/strong>\uff1a\u63d0\u4f9b\u4e86\u5404\u79cd\u5e38\u7528\u7684\u6570\u5b66\u8ba1\u7b97\u65b9\u6cd5\uff0c\u5305\u62ec\u7edd\u5bf9\u503c\u3001\u6700\u5927\u503c\u3001\u6700\u5c0f\u503c\u3001\u5e42\u8fd0\u7b97\u3001\u5e73\u65b9\u6839\u3001\u968f\u673a\u6570\u548c\u56db\u820d\u4e94\u5165\u7b49\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u4e24\u4e2a\u7c7b\u5728\u65e5\u5e38\u5f00\u53d1\u4e2d\u975e\u5e38\u5b9e\u7528\uff0c\u80fd\u591f\u7b80\u5316\u5f88\u591a\u6570\u503c\u548c\u6570\u5b66\u8ba1\u7b97\u4efb\u52a1\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\uff0c\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Java \u4e2d\uff0cNumber&nbsp;\u7c7b\u548c&nbsp;Math&nbsp;\u7c7b\u90fd\u662f\u5e38\u7528\u7684\u7c7b\uff0c\u5206\u522b\u5904\u7406\u6570\u503c\u548c\u6570 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[68],"tags":[],"class_list":["post-2202","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2202","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2202"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2202\/revisions"}],"predecessor-version":[{"id":2203,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2202\/revisions\/2203"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}