java中数据类型与包装类

 

一、包装类

 

        在这八个类名中,除了Integer和Character类以后,其它六个类的类名和基本数据类型一直,只是类名的第一个字母大写即可。
对于包装类说,这些类的用途主要包含两种:

 a、作为和基本数据类型对应的类类型存在,方便涉及到对象的操作。

 b、包含每种基本数据类型的相关属性如最大值、最小值等,以及相关的操作方法。

 由于八个包装类的使用比较类似,下面以最常用的Integer类为例子介绍包装类的实际使用。

 1、实现int和Integer类之间的转换

在实际转换时,使用Integer类的构造方法和Integer类内部的intValue方法实现这些类型之间的相互转换,实现的代码如下:

 int n = 10;

 Integer in = new Integer(100);

 //将int类型转换为Integer类型

 Integer in1 = new Integer(n);

 //将Integer类型的对象转换为int类型

 int m = in.intValue();

 2、Integer类内部的常用方法

在Integer类内部包含了一些和int操作有关的方法,下面介绍一些比较常用的方法:

 a、parseInt方法

public static int parseInt(String s)

该方法的作用是将数字字符串转换为int数值。在以后的界面编程中,将字符串转换为对应的int数字是一种比较常见的操作。使用示例如下:

 String s = “123”;

 int n = Integer.parseInt(s);

则int变量n的值是123,该方法实际上实现了字符串和int之间的转换,如果字符串都包含的不是都是数字字符,则程序执行将出现异常。(说明:异常的概念将在下一章进行讲述)

另外一个parseInt方法:

 public static int parseInt(String s, int radix)

则实现将字符串按照参数radix指定的进制转换为int,使用示例如下:

 //将字符串”120”按照十进制转换为int,则结果为120

 int n = Integer.parseInt(“120”,10);

 //将字符串”12”按照十六进制转换为int,则结果为18

 int n = Integer.parseInt(“12”,16);

 //将字符串”ff”按照十六进制转换为int,则结果为255

 int n = Integer.parseInt(“ff”,16);

这样可以实现更灵活的转换。

 b、toString方法

public static String toString(int i)

该方法的作用是将int类型转换为对应的String类型。

使用示例代码如下:

 int m = 1000;

 String s = Integer.toString(m);

则字符串s的值是”1000”。

另外一个toString方法则实现将int值转换为特定进制的字符串:

 public static int parseInt(String s, int radix)

使用示例代码如下:

 int m = 20;

 String s = Integer.toString(m);

则字符串s的值是”14”。

 其实,JDK自从1.5(5.0)版本以后,就引入了自动拆装箱的语法,也就是在进行基本数据类型和对应的包装类转换时,系统将自动进行,这将大大方便程序员的代码书写。使用示例代码如下:

 //int类型会自动转换为Integer类型

 int m = 12;

 Integer in = m;

 //Integer类型会自动转换为int类型

 int n = in;

 所以在实际使用时的类型转换将变得很简单,系统将自动实现对应的转换。

3、String类总结

1,获取。
1.1 字符串中的包含的字符数,也就是字符串的长度。
int length():获取长度。
1.2 根据位置获取位置上某个字符。
char charAt(int index):
1.3 根据字符获取该字符在字符串中位置。
int indexOf(int ch):返回的是ch在字符串中第一次出现的位置。
int indexOf(int ch, int fromIndex) :从fromIndex指定位置开始,获取ch在字符串中出现的位置。
int indexOf(String str):返回的是str在字符串中第一次出现的位置。
int indexOf(String str, int fromIndex) :从fromIndex指定位置开始,获取str在字符串中出现的位置。

int lastIndexOf(int ch)

2,判断。
2.1 字符串中是否包含某一个子串。
boolean contains(str):
特殊之处:indexOf(str):可以索引str第一次出现位置,如果返回-1.表示该str不在字符串中存在。
所以,也可以用于对指定判断是否包含。
if(str.indexOf("aa")!=-1)
而且该方法即可以判断,有可以获取出现的位置。
2.2 字符中是否有内容。
boolean isEmpty(): 原理就是判断长度是否为0.
2.3 字符串是否是以指定内容开头。
boolean startsWith(str);
2.4 字符串是否是以指定内容结尾。
boolean endsWith(str);
2.5 判断字符串内容是否相同。复写了Object类中的equals方法。
boolean equals(str);
2.6 判断内容是否相同,并忽略大小写。
boolean equalsIgnoreCase();
3,转换。
3.1 将字符数组转成字符串。
构造函数:String(char[])
 String(char[],offset,count):将字符数组中的一部分转成字符串。
静态方法:
static String copyValueOf(char[]);
static String copyValueOf(char[] data, int offset, int count)
static String valueOf(char[])
3.2 将字符串转成字符数组。**
char[] toCharArray():
3.3 将字节数组转成字符串。
String(byte[])
String(byte[],offset,count):将字节数组中的一部分转成字符串。
3.4 将字符串转成字节数组。
byte[]  getBytes():
3.5 将基本数据类型转成字符串。
static String valueOf(int)
static String valueOf(double)
//3+"";//String.valueOf(3);
特殊:字符串和字节数组在转换过程中,是可以指定编码表的。
4,替换
String replace(oldchar,newchar);
5,切割
String[] split(regex);
6,子串。获取字符串中的一部分。
String substring(begin);
String substring(begin,end);
7,转换,去除空格,比较。
7.1 将字符串转成大写或则小写。
String toUpperCase();

String toLowerCase();

7.2 将字符串两端的多个空格去除。
String trim();
7.3 对两个字符串进行自然顺序的比较。

int compareTo(string);

代码示例:

 

class StringTest  
{  
    public static void main(String[] args)  
    {  
        String s = "   ni shi    ";  
        myTrim1(s);  
        myTrim2(s);  
          
    }  
  
    public static void myTrim1(String s)  
    {  
        int start = 0;  
        int end = s.length()-1;  
        int count1 = 0;  
        int count2 = 0;  
  
        for (start=0; s.charAt(start)==' ' && start<=end; start++)  
        {  
            count1++;       //按照for循环顺序,count先自增,start再自增,因此start与count相等  
        }  
        for (end=s.length()-1; s.charAt(end)==' ' && start<=end; end--)  
        {  
            count2 = end;   //count比最终end的结果大1,正好下面substring中不需要加一  
        }  
  
        String s2 = s.substring(count1,count2);  
  
        System.out.println(s2.length());  
        System.out.println(s2);  
    }  
  
    public static void myTrim2(String str)  
    {  
        int start = 0,end = str.length()-1;  
  
        while(start<=end && str.charAt(start)==' ')  
            start++;  
  
        while(start<=end && str.charAt(end)==' ')  
            end--;  
  
        String s2 = str.substring(start,end+1);  
        System.out.println(s2.length());  
        System.out.println(s2);  
    }  
}  

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值