{"id":15,"date":"2023-05-19T20:17:44","date_gmt":"2023-05-19T12:17:44","guid":{"rendered":"http:\/\/www.greatfar.cn\/?p=15"},"modified":"2023-05-19T20:17:44","modified_gmt":"2023-05-19T12:17:44","slug":"java-%e5%8c%85%e8%a3%85%e7%b1%bb%e3%80%81%e8%a3%85%e7%ae%b1%e5%92%8c%e6%8b%86%e7%ae%b1","status":"publish","type":"post","link":"https:\/\/www.greatfar.cn\/archives\/15","title":{"rendered":"Java \u5305\u88c5\u7c7b\u3001\u88c5\u7bb1\u548c\u62c6\u7bb1"},"content":{"rendered":"\n

\u5728 Java<\/a> \u7684\u8bbe\u8ba1\u4e2d\u63d0\u5021\u4e00\u79cd\u601d\u60f3\uff0c\u5373\u4e00\u5207\u7686\u5bf9\u8c61\u3002\u4f46\u662f\u4ece\u6570\u636e\u7c7b\u578b\u7684\u5212\u5206\u4e2d\uff0c\u6211\u4eec\u77e5\u9053 Java \u4e2d\u7684\u6570\u636e\u7c7b\u578b\u5206\u4e3a\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u5f15\u7528\u6570\u636e\u7c7b\u578b\uff0c\u4f46\u662f\u57fa\u672c\u6570\u636e\u7c7b\u578b\u600e\u4e48\u80fd\u591f\u79f0\u4e3a\u5bf9\u8c61\u5462\uff1f\u4e8e\u662f Java \u4e3a\u6bcf\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u5206\u522b\u8bbe\u8ba1\u4e86\u5bf9\u5e94\u7684\u7c7b\uff0c\u79f0\u4e4b\u4e3a\u5305\u88c5\u7c7b\uff08Wrapper Classes\uff09\uff0c\u4e5f\u6709\u5730\u65b9\u79f0\u4e3a\u5916\u8986\u7c7b\u6216\u6570\u636e\u7c7b\u578b\u7c7b\u3002<\/p>\n\n\n\n

\u5305\u88c5\u7c7b\u548c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u5173\u7cfb\u5982\u4e0b\u8868\u6240\u793a\u3002<\/p>\n\n\n\n

\u5e8f\u53f7<\/td>\u57fa\u672c\u6570\u636e\u7c7b\u578b<\/td>\u5305\u88c5\u7c7b<\/td><\/tr>
1<\/td>byte<\/td>Byte<\/td><\/tr>
2<\/td>short<\/td>Short<\/td><\/tr>
3<\/td>int<\/td>Integer<\/td><\/tr>
4<\/td>long<\/td>Long<\/td><\/tr>
5<\/td>char<\/td>Character<\/td><\/tr>
6<\/td>float<\/td>Float<\/td><\/tr>
7<\/td>double<\/td>Double<\/td><\/tr>
8<\/td>boolean<\/td>Boolean<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n

\u4ece\u4e0a\u8868\u4e2d\u6211\u4eec\u53ef\u4ee5\u770b\u51fa\uff0c\u9664\u4e86 Integer \u548c Character \u5b9a\u4e49\u7684\u540d\u79f0\u4e0e\u57fa\u672c\u6570\u636e\u7c7b\u578b\u5b9a\u4e49\u7684\u540d\u79f0\u76f8\u5dee\u8f83\u5927\u5916\uff0c\u5176\u5b83\u7684 6 \u79cd\u7c7b\u578b\u7684\u540d\u79f0\u90fd\u662f\u5f88\u597d\u638c\u63e1\u7684\u3002<\/p>\n\n\n\n

\u88c5\u7bb1\u548c\u62c6\u7bb1<\/strong><\/p>\n\n\n\n

\u5728\u4e86\u89e3\u5305\u88c5\u7c7b\u540e\uff0c\u4e0b\u9762\u4ecb\u7ecd\u5305\u88c5\u7c7b\u7684\u88c5\u7bb1\u4e0e\u62c6\u7bb1\u7684\u6982\u5ff5\u3002\u5176\u5b9e\u8fd9\u4e24\u4e2a\u6982\u5ff5\u672c\u8eab\u5e76\u4e0d\u96be\u7406\u89e3\uff0c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u5305\u88c5\u7c7b\u7684\u8fc7\u7a0b\u79f0\u4e3a\u88c5\u7bb1\uff0c\u4f8b\u5982\u628a int \u5305\u88c5\u6210 Integer \u7c7b\u7684\u5bf9\u8c61\uff1b\u5305\u88c5\u7c7b\u53d8\u4e3a\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u8fc7\u7a0b\u79f0\u4e3a\u62c6\u7bb1\uff0c\u4f8b\u5982\u628a Integer \u7c7b\u7684\u5bf9\u8c61\u91cd\u65b0\u7b80\u5316\u4e3a int\u3002<\/p>\n\n\n\n

\u624b\u52a8\u5b9e\u4f8b\u5316\u4e00\u4e2a\u5305\u88c5\u7c7b\u79f0\u4e3a\u624b\u52a8\u62c6\u7bb1\u88c5\u7bb1\u3002Java 1.5 \u7248\u672c\u4e4b\u524d\u5fc5\u987b\u624b\u52a8\u62c6\u7bb1\u88c5\u7bb1\uff0c\u4e4b\u540e\u53ef\u4ee5\u81ea\u52a8\u62c6\u7bb1\u88c5\u7bb1\uff0c\u4e5f\u5c31\u662f\u5728\u8fdb\u884c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u5bf9\u5e94\u7684\u5305\u88c5\u7c7b\u8f6c\u6362\u65f6\uff0c\u7cfb\u7edf\u5c06\u81ea\u52a8\u8fdb\u884c\u88c5\u7bb1\u53ca\u62c6\u7bb1\u64cd\u4f5c\uff0c\u4e0d\u7528\u5728\u8fdb\u884c\u624b\u5de5\u64cd\u4f5c\uff0c\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u65b9\u4fbf\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n

public class Demo { public static void main(String[] args) { int m = 500; Integer obj = m; \/\/ \u81ea\u52a8\u88c5\u7bb1 int n = obj; \/\/ \u81ea\u52a8\u62c6\u7bb1 System.out.println(“n = ” + n); Integer obj1 = 500; System.out.println(“obj\u7b49\u4ef7\u4e8eobj1\u8fd4\u56de\u7ed3\u679c\u4e3a” + obj.equals(obj1)); } }<\/p>\n\n\n\n

\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n

n = 500<\/p>\n\n\n\n

obj\u7b49\u4ef7\u4e8eobj1\u8fd4\u56de\u7ed3\u679c\u4e3atrue<\/p>\n\n\n\n

\u81ea\u52a8\u62c6\u7bb1\u88c5\u7bb1\u662f\u5e38\u7528\u7684\u4e00\u4e2a\u529f\u80fd\uff0c\u8bfb\u8005\u9700\u8981\u91cd\u70b9\u638c\u63e1\u3002<\/p>\n\n\n\n

\u5305\u88c5\u7c7b\u7684\u5e94\u7528<\/strong><\/p>\n\n\n\n

\u4e0b\u9762\u6211\u4eec\u8bb2\u89e3 8 \u4e2a\u5305\u88c5\u7c7b\u7684\u4f7f\u7528\uff0c\u4e0b\u9762\u662f\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n\n\n\n

1) \u5b9e\u73b0 int \u548c Integer \u7684\u76f8\u4e92\u8f6c\u6362<\/strong><\/p>\n\n\n\n

\u53ef\u4ee5\u901a\u8fc7 Integer \u7c7b\u7684\u6784\u9020\u65b9\u6cd5\u5c06 int \u88c5\u7bb1\uff0c\u901a\u8fc7 Integer \u7c7b\u7684 intValue \u65b9\u6cd5\u5c06 Integer \u62c6\u7bb1\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n

public class Demo { public static void main(String[] args) { int m = 500; Integer obj = new Integer(m); \/\/ \u624b\u52a8\u88c5\u7bb1 int n = obj.intValue(); \/\/ \u624b\u52a8\u62c6\u7bb1 System.out.println(“n = ” + n); Integer obj1 = new Integer(500); System.out.println(“obj\u7b49\u4ef7\u4e8eobj1\u7684\u8fd4\u56de\u7ed3\u679c\u4e3a” + obj.equals(obj1)); } }<\/p>\n\n\n\n

\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n

n = 500<\/p>\n\n\n\n

obj\u7b49\u4ef7\u4e8eobj1\u7684\u8fd4\u56de\u7ed3\u679c\u4e3atrue<\/p>\n\n\n\n

2) \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u503c\u7c7b\u578b<\/strong>\u5728 Integer \u548c Float \u7c7b\u4e2d\u5206\u522b\u63d0\u4f9b\u4e86\u4ee5\u4e0b\u4e24\u79cd\u65b9\u6cd5\uff1a<\/strong><\/p>\n\n\n\n

