{"id":720,"date":"2023-03-23T21:50:23","date_gmt":"2023-03-23T13:50:23","guid":{"rendered":""},"modified":"2023-03-23T21:50:23","modified_gmt":"2023-03-23T13:50:23","slug":"Java \u65b9\u6cd5\u91cd\u8f7d","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/720.html","title":{"rendered":"Java \u65b9\u6cd5\u91cd\u8f7d"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

Java \u65b9\u6cd5\u91cd\u8f7d<\/h1>\n<\/p><\/div>\n
\n \u5982\u679c\u672a\u627e\u5230\u5339\u914d\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5219\u5c06\u4e00\u79cd\u7c7b\u578b\u9690\u5f0f\u63d0\u5347\u4e3a\u53e6\u4e00\u79cd\u7c7b\u578b\u3002\u8ba9\u6211\u4eec\u7528\u4e0b\u9762\u7684\u56fe\u6765\u7406\u89e3\u8fd9\u4e2a\u6982\u5ff5:\n <\/div>\n

Java \u65b9\u6cd5\u91cd\u8f7d_https:\/\/bianchenghao6.com_\u3010Java \u57fa\u7840\u6559\u7a0b\u3011_\u7b2c1\u5f20 <\/p>\n

\n \u5728\u4e0a\u56fe\u4e2d\uff0c\u5b57\u8282\u53ef\u4ee5\u63d0\u5347\u4e3ashort\uff0cint\uff0clong\uff0cfloat\u6216double\u3002 short\u6570\u636e\u7c7b\u578b\u53ef\u4ee5\u63d0\u5347\u4e3aint\uff0clong\uff0cfloat\u6216double\u3002\u53ef\u4ee5\u5c06char\u6570\u636e\u7c7b\u578b\u63d0\u5347\u4e3aint\uff0clong\uff0cfloat\u6216double\u7b49\u3002\n <\/div>\n

\u4f7f\u7528TypePromotion\u91cd\u8f7d\u65b9\u6cd5\u7684\u793a\u4f8b<\/h3>\n
\n
 class <\/span>OverloadingCalculation1{
    void sum(int <\/span>a,long <\/span>b){
        System.out.println<\/span>(a+b);
    }
    void sum(int <\/span>a,int <\/span>b,int <\/span>c){
        System.out.println<\/span>(a+b+c);
    }
    public <\/span>static void <\/span>main(String args[]){
        OverloadingCalculation1 obj=new OverloadingCalculation1();
        obj.sum<\/span>(20,20);
        \/\/now second int <\/span>literal will be promoted to long <\/span> obj.sum<\/span>(20,20,20);
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n
 Output:40 60
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n

\u5982\u679c\u53d1\u73b0\u5339\u914d\u9879\uff0c\u5219\u4f7f\u7528\u7c7b\u578b\u5347\u7ea7\u7684\u65b9\u6cd5\u91cd\u8f7d\u793a\u4f8b<\/h3>\n
\n \u5982\u679c\u65b9\u6cd5\u4e2d\u5b58\u5728\u5339\u914d\u7684\u7c7b\u578b\u53c2\u6570\uff0c\u5219\u4e0d\u6267\u884c\u7c7b\u578b\u5347\u7ea7\u3002\n <\/div>\n
\n
 class <\/span>OverloadingCalculation2{
    void sum(int <\/span>a,int <\/span>b){
        System.out.println<\/span>(\"int <\/span>arg method invoked\"<\/span>);
    }
    void sum(long <\/span>a,long <\/span>b){
        System.out.println<\/span>(\"long <\/span>arg method invoked\"<\/span>);
    }
    public <\/span>static void <\/span>main(String args[]){
        OverloadingCalculation2 obj=new OverloadingCalculation2();
        obj.sum<\/span>(20,20);
    \/\/now int <\/span>arg sum() method gets invoked }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n
 Output:int <\/span>arg method invoked
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u5728\u51fa\u73b0\u6b67\u4e49\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u7c7b\u578b\u63d0\u5347\u7684\u65b9\u6cd5\u91cd\u8f7d\u793a\u4f8b<\/h3>\n
\n \u5982\u679c\u65b9\u6cd5\u4e2d\u6ca1\u6709\u5339\u914d\u7684\u7c7b\u578b\u53c2\u6570\uff0c\u5e76\u4e14\u6bcf\u4e2a\u65b9\u6cd5\u90fd\u63d0\u5347\u4e86\u76f8\u4f3c\u6570\u91cf\u7684\u53c2\u6570\uff0c\u6a21\u68f1\u4e24\u53ef\u3002\n <\/div>\n
\n
 class <\/span>OverloadingCalculation3{
    void sum(int <\/span>a,long <\/span>b){
        System.out.println<\/span>(\"a method invoked\"<\/span>);
    }
    void sum(long <\/span>a,int <\/span>b){
        System.out.println<\/span>(\"b method invoked\"<\/span>);
    }
    public <\/span>static void <\/span>main(String args[]){
        OverloadingCalculation3 obj=new OverloadingCalculation3();
        obj.sum<\/span>(20,20);
    \/\/now ambiguity }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n
 Output:Compile Time Error
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6ca1\u6709\u9690\u5f0f\u53d6\u6d88\u63a8\u5e7f\u4e00\u79cd\u7c7b\u578b\uff0c\u4f8b\u5982\uff0c\u4e0d\u80fd\u5c06double\u9690\u5f0f\u53d6\u6d88\u63a8\u5e7f\u4e3a\u4efb\u4f55\u7c7b\u578b\u3002\n <\/div>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Java \u65b9\u6cd5\u91cd\u8f7dzh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[111],"tags":[],"class_list":["post-720","post","type-post","status-publish","format-standard","hentry","category-java-jichu"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/720"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=720"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/720\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}