一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java基本類型包裝類概述與Integer類、Character類用法分析

Java基本類型包裝類概述與Integer類、Character類用法分析

2021-07-24 14:28白楊-M Java教程

這篇文章主要介紹了Java基本類型包裝類概述與Integer類、Character類用法,結合實例形式分析了java基本數據類型與字符串轉換相關操作技巧,需要的朋友可以參考下

本文實例講述了java基本類型包裝類概述與integer類、character類用法。分享給大家供大家參考,具體如下:

基本類型包裝類概述

將基本數據類型封裝成對象的好處在于可以在對象中定義更多的功能方法操作該數據。

常用的操作之一:用于基本數據類型與字符串之間的轉換。

基本類型和包裝類的對應

byte,short,integer,long,float,doublecharacter,boolean

integer類

為了讓基本類型的數據進行更多的操作,java就為每種基本類型提供了對應的包裝類類型

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package cn.itcast_01;
/*
 * 需求1:我要求大家把100這個數據的二進制,八進制,十六進制計算出來
 * 需求2:我要求大家判斷一個數據是否是int范圍內的。
 *     首先你的知道int的范圍是多大?
 *
 * 為了對基本數據類型進行更多的操作,更方便的操作,java就針對每一種基本數據類型提供了對應的類類型。包裝類類型。
 * byte       byte
 * short      short
 * int        integer
 * long        long
 * float      float
 * double      double
 * char        character
 * boolean      boolean
 *
 * 用于基本數據類型與字符串之間的轉換。
 */
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的構造方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package cn.itcast_02;
/*
 * integer的構造方法:
 * public integer(int value)
 * public integer(string s)
 *     注意:這個字符串必須是由數字字符組成
 */
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";//這個字符串必須是由數字字符組成
    integer iii = new integer(s);
    system.out.println("iii:" + iii);
  }
}

string和int的相互轉換

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package cn.itcast_03;
/*
 * int類型和string類型的相互轉換
 *
 * 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的進制轉換的操作

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package cn.itcast_04;
/*
 * 常用的基本進制轉換
 * public static string tobinarystring(int i)
 * public static string tooctalstring(int i)
 * public static string tohexstring(int i)
 *
 * 十進制到其他進制
 * public static string tostring(int i,int radix)
 * 由這個我們也看到了進制的范圍:2-36
 * 為什么呢?0,...9,a...z,加起來36個
 *
 * 其他進制到十進制
 * public static int parseint(string s,int radix)
 */
public class integerdemo {
  public static void main(string[] args) {
    // 十進制到二進制,八進制,十六進制
    system.out.println(integer.tobinarystring(100));
    system.out.println(integer.tooctalstring(100));
    system.out.println(integer.tohexstring(100));
    system.out.println("-------------------------");
    // 十進制到其他進制
    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("-------------------------");
    //其他進制到十進制
    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的新特性--自動裝箱和自動拆箱

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package cn.itcast_05;
/*
 * jdk5的新特性
 * 自動裝箱:把基本類型轉換為包裝類類型
 * 自動拆箱:把包裝類類型轉換為基本類型
 *
 * 注意一個小問題:
 *     在使用時,integer x = null;代碼就會出現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為空對象,會報錯,需要判斷是否為空
    if (iii != null) {
      iii += 1000;
      system.out.println(iii);
    }
  }
}

-128到127之間的數據緩沖池問題

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package cn.itcast_06;
/*
 * 看程序寫結果
 *
 * 注意:integer的數據直接賦值,如果在-128到127之間,會直接從緩沖池里獲取數據
 */
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之間的數據,做了一個數據緩沖池,如果數據是該范圍內的,每次并不創建新的空間
    // integer ii = integer.valueof(127);
  }
}

character

character 類在對象中包裝一個基本類型 char 的值

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package cn.itcast_01;
/*
 * character 類在對象中包裝一個基本類型 char 的值
 * 此外,該類提供了幾種方法,以確定字符的類別(小寫字母,數字,等等),并將字符從大寫轉換成小寫,反之亦然
 *
 * 構造方法:
 *     character(char value)
 */
public class characterdemo {
  public static void main(string[] args) {
    // 創建對象
    // character ch = new character((char) 97);
    character ch = new character('a');
    system.out.println("ch:" + ch);
  }
}

character 類,常見方法。

