Java基本類型包裝類概述與Integer類、Character類用法分析
本文實(shí)例講述了Java基本類型包裝類概述與Integer類、Character類用法。分享給大家供大家參考,具體如下:
基本類型包裝類概述
將基本數(shù)據(jù)類型封裝成對象的好處在于可以在對象中定義更多的功能方法操作該數(shù)據(jù)。
常用的操作之一:用于基本數(shù)據(jù)類型與字符串之間的轉(zhuǎn)換。
基本類型和包裝類的對應(yīng)
Byte,Short,Integer,Long,Float,Double,Character,Boolean
Integer類
為了讓基本類型的數(shù)據(jù)進(jìn)行更多的操作,Java就為每種基本類型提供了對應(yīng)的包裝類類型
package cn.itcast_01; /* * 需求1:我要求大家把100這個數(shù)據(jù)的二進(jìn)制,八進(jìn)制,十六進(jìn)制計(jì)算出來 * 需求2:我要求大家判斷一個數(shù)據(jù)是否是int范圍內(nèi)的。 * 首先你的知道int的范圍是多大? * * 為了對基本數(shù)據(jù)類型進(jìn)行更多的操作,更方便的操作,Java就針對每一種基本數(shù)據(jù)類型提供了對應(yīng)的類類型。包裝類類型。 * byte Byte * short Short * int Integer * long Long * float Float * double Double * char Character * boolean Boolean * * 用于基本數(shù)據(jù)類型與字符串之間的轉(zhuǎn)換。 */ public class IntegerDemo { public static void main(String[] args) { // 不麻煩的就來了 // public static String toBinaryString(int i) System.out.println(Integer.toBinaryString(100)); // public static String toOctalString(int i) System.out.println(Integer.toOctalString(100)); // public static String toHexString(int i) System.out.println(Integer.toHexString(100)); // public static final int MAX_VALUE System.out.println(Integer.MAX_VALUE); // public static final int MIN_VALUE System.out.println(Integer.MIN_VALUE); } }
Integer的構(gòu)造方法
package cn.itcast_02; /* * Integer的構(gòu)造方法: * public Integer(int value) * public Integer(String s) * 注意:這個字符串必須是由數(shù)字字符組成 */ public class IntegerDemo { public static void main(String[] args) { // 方式1 int i = 100; Integer ii = new Integer(i); System.out.println("ii:" + ii); // 方式2 String s = "100"; // NumberFormatException // String s = "abc";//這個字符串必須是由數(shù)字字符組成 Integer iii = new Integer(s); System.out.println("iii:" + iii); } }
String和int的相互轉(zhuǎn)換
package cn.itcast_03; /* * int類型和String類型的相互轉(zhuǎn)換 * * int -- String * String.valueOf(number) * * String -- int * Integer.parseInt(s) */ public class IntegerDemo { public static void main(String[] args) { // int -- String int number = 100; // 方式1 String s1 = "" + number; System.out.println("s1:" + s1); // 方式2 String s2 = String.valueOf(number); System.out.println("s2:" + s2); // 方式3 // int -- Integer -- String Integer i = new Integer(number); String s3 = i.toString(); System.out.println("s3:" + s3); // 方式4 // public static String toString(int i) String s4 = Integer.toString(number); System.out.println("s4:" + s4); System.out.println("-----------------"); // String -- int String s = "100"; // 方式1 // String -- Integer -- int Integer ii = new Integer(s); // public int intValue() int x = ii.intValue(); System.out.println("x:" + x); //方式2 //public static int parseInt(String s) int y = Integer.parseInt(s); System.out.println("y:"+y); } }
Integer的進(jìn)制轉(zhuǎn)換的操作
package cn.itcast_04; /* * 常用的基本進(jìn)制轉(zhuǎn)換 * public static String toBinaryString(int i) * public static String toOctalString(int i) * public static String toHexString(int i) * * 十進(jìn)制到其他進(jìn)制 * public static String toString(int i,int radix) * 由這個我們也看到了進(jìn)制的范圍:2-36 * 為什么呢?0,...9,a...z,加起來36個 * * 其他進(jìn)制到十進(jìn)制 * public static int parseInt(String s,int radix) */ public class IntegerDemo { public static void main(String[] args) { // 十進(jìn)制到二進(jìn)制,八進(jìn)制,十六進(jìn)制 System.out.println(Integer.toBinaryString(100)); System.out.println(Integer.toOctalString(100)); System.out.println(Integer.toHexString(100)); System.out.println("-------------------------"); // 十進(jìn)制到其他進(jìn)制 System.out.println(Integer.toString(100, 10)); System.out.println(Integer.toString(100, 2)); System.out.println(Integer.toString(100, 8)); System.out.println(Integer.toString(100, 16)); System.out.println(Integer.toString(100, 5)); System.out.println(Integer.toString(100, 7)); System.out.println(Integer.toString(100, -7)); System.out.println(Integer.toString(100, 70)); System.out.println(Integer.toString(100, 1)); System.out.println(Integer.toString(100, 17)); System.out.println(Integer.toString(100, 32)); System.out.println(Integer.toString(100, 37)); System.out.println(Integer.toString(100, 36)); System.out.println("-------------------------"); //其他進(jìn)制到十進(jìn)制 System.out.println(Integer.parseInt("100", 10)); System.out.println(Integer.parseInt("100", 2)); System.out.println(Integer.parseInt("100", 8)); System.out.println(Integer.parseInt("100", 16)); System.out.println(Integer.parseInt("100", 23)); //NumberFormatException //System.out.println(Integer.parseInt("123", 2)); } }
JDK5的新特性--自動裝箱和自動拆箱
package cn.itcast_05; /* * JDK5的新特性 * 自動裝箱:把基本類型轉(zhuǎn)換為包裝類類型 * 自動拆箱:把包裝類類型轉(zhuǎn)換為基本類型 * * 注意一個小問題: * 在使用時,Integer x = null;代碼就會出現(xiàn)NullPointerException。 * 建議先判斷是否為null,然后再使用。 */ public class IntegerDemo { public static void main(String[] args) { // 定義了一個int類型的包裝類類型變量i // Integer i = new Integer(100); Integer ii = 100; ii += 200; System.out.println("ii:" + ii); // 通過反編譯后的代碼 // Integer ii = Integer.valueOf(100); //自動裝箱 // ii = Integer.valueOf(ii.intValue() + 200); //自動拆箱,再自動裝箱 // System.out.println((new StringBuilder("ii:")).append(ii).toString()); Integer iii = null; // NullPointerException,如果iii為空對象,會報(bào)錯,需要判斷是否為空 if (iii != null) { iii += 1000; System.out.println(iii); } } }
-128到127之間的數(shù)據(jù)緩沖池問題
package cn.itcast_06; /* * 看程序?qū)懡Y(jié)果 * * 注意:Integer的數(shù)據(jù)直接賦值,如果在-128到127之間,會直接從緩沖池里獲取數(shù)據(jù) */ public class IntegerDemo { public static void main(String[] args) { Integer i1 = new Integer(127); Integer i2 = new Integer(127); System.out.println(i1 == i2); System.out.println(i1.equals(i2)); System.out.println("-----------"); Integer i3 = new Integer(128); Integer i4 = new Integer(128); System.out.println(i3 == i4); System.out.println(i3.equals(i4)); System.out.println("-----------"); Integer i5 = 128; Integer i6 = 128; System.out.println(i5 == i6); System.out.println(i5.equals(i6)); System.out.println("-----------"); Integer i7 = 127; Integer i8 = 127; System.out.println(i7 == i8);//true System.out.println(i7.equals(i8)); // 通過查看源碼,我們就知道了,針對-128到127之間的數(shù)據(jù),做了一個數(shù)據(jù)緩沖池,如果數(shù)據(jù)是該范圍內(nèi)的,每次并不創(chuàng)建新的空間 // Integer ii = Integer.valueOf(127); } }
Character
Character 類在對象中包裝一個基本類型 char 的值
package cn.itcast_01; /* * Character 類在對象中包裝一個基本類型 char 的值 * 此外,該類提供了幾種方法,以確定字符的類別(小寫字母,數(shù)字,等等),并將字符從大寫轉(zhuǎn)換成小寫,反之亦然 * * 構(gòu)造方法: * Character(char value) */ public class CharacterDemo { public static void main(String[] args) { // 創(chuàng)建對象 // Character ch = new Character((char) 97); Character ch = new Character('a'); System.out.println("ch:" + ch); } }
Character 類,常見方法。
確定字符的類別(小寫字母,數(shù)字,等等),并將字符從大寫轉(zhuǎn)換成小寫
package cn.itcast_02; /* * public static boolean isUpperCase(char ch):判斷給定的字符是否是大寫字符 * public static boolean isLowerCase(char ch):判斷給定的字符是否是小寫字符 * public static boolean isDigit(char ch):判斷給定的字符是否是數(shù)字字符 * public static char toUpperCase(char ch):把給定的字符轉(zhuǎn)換為大寫字符 * public static char toLowerCase(char ch):把給定的字符轉(zhuǎn)換為小寫字符 */ public class CharacterDemo { public static void main(String[] args) { // public static boolean isUpperCase(char ch):判斷給定的字符是否是大寫字符 System.out.println("isUpperCase:" + Character.isUpperCase('A')); System.out.println("isUpperCase:" + Character.isUpperCase('a')); System.out.println("isUpperCase:" + Character.isUpperCase('0')); System.out.println("-----------------------------------------"); // public static boolean isLowerCase(char ch):判斷給定的字符是否是小寫字符 System.out.println("isLowerCase:" + Character.isLowerCase('A')); System.out.println("isLowerCase:" + Character.isLowerCase('a')); System.out.println("isLowerCase:" + Character.isLowerCase('0')); System.out.println("-----------------------------------------"); // public static boolean isDigit(char ch):判斷給定的字符是否是數(shù)字字符 System.out.println("isDigit:" + Character.isDigit('A')); System.out.println("isDigit:" + Character.isDigit('a')); System.out.println("isDigit:" + Character.isDigit('0')); System.out.println("-----------------------------------------"); // public static char toUpperCase(char ch):把給定的字符轉(zhuǎn)換為大寫字符 System.out.println("toUpperCase:" + Character.toUpperCase('A')); System.out.println("toUpperCase:" + Character.toUpperCase('a')); System.out.println("-----------------------------------------"); // public static char toLowerCase(char ch):把給定的字符轉(zhuǎn)換為小寫字符 System.out.println("toLowerCase:" + Character.toLowerCase('A')); System.out.println("toLowerCase:" + Character.toLowerCase('a')); } }
統(tǒng)計(jì)一個字符串中大寫字母字符,小寫字母字符,數(shù)字字符出現(xiàn)的次數(shù)
package cn.itcast_03; import java.util.Scanner; /* * 統(tǒng)計(jì)一個字符串中大寫字母字符,小寫字母字符,數(shù)字字符出現(xiàn)的次數(shù)。(不考慮其他字符) * * 分析: * A:定義三個統(tǒng)計(jì)變量。 * int bigCont=0; * int smalCount=0; * int numberCount=0; * B:鍵盤錄入一個字符串。 * C:把字符串轉(zhuǎn)換為字符數(shù)組。 * D:遍歷字符數(shù)組獲取到每一個字符 * E:判斷該字符是 * 大寫 bigCount++; * 小寫 smalCount++; * 數(shù)字 numberCount++; * F:輸出結(jié)果即可 */ public class CharacterTest { public static void main(String[] args) { // 定義三個統(tǒng)計(jì)變量。 int bigCount = 0; int smallCount = 0; int numberCount = 0; // 鍵盤錄入一個字符串。 Scanner sc = new Scanner(System.in); System.out.println("請輸入一個字符串:"); String line = sc.nextLine(); // 把字符串轉(zhuǎn)換為字符數(shù)組。 char[] chs = line.toCharArray(); // 歷字符數(shù)組獲取到每一個字符 for (int x = 0; x < chs.length; x++) { char ch = chs[x]; // 判斷該字符 if (Character.isUpperCase(ch)) { bigCount++; } else if (Character.isLowerCase(ch)) { smallCount++; } else if (Character.isDigit(ch)) { numberCount++; } } // 輸出結(jié)果即可 System.out.println("大寫字母:" + bigCount + "個"); System.out.println("小寫字母:" + smallCount + "個"); System.out.println("數(shù)字字符:" + numberCount + "個"); } }
PS:這里再為大家推薦幾款功能相似的在線工具供大家參考:
在線任意進(jìn)制轉(zhuǎn)換工具:
http://tools.jb51.net/transcoding/hexconvert
字?jǐn)?shù)統(tǒng)計(jì)工具:
http://tools.jb51.net/code/zishutongji
在線字母大小寫轉(zhuǎn)換工具:
http://tools.jb51.net/transcoding/upper
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java字符與字符串操作技巧總結(jié)》、《Java數(shù)組操作技巧總結(jié)》、《Java數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》及《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》
希望本文所述對大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
取消idea雙擊shift鍵時出現(xiàn)的全局搜索的問題分析
這篇文章主要介紹了取消idea雙擊shift鍵時出現(xiàn)的全局搜索的問題分析,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-10-10如何通過Java監(jiān)聽MySQL數(shù)據(jù)的變化
對于二次開發(fā)來說,很大一部分就找找文件和找數(shù)據(jù)庫的變化情況,下面這篇文章主要給大家介紹了關(guān)于如何通過Java監(jiān)聽MySQL數(shù)據(jù)的變化的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03Java實(shí)現(xiàn)XML文件學(xué)生通訊錄
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)XML文件學(xué)生通訊錄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02Java實(shí)現(xiàn)Word/Pdf/TXT轉(zhuǎn)html的實(shí)例代碼
本文主要介紹了Java實(shí)現(xiàn)Word/Pdf/TXT轉(zhuǎn)html的實(shí)例代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02詳解SpringCloud新一代網(wǎng)關(guān)Gateway
SpringCloud Gateway是Spring Cloud的一個全新項(xiàng)目,Spring 5.0+ Spring Boot 2.0和Project Reactor等技術(shù)開發(fā)的網(wǎng)關(guān),它旨在為微服務(wù)架構(gòu)提供一種簡單有效的統(tǒng)一的API路由管理方式2021-06-06