[問題] Number Wrapper & method overloading

看板java作者 (pcman)時間8年前 (2017/02/15 19:12), 8年前編輯推噓1(102)
留言3則, 2人參與, 最新討論串1/1
public class Prac extends Test2{ public static void go(Long n){ System.out.println("Long "); } public static void go(Short n){ System.out.println("Short "); } public static void go(int n){ System.out.println("int "); } public static void main(String[] args) throws IOException{ short y = 6; long z = 7; go(y); go(z); } } --------------------------------------------------------------------- What is the result? Answer: int Long 為何是這樣,我本來以為會autoboxing變成 Short? -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 101.13.4.123 ※ 文章網址: https://www.ptt.cc/bbs/java/M.1487157143.A.9F8.html ※ 編輯: uopsdod (101.13.4.123), 02/15/2017 19:12:58

02/15 22:36, , 1F
main裡面的short是基本型態 比起自動裝箱 Java會先尋找自
02/15 22:36, 1F

02/15 22:36, , 2F
動寬化後符合的型態吧
02/15 22:36, 2F

02/19 18:41, , 3F
原來是這樣,了解了感謝!
02/19 18:41, 3F
文章代碼(AID): #1Of3UNdu (java)
文章代碼(AID): #1Of3UNdu (java)