JAVA Integer類常用方法解析
JAVA中Integer類下的常用方法有哪些?
1.進(jìn)制轉(zhuǎn)換 n進(jìn)制轉(zhuǎn)10進(jìn)制 字符串結(jié)果
Integer.parseInt(String s, int radix);radix范圍為2-36(包括0-9,a-z)string輸入為二進(jìn)制字符串System.out.println( Integer.parseInt('10000',2));//16
2.int轉(zhuǎn)二進(jìn)制字符串
System.out.println( Integer.toBinaryString(789));//11000101013.int的最大值和最小值
System.out.println( Integer.MIN_VALUE); System.out.println( Integer.MAX_VALUE); //-2147483648 //2147483647
4.10進(jìn)制轉(zhuǎn)16進(jìn)制字符串
System.out.println( Integer.toHexString(456));//1c8
5.n進(jìn)制轉(zhuǎn)10進(jìn)制數(shù)
System.out.println( Integer.valueOf('100',10)); 6.max(int a, int b) 返回兩個 int的較大值,就像調(diào)用 Math.max一樣 。 System.out.println(new Integer(1).max(2, 3));//返回max中最大的值/min(int a, int b) 返回兩個 int的較小值,就像調(diào)用 Math.min一樣 。 System.out.println(new Integer(1).min(2, 3)); 7.parseInt(String s) 將字符串參數(shù)解析為帶符號的十進(jìn)制整數(shù)。默認(rèn)是十進(jìn)制 System.out.println(new Integer(55).parseInt('+110')); System.out.println(new Integer(55).parseInt('-110'));//由參數(shù)以十進(jìn)制表示的整數(shù)值 parseInt(String s, int radix) 將字符串參數(shù)解析為第二個參數(shù)指定的基數(shù)中的有符號整數(shù)。方法parseInt(String s,int radix)的目的是輸出一個十進(jìn)制數(shù),這個數(shù)字是“String s”但是我們要知道他是多少進(jìn)制的,而方法中“int radix”參數(shù)正是來表達(dá)這個信息的。 比如:parseInt(1010,2) 意思就是:輸出2進(jìn)制數(shù)1010在十進(jìn)制下的數(shù). radix的范圍是在2~36之間,超出范圍會拋異常。其中s的長度也不能超出7,否也會拋異常。 System.out.println(new Integer(9).parseInt('111', 11)); //vreverse(int i) 返回由指定的二進(jìn)制補(bǔ)碼表示反轉(zhuǎn)位的順序而獲得的值 int值。i - 要反轉(zhuǎn)的值 System.out.println(new Integer(10).reverse(3)); //reverseBytes(int i) 返回反轉(zhuǎn)指定的二進(jìn)制補(bǔ)碼表示的字節(jié)順序而獲得的值 int值。i - 要顛倒其字節(jié)的值 System.out.println(new Integer(500).reverseBytes(3)) //toBinaryString(int i) 在基數(shù)2中返回整數(shù)參數(shù)的字符串表示形式為無符號整數(shù)。 //如果整型變量為負(fù),這個變量加上二百三十二就是實(shí)際儲存的二進(jìn)制;如果整型變量為正,這個變量的二進(jìn)制就是實(shí)際存儲的二進(jìn)制.然后,從左到右掃描入字符串中.如果無符號值為零,它就只用一個零來表示;否則的話,無符號字符串第一位不用零來表示.二進(jìn)制表中只用0和1. System.out.println(new Integer(0).toBinaryString(1)); System.out.println(new Integer(0).toBinaryString(-333)); System.out.println(new Integer(0).toBinaryString(0)); //toHexString(int i) 返回整數(shù)參數(shù)的字符串表示形式,作為16位中的無符號整數(shù)。 //說簡單點(diǎn),就是十進(jìn)制轉(zhuǎn)化為十六進(jìn)制 System.out.println(new Integer(0).toHexString(1)); System.out.println(new Integer(0).toHexString(-333)); System.out.println(new Integer(0).toHexString(0)); //toOctalString(int i) 在基數(shù)8中返回整數(shù)參數(shù)的字符串表示形式為無符號整數(shù)。 //由參數(shù)以八進(jìn)制輸出 System.out.println(new Integer(0).toOctalString(1)); System.out.println(new Integer(0).toOctalString(-333)); System.out.println(new Integer(0).toOctalString(0)); //toString(int i) 返回 String表示此對象 Integer的價值。 System.out.println(new Integer(0).toString(33)); System.out.println(new Integer(0).toString(22)); //toString(int i, int radix) 返回由第二個參數(shù)指定的基數(shù)中的第一個參數(shù)的字符串表示形式。 System.out.println(new Integer(0).toString(33,4));//第二個參數(shù)是直接基數(shù)返回 System.out.println(new Integer(0).toString(22,10));//valueOf(String s) 返回一個 Integer對象,保存指定的值為 String 。 System.out.println(new Integer(111).valueOf('99')); System.out.println(new Integer(111).valueOf('88')); System.out.println(new Integer(111).valueOf('-12')); //valueOf(String s, int radix) 返回一個 Integer對象,保存從指定的String中 String的值,當(dāng)用第二個參數(shù)給出的基數(shù)進(jìn)行解析時。 //返回第二個參數(shù)指定的技術(shù)進(jìn)行解析 System.out.println(new Integer(111).valueOf('99',10)); System.out.println(new Integer(111).valueOf('88',11)); System.out.println(new Integer(111).valueOf('-12',10));
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Python 的 __str__ 和 __repr__ 方法對比2. IntelliJ IDEA設(shè)置默認(rèn)瀏覽器的方法3. Spring security 自定義過濾器實(shí)現(xiàn)Json參數(shù)傳遞并兼容表單參數(shù)(實(shí)例代碼)4. IntelliJ IDEA設(shè)置背景圖片的方法步驟5. docker /var/lib/docker/aufs/mnt 目錄清理方法6. Python TestSuite生成測試報告過程解析7. 學(xué)python最電腦配置有要求么8. JAMon(Java Application Monitor)備忘記9. Python Scrapy多頁數(shù)據(jù)爬取實(shí)現(xiàn)過程解析10. Python OpenCV去除字母后面的雜線操作