確定字符的類別(小寫字母,數字,等等),并將字符從大寫轉換成小寫

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package cn.itcast_02;
/*
 * public static boolean isuppercase(char ch):判斷給定的字符是否是大寫字符
 * public static boolean islowercase(char ch):判斷給定的字符是否是小寫字符
 * public static boolean isdigit(char ch):判斷給定的字符是否是數字字符
 * public static char touppercase(char ch):把給定的字符轉換為大寫字符
 * public static char tolowercase(char ch):把給定的字符轉換為小寫字符
 */
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):判斷給定的字符是否是數字字符
    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):把給定的字符轉換為大寫字符
    system.out.println("touppercase:" + character.touppercase('a'));
    system.out.println("touppercase:" + character.touppercase('a'));
    system.out.println("-----------------------------------------");
    // public static char tolowercase(char ch):把給定的字符轉換為小寫字符
    system.out.println("tolowercase:" + character.tolowercase('a'));
    system.out.println("tolowercase:" + character.tolowercase('a'));
  }
}

統計一個字符串中大寫字母字符,小寫字母字符,數字字符出現的次數

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package cn.itcast_03;
import java.util.scanner;
/*
 * 統計一個字符串中大寫字母字符,小寫字母字符,數字字符出現的次數。(不考慮其他字符)
 *
 * 分析:
 *     a:定義三個統計變量。
 *       int bigcont=0;
 *       int smalcount=0;
 *       int numbercount=0;
 *     b:鍵盤錄入一個字符串。
 *     c:把字符串轉換為字符數組。
 *     d:遍歷字符數組獲取到每一個字符
 *     e:判斷該字符是
 *       大寫  bigcount++;
 *       小寫  smalcount++;
 *       數字  numbercount++;
 *     f:輸出結果即可
 */
public class charactertest {
  public static void main(string[] args) {
    // 定義三個統計變量。
    int bigcount = 0;
    int smallcount = 0;
    int numbercount = 0;
    // 鍵盤錄入一個字符串。
    scanner sc = new scanner(system.in);
    system.out.println("請輸入一個字符串:");
    string line = sc.nextline();
    // 把字符串轉換為字符數組。
    char[] chs = line.tochararray();
    // 歷字符數組獲取到每一個字符
    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++;
      }
    }
    // 輸出結果即可
    system.out.println("大寫字母:" + bigcount + "個");
    system.out.println("小寫字母:" + smallcount + "個");
    system.out.println("數字字符:" + numbercount + "個");
  }
}

ps:這里再為大家推薦幾款功能相似的在線工具供大家參考:

在線任意進制轉換工具:https://tool.zzvips.com/t/hex/

字數統計工具:https://tool.zzvips.com/t/textcount/

在線字母大小寫轉換工具:https://tool.zzvips.com/t/daxiaoxie/

希望本文所述對大家java程序設計有所幫助。

原文鏈接:https://www.cnblogs.com/baiyangyuanzi/p/6861718.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 96萝莉| 热久久天天拍天天拍热久久2018 | 国产成人精品高清免费 | 日本在线视频播放 | 鬼吹灯天星术在线高清观看 | 91日本在线 | 成人网免费视频 | 欧美肥b | 国产精品99爱免费视频 | 久久伊人在| 色综合久久六月婷婷中文字幕 | 日本大片免a费观看在线 | 日韩日日操 | 别停好爽好深好大好舒服视频 | 国产成人www免费人成看片 | 亚洲国产精品无码中文字幕 | 九九大香尹人视频免费 | 美女被无套进入 | 日本精品久久久久中文字幕 1 | 无人区在线观看免费完整版免费 | 亚洲精品动漫在线观看 | 久久WWW免费人成一看片 | 日韩一区二区不卡 | 色导行| 国产1区2区三区不卡 | 色综合久久中文字幕综合网 | 亚洲国产精品久久久久久网站 | 成人小视频在线观看免费 | 富士av105| 双性太子 | 免费看黄色片网站 | 亚洲欧美日韩国产一区二区精品 | 成年人视频在线 | 亚洲va天堂va国产va久久 | 日本老师xxxxx18 | 国产码一区二区三区 | 亚裔maricahaseaⅴ | 大伊香蕉在线精品不卡视频 | 精品四虎国产在免费观看 | 12一14性xxxxx国外 | 欧美福利二区 |