# 包裝型別與基本型別之比較 ###### tags: `Java` `numberCompare` 在實務上,以包裝型別來做空值判斷會比較方便,但是總是會需要回歸數字比較的情形。 由於不確定Long跟int的比較是否會有不同,所以做了簡單的測試。 ```java= Long a=129L; Long ab = 130L; int b = 129; Integer c = 129; Float d=129.1F; System.out.println(a==128); System.out.println(a==b); System.out.println(a.longValue() == c); System.out.println(a.longValue() == c); System.out.println(a.equals(c)); System.out.println(a.equals(d)); System.out.println(a.equals((Float)d)); System.out.println(a==d.floatValue()); System.out.println(a.longValue() == (Float)d); System.out.println(a.longValue()); System.out.println(a<ab); ``` 其結果為 :  看起來如果包裝型別直接跟基本型別的int相比的話,似乎不會有錯誤。 但是怕自己沒有測到極限的部分,所以有上網稍微找了一下。   所以目前就暫時用longValue來比較大小(讓一邊直接變成基本型別,另一邊的包裝型別就會自動拆箱來比對數值,就不容易去踩坑)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up