\u2460 Integer \u7c7b\uff08String \u8f6c int \u578b\uff09<\/p>\n\n\n\n

int parseInt(String s);<\/p>\n\n\n\n

s \u4e3a\u8981\u8f6c\u6362\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n

\u2461 Float \u7c7b\uff08String \u8f6c float \u578b\uff09<\/p>\n\n\n\n

float parseFloat(String s)<\/p>\n\n\n\n

\u6ce8\u610f\uff1a\u4f7f\u7528\u4ee5\u4e0a\u4e24\u79cd\u65b9\u6cd5\u65f6\uff0c\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u636e\u5fc5\u987b\u7531\u6570\u5b57\u7ec4\u6210\uff0c\u5426\u5219\u8f6c\u6362\u65f6\u4f1a\u51fa\u73b0\u7a0b\u5e8f\u9519\u8bef\u3002<\/p>\n\n\n\n

\u4e0b\u9762\u4e3a\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u503c\u7c7b\u578b\u7684\u4f8b\u5b50\uff1a<\/p>\n\n\n\n

public class Demo { public static void main(String[] args) { String str1 = “30”; String str2 = “30.3”; \/\/ \u5c06\u5b57\u7b26\u4e32\u53d8\u4e3aint\u578b int x = Integer.parseInt(str1); \/\/ \u5c06\u5b57\u7b26\u4e32\u53d8\u4e3afloat\u578b float f = Float.parseFloat(str2); System.out.println(“x = ” + x + “\uff1bf = ” + f); } }<\/p>\n\n\n\n

