Java_数字与字符串

一、封装类
1.Integer类
所有的基本类型,都有对应的类类型
比如int对应的类是Ingeter
这种类就称为封装类

代码示例:

package cn.test.study;

public class TestInteger {

	public static void main(String[] args) {
	        int i = 5;
	         
	        //把一个基本类型的变量,转换为Integer对象
	        @SuppressWarnings("deprecation")
			Integer it = new Integer(i);
	        //把一个Integer对象,转换为一个基本类型的int
	        @SuppressWarnings("unused")
			int i2 = it.intValue();

	}

}

2.Number类
数字封装类有Short,Byte,Integer,Long,Float,Double
这些类都是抽象类Number的子类

代码示例:

package cn.test.study;

public class TestNumber {

	public static void main(String[] args) {
		int i=5;
		@SuppressWarnings("deprecation")
		Integer n1=new Integer(i);
		
		System.out.println(n1 instanceof Number);
		//Integer 是 Number类的子类,所以打印true
	}

}

3.基本类型转封装类

new一个对象(实例化一个引用)

代码示例:

		int j=3;
		//基本类型转封装类
		Integer n2=new Integer(j);

4.封装类转基本类型

比如把一个int类型的封装类转化为基本类型,只需用实例名.intValue()即可

代码示例:

public class TestNumber {
 
    public static void main(String[] args) {
        int i = 5;
 
        //基本类型转换成封装类型
        Integer it = new Integer(i);
         
        //封装类型转换成基本类型
        int i2 = it.intValue();
         
    }
}

5.自动装箱
把基本类型转化为类类型时,不需要调用构造方法,通过=符号自动把基本类型转化为类类型,这个过程叫做自动装箱

代码示例:

 		int i = 5;
        //基本类型转换成封装类型
        Integer it = new Integer(i)//自动转换就叫装箱
        Integer it2 = i;

6.自动拆箱
不需要调用Integer的intValue方法,通过=就自动转换成int类型,就叫拆箱

代码示例:

 		int i = 5;
        Integer it = new Integer(i);  
        //封装类型转换成基本类型
        int i2 = it.intValue();  
        //自动转换就叫拆箱
        int i3 = it;

7.int类型的最大值和最小值

int的最大值和最小值都可以通过其对应封装类Integer的Integer.MAX_VALUE、Integer.MIX_VALUE获得

代码示例:

		 //int的最大值
        System.out.println(Integer.MAX_VALUE);
        //int的最小值      
        System.out.println(Integer.MIN_VALUE);

二、数字和字符串的转化
1.数字转字符串

在Java中,数字转化为字符串有两种方法(目前学到的,嘻嘻)
第一种,使用String类提供的静态方法valueOf,把数字转化为字符串
第二种,使用前面的装箱操作,数字属于基本类型,可以先封装为对象,然后调用对象的toString()方法

代码示例:

 		 int i = 5;   
        //方法1
        String str = String.valueOf(i);   
        //方法2
        Integer it = i;
        String str2 = it.toString();

2.数字字符串转为数字
数字字符串转为数字调用Integer的静态方法parseInt()

代码示例:

		String str = "999";
         
        int i= Integer.parseInt(str);
         
        System.out.println(i);

三、字符

1.Java中用基本类型char来保存一个字符
代码示例:

package character;

public class TestChar {

	public static void main(String[] args) {
		char c1 = 'a';
        char c2 = '1';//字符1,而非数字1
        char c3 = '中';//汉字字符
       // char c4 = 'ab'; //只能放一个字符 编译不通过
	}

}

2.char对应的封装类

char对应的封装类为Character

public class TestChar {
 
    public static void main(String[] args) {
        char c1 = 'a';
        Character c = c1; //自动装箱
        c1 = c;//自动拆箱
         
    }
}

3.char常用的方法

 public static void main(String[] args) {
         
        System.out.println(Character.isLetter('a'));//判断是否为字母
        System.out.println(Character.isDigit('a')); //判断是否为数字
        System.out.println(Character.isWhitespace(' ')); //是否是空白
        System.out.println(Character.isUpperCase('a')); //是否是大写
        System.out.println(Character.isLowerCase('a')); //是否是小写
         
        System.out.println(Character.toUpperCase('a')); //转换为大写
        System.out.println(Character.toLowerCase('A')); //转换为小写
 
        String a = 'a'; //不能够直接把一个字符转换成字符串
        String a2 = Character.toString('a'); //转换为字符串
         
    }

四、字符串

字符串是字符的组合,在Java中,字符串是一个类(String类),所以每一个字符串都是一个对象。 String类是不可被继承的,就是说你创建了一个字符串对象后,这个对象是不可变的(immutable)
immutable意味着:
不能增加长度
不能减少长度
不能插入字符
不能删除字符
不能修改字符
一旦创建好这个字符串,里面的内容永远不能改变,你可以把String的这种“表现”理解为常量。
1.创建字符串的常用方法:
(1)每当遇到一个字面值时,虚拟机就会创建一个字符串;
(2)通过"+"号进行字符串拼接,也有创建出一个新的字符串;
(3)调用String类的构造方法创建一个字符串。

代码示例:

package string;

public class TestString {

	public static void main(String[] args) {
		
		String str="lpx";//字面值,虚拟机碰到字面值就会创建一个字符串对象
		
		String str1=new String("lpx");//创建了两个字符串对象
		
		char[] name=new char[] {'l','p','x'};
		String str2=new String(name);//  通过字符数组创建一个字符串对象
		
		String str3= str2+str1;// 通过+加号进行字符串拼接
	}

}

2.常用操作字符串的方法

toCharArray获取对应的字符数组
charAt获取字符
subString截取子字符串
trim去掉首尾空格
split分隔
toLowerCase, toUpperCase大小写
indexOf,lastIndexOf,contains定位
replaceAll ,replaceFirst替换

3.字符串之间的比较
比较字符串的时候,一定要 清楚是比较它们是不是同一个对象(引用)还是字符串内容。
“==”
“equals()方法”

注意1:是否是同一个对象

代码示例;

public class TestString {
 
    public static void main(String[] args) {
 
        String str1 = "lpx";
         
        String str2 = new String(str1);
         
        //==用于判断是否是同一个字符串对象
        System.out.println( str1  ==  str2);
         
    }
 
}

“==”用于判断两个字符串是不是一个对象,str1和str2属于不同的对象所以结果是false;但str1和str2里的内容是一样的,都是“lpx”,所以equals方法返回true

!!特例:

public class TestString {
 
    public static void main(String[] args) {
        String str1 = "the light";
        String str3 = "the light";
        System.out.println( str1  ==  str3);
    }
 
}

在这里控制台会输出true,为什么呢?编译器每碰到一个字面值,就会创建一个对象。在第4行中,已经创建了字符串对象,在下一行中,编译器发现“lpx”已经创建了,所以就直接拿来用了。实际上“the light”这个字符串是在常量池里,本来的引用是str1,str1指向这个字符串的地址,然后让str2这个引用也指向这个字符串的地址。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值