JAVA Integer類常用方法解析
JAVA中Integer類下的常用方法有哪些?
1.進制轉(zhuǎn)換 n進制轉(zhuǎn)10進制 字符串結(jié)果
Integer.parseInt(String s, int radix);radix范圍為2-36(包括0-9,a-z)string輸入為二進制字符串System.out.println( Integer.parseInt('10000',2));//16
2.int轉(zhuǎ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進制轉(zhuǎn)16進制字符串
System.out.println( Integer.toHexString(456));//1c8
5.n進制轉(zhuǎn)10進制數(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ù)解析為帶符號的十進制整數(shù)。默認是十進制 System.out.println(new Integer(55).parseInt('+110')); System.out.println(new Integer(55).parseInt('-110'));//由參數(shù)以十進制表示的整數(shù)值 parseInt(String s, int radix) 將字符串參數(shù)解析為第二個參數(shù)指定的基數(shù)中的有符號整數(shù)。方法parseInt(String s,int radix)的目的是輸出一個十進制數(shù),這個數(shù)字是“String s”但是我們要知道他是多少進制的,而方法中“int radix”參數(shù)正是來表達這個信息的。 比如:parseInt(1010,2) 意思就是:輸出2進制數(shù)1010在十進制下的數(shù). radix的范圍是在2~36之間,超出范圍會拋異常。其中s的長度也不能超出7,否也會拋異常。 System.out.println(new Integer(9).parseInt('111', 11)); //vreverse(int i) 返回由指定的二進制補碼表示反轉(zhuǎn)位的順序而獲得的值 int值。i - 要反轉(zhuǎn)的值 System.out.println(new Integer(10).reverse(3)); //reverseBytes(int i) 返回反轉(zhuǎn)指定的二進制補碼表示的字節(jié)順序而獲得的值 int值。i - 要顛倒其字節(jié)的值 System.out.println(new Integer(500).reverseBytes(3)) //toBinaryString(int i) 在基數(shù)2中返回整數(shù)參數(shù)的字符串表示形式為無符號整數(shù)。 //如果整型變量為負,這個變量加上二百三十二就是實際儲存的二進制;如果整型變量為正,這個變量的二進制就是實際存儲的二進制.然后,從左到右掃描入字符串中.如果無符號值為零,它就只用一個零來表示;否則的話,無符號字符串第一位不用零來表示.二進制表中只用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ù)。 //說簡單點,就是十進制轉(zhuǎ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ù)以八進制輸出 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ù)進行解析時。 //返回第二個參數(shù)指定的技術(shù)進行解析 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. ASP.NET MVC把數(shù)據(jù)庫中枚舉項的數(shù)字轉(zhuǎn)換成文字2. SpringMVC+Jquery實現(xiàn)Ajax功能3. 基于javaweb+jsp實現(xiàn)企業(yè)財務(wù)記賬管理系統(tǒng)4. 博客日志摘要暨RSS技術(shù)5. 低版本IE正常運行HTML5+CSS3網(wǎng)站的3種解決方案6. WML學(xué)習(xí)之一概述和基本規(guī)則7. Java pom.xml parent引用報錯問題解決方案8. python 集合set中 add與update區(qū)別介紹9. CSS單標(biāo)簽實現(xiàn)復(fù)雜的棋盤布局10. Java try catch finally異常處理組合詳解