\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n

x = 30\uff1bf = 30.3<\/p>\n\n\n\n

3) \u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/strong><\/p>\n\n\n\n

Integer \u7c7b\u6709\u4e00\u4e2a\u9759\u6001\u7684 toString() \u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n

public class Demo { public static void main(String[] args) { int m = 500; String s = Integer.toString(m); System.out.println(“s = ” + s); } }<\/p>\n\n\n\n

\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n

s = 500<\/p>\n\n\n\n

http:\/\/c.biancheng.net\/view\/6119.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"

\u5728 Java \u7684\u8bbe\u8ba1\u4e2d\u63d0\u5021\u4e00\u79cd\u601d\u60f3\uff0c\u5373\u4e00\u5207\u7686\u5bf9\u8c61...<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.greatfar.cn\/wp-json\/wp\/v2\/posts\/15"}],"collection":[{"href":"https:\/\/www.greatfar.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.greatfar.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.greatfar.cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.greatfar.cn\/wp-json\/wp\/v2\/comments?post=15"}],"version-history":[{"count":1,"href":"https:\/\/www.greatfar.cn\/wp-json\/wp\/v2\/posts\/15\/revisions"}],"predecessor-version":[{"id":16,"href":"https:\/\/www.greatfar.cn\/wp-json\/wp\/v2\/posts\/15\/revisions\/16"}],"wp:attachment":[{"href":"https:\/\/www.greatfar.cn\/wp-json\/wp\/v2\/media?parent=15"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.greatfar.cn\/wp-json\/wp\/v2\/categories?post=15"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.greatfar.cn\/wp-json\/wp\/v2\/tags?post=15"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}