簡介
java中可以被稱為Number的有byte,short,int,long,float,double和char,我們在使用這些Nubmer的過程中,需要注意些什么內(nèi)容呢?一起來看看吧。
Number的范圍
每種Number類型都有它的范圍,我們看下java中Number類型的范圍:
考慮到我們最常用的int操作,雖然int的范圍夠大,但是如果我們在做一些int操作的時候還是可能超出int的范圍。
超出了int范圍會發(fā)送什么事情呢?看下面的例子:
1
2
3
|
public void testIntegerOverflow(){ System.out.println(Integer.MAX_VALUE+ 1000 ); } |
運行結(jié)果:-2147482649。
很明顯Integer.MAX_VALUE+1000將會超出Integer的最大值范圍,但是我們沒有得到異常提醒,反而得到了一個錯誤的結(jié)果。
正確的操作是如果我們遇到了Overflow的問題,需要拋出異常:ArithmeticException。
怎么防止這種IntegerOverflow的問題呢?一般來講,我們有下面幾種方式。
第一種方式:在做Integer操作之前,進行預(yù)判斷是否超出范圍:
舉個例子:
1
2
3
4
5
6
7
|
static final int safeAdd( int left, int right) { if (right > 0 ? left > Integer.MAX_VALUE - right : left < Integer.MIN_VALUE - right) { throw new ArithmeticException( "Integer overflow" ); } return left + right; } |
上面的例子中,我們需要進行兩個整數(shù)相加操作,在相加之前,我們需要進行范圍的判斷,從而保證計算的安全性。
第二種方式:使用Math的addExact和multiplyExact方法:
Math的addExact和multiplyExact方法已經(jīng)提供了Overflow的判斷,我們看下addExact的實現(xiàn):
1
2
3
4
5
6
7
8
|
public static int addExact( int x, int y) { int r = x + y; // HD 2-12 Overflow iff both arguments have the opposite sign of the result if (((x ^ r) & (y ^ r)) < 0 ) { throw new ArithmeticException( "integer overflow" ); } return r; } |
看下怎么使用:
1
2
3
|
public int addUseMath( int a, int b){ return Math.addExact(a,b); } |
第三種方式:向上轉(zhuǎn)型
既然超出了Integer的范圍,那么我們可以用范圍更大的long來存儲數(shù)據(jù)。
1
2
3
4
5
6
7
8
9
10
|
public static long intRangeCheck( long value) { if ((value < Integer.MIN_VALUE) || (value > Integer.MAX_VALUE)) { throw new ArithmeticException( "Integer overflow" ); } return value; } public int addUseUpcasting( int a, int b){ return ( int )intRangeCheck(( long )a+( long )b); } |
上面的例子中,我們將a+b轉(zhuǎn)換成了兩個long相加,從而保證不溢出范圍。
然后進行一次范圍比較,從而判斷相加之后的結(jié)果是否仍然在整數(shù)范圍內(nèi)。
第四種方式:使用BigInteger
我們可以使用BigInteger.valueOf(a)將int轉(zhuǎn)換成為BigInteger,再進行后續(xù)操作:
1
2
3
|
public int useBigInteger( int a, int b){ return BigInteger.valueOf(a).add(BigInteger.valueOf(b)).intValue(); } |
區(qū)分位運算和算數(shù)運算
我們通常會對Integer進行位運算或者算數(shù)運算。雖然可以進行兩種運算,但是最好不要將兩種運算同時進行,這樣會造成混淆。
比如下面的例子:
x += (x << 1) + 1;
上面的例子是想做什么呢?其實它是想將3x+1的值賦給x。
但是這樣寫出來讓人很難理解,所以我們需要避免這樣實現(xiàn)。
再看下面的一個例子:
1
2
3
4
5
6
|
public void testBitwiseOperation(){ int i = - 10 ; System.out.println(i>>> 2 ); System.out.println(i>> 2 ); System.out.println(i/ 4 ); } |
本來我們想做的是將i除以4,結(jié)果發(fā)現(xiàn)只有最后一個才是我們要的結(jié)果。
我們來解釋一下,第一個i>>>2是邏輯右移,將會把最左邊的填充成0,所以得出的結(jié)果是一個正值1073741821。
第二個i>>2是算數(shù)右移,最左邊的還是會填充成1,但是會向下取整,所以得出結(jié)果是-3.
直接使用i/4,我們是向上取整,所以得出結(jié)果是-2.
注意不要使用0作為除數(shù)
我們在使用變量作為除數(shù)的時候,一定要注意先判斷是否為0.
兼容C++的無符號整數(shù)類型
在java中只有16位的char表示的是無符號整數(shù),而int實際上表示的是帶符號的整數(shù)。
而在C或者C++中是可以直接表示無符號的整數(shù)的,那么,如果我們有一個32位的無符號整數(shù),該怎么用java來處理呢?
1
2
3
|
public int readIntWrong(DataInputStream is) throws IOException { return is.readInt(); } |
看上面的例子,我們從Stream中讀取一個int值,如果是一個32位的無符號整數(shù),那么讀出來int就變成了有符號的負整數(shù),這和我們的期望是相符的。
考慮一下,long是64位的,我們是不是可以使用long來表示32位的無符號整數(shù)呢?
1
2
3
|
public long readIntRight(DataInputStream is) throws IOException{ return is.readInt() & 0xFFFFFFFFL; // Mask with 32 one-bits } |
看上面的例子,我們返回的是long,如果將32位的int轉(zhuǎn)換成為64位的long,會自動根據(jù)符號位進行補全。
所以這時候我們需要和0xFFFFFFFFL進行mask操作,將高32位重置為0.
NAN和INFINITY
在整型運算中,除數(shù)是不能為0的,否則直接運行異常。但是在浮點數(shù)運算中,引入了NAN和INFINITY的概念,我們來看一下Double和Float中的定義。
1
2
3
|
public static final double POSITIVE_INFINITY = 1.0 / 0.0 ; public static final double NEGATIVE_INFINITY = - 1.0 / 0.0 ; public static final double NaN = 0 .0d / 0.0 ; |
1
2
3
|
public static final float POSITIVE_INFINITY = 1 .0f / 0 .0f; public static final float NEGATIVE_INFINITY = - 1 .0f / 0 .0f; public static final float NaN = 0 .0f / 0 .0f; |
1除以0就是INFINITY,而0除以0就是NaN。
接下來,我們看一下NAN和INFINITY的比較:
1
2
3
|
public void compareInfinity(){ System.out.println(Double.POSITIVE_INFINITY == Double.POSITIVE_INFINITY); } |
運行結(jié)果是true。
1
2
3
|
public void compareNaN(){ System.out.println(Double.NaN == Double.NaN); } |
運行結(jié)果是false。
可以看到NaN和NaN相比是false。
那么我們怎么比較NaN呢?
別急,Double提供了一個isNaN方法,我們可以這樣使用:
System.out.println(Double.isNaN(Double.NaN));
接下來我們看一個在代碼中經(jīng)常會用到的一個Double解析:
1
2
3
4
5
6
7
8
|
public void incorrectParse(String userInput){ double val = 0 ; try { val = Double.valueOf(userInput); } catch (NumberFormatException e) { } //do something for val } |
這段代碼有沒有問題?咋看下好像沒有問題,但是,如果我們的userInput是NaN,Infinity,或者-Infinity,Double.valueOf是可以解析得到結(jié)果的。
1
2
3
4
5
|
public void testNaN(){ System.out.println(Double.valueOf( "NaN" )); System.out.println(Double.valueOf( "Infinity" )); System.out.println(Double.valueOf( "-Infinity" )); } |
運行輸出:
NaN
Infinity
-Infinity
所以,我們還需要額外去判斷NaN和Infinity:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public void correctParse(String userInput){ double val = 0 ; try { val = Double.valueOf(userInput); } catch (NumberFormatException e) { } if (Double.isInfinite(val)){ // Handle infinity error } if (Double.isNaN(val)) { // Handle NaN error } //do something for val } |
不要使用float或者double作為循環(huán)的計數(shù)器
考慮下面的代碼:
1
2
3
|
for ( float x = 0 .1f; x <= 1 .0f; x += 0 .1f) { System.out.println(x); } |
上面的代碼有什么問題呢?
我們都知道java中浮點數(shù)是不準(zhǔn)確的,但是不一定有人知道為什么不準(zhǔn)確。
這里給大家解釋一下,計算機中所有與的數(shù)都是以二進制存儲的,我們以0.6為例。
0.6轉(zhuǎn)成為二進制格式是乘2取整,0.6x2=1.2,取整剩余0.2,繼續(xù)上面的步驟0.2x2=0.4,0.4x2=0.8,0.8x2=1.6,取整剩余0.6,產(chǎn)生了一個循環(huán)。
所以0.6的二進制格式是.1001 1001 1001 1001 1001 1001 1001 … 無限循環(huán)下去。
所以,有些小數(shù)是無法用二進制精確的表示的,最終導(dǎo)致使用float或者double作為計數(shù)器是不準(zhǔn)的。
BigDecimal的構(gòu)建
為了解決float或者Double計算中精度缺失的問題,我們通常會使用BigDecimal。
那么在使用BigDecimal的時候,請注意一定不要從float構(gòu)建BigDecimal,否則可能出現(xiàn)意想不到的問題。
1
2
3
|
public void getFromFloat(){ System.out.println( new BigDecimal( 0.1 )); } |
上面的代碼,我們得到的結(jié)果是:0.1000000000000000055511151231257827021181583404541015625。
這是因為二進制無法完美的展示所有的小數(shù)。
所以,我們需要從String來構(gòu)建BigDecimal:
1
2
3
|
public void getFromString(){ System.out.println( new BigDecimal( "0.1" )); } |
類型轉(zhuǎn)換問題
在java中各種類型的Number可以互相進行轉(zhuǎn)換:
比如:
short to byte or char
char to byte or short
int to byte, short, or char
long to byte, short, char, or int
float to byte, short, char, int, or long
double to byte, short, char, int, long, or float
或者反向:
byte to short, int, long, float, or double
short to int, long, float, or double
char to int, long, float, or double
int to long, float, or double
long to float or double
float to double
從大范圍的類型轉(zhuǎn)向小范圍的類型時,我們要考慮是否超出轉(zhuǎn)換類型范圍的情況:
1
2
3
4
5
6
|
public void intToByte( int i){ if ((i < Byte.MIN_VALUE) || (i > Byte.MAX_VALUE)) { throw new ArithmeticException( "Value is out of range" ); } byte b = ( byte ) i; } |
比如上面的例子中,我們將int轉(zhuǎn)換成為byte,那么在轉(zhuǎn)換之前,需要先判斷int是否超出了byte的范圍。
同時我們還需要考慮到精度的切換,看下面的例子:
1
2
3
4
5
6
7
|
public void intToFloat(){ System.out.println(subtraction( 1111111111 , 1111111111 )); } public int subtraction( int i , float j){ return i - ( int )j; } |
結(jié)果是多少呢?
答案不是0,而是-57。
為什么呢?
因為這里我們做了兩次轉(zhuǎn)換,第一次從1111111111轉(zhuǎn)換到float,float雖然有32位,但是只有23位是存放真正的數(shù)值的,1位是符號位,剩下的8位是指數(shù)位。
所以從1111111111轉(zhuǎn)換到float發(fā)送了精度丟失。
我們可以把subtraction方法修改一下,首先判斷float的范圍,如果超出了23bit的表示范圍,則說明發(fā)送了精度丟失,我們需要拋出異常:
1
2
3
4
5
6
7
|
public int subtraction( int i , float j){ System.out.println(j); if ((j > 0x007fffff ) || (j < - 0x800000 )) { throw new ArithmeticException( "Insufficient precision" ); } return i - ( int )j; } |
當(dāng)然還有一種辦法,我們可以用精度更高的double來做轉(zhuǎn)換,double有52位來存放真正的數(shù)據(jù),所以足夠了。
1
2
3
4
|
public int subtractionWithDouble( int i , double j){ System.out.println(j); return i - ( int )j; } |
本文的代碼:
learn-java-base-9-to-20/tree/master/security
以上這篇java安全編碼指南之:Number操作詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/superfjj/article/details/108507861