Java开发必备点_13 包装类和String类

包装类 和 String类

昨日回顾

内部类
	成员内部类
	静态内部类
	局部内部类
	匿名内部类
	
Object
	概念:是所有类的父类 如果定义一个类没有继承任何类 这个类的父类默认是Object
	方法:
		finalize():垃圾回收  Java中垃圾回收是自动机制  当内存中存在类大量的垃圾(没有引用指向的对象) 就会触动垃圾回收机制  我们也可以进行手动通知 通知垃圾回收器 但是不一定管用  
		getClass():返回值类型 打印结果是一个字符串 Class 包名.类名
		toString():打印对象的字符串形式  我们可以自己覆盖 如果不覆盖就是Object原有的方法
		equals():判断两个引用中存放的对象是否是同一个对象 如果自己定义了一个类 那么需要根据实际需求进行覆盖

包装类

引出案例
案例:
在jdk5之前 Object object = 20;//类型不兼容 	 是错误的
Java是面向对象的编程语言 所以对于基本的数据类型我们也要将它看作为对象 由此诞生了包装类 实现Object一统Java的天下
基本数据类型和所对应的包装类
基本数据类型包装类
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean
Integer为例
int ---- Integer类型的转换
案例:	  //将基本数据----->包装类
		//1.基本数据类型转为 包装类 调用的是 有参构造
		int a = 20;
		Integer integer = new Integer(a);
		System.out.println(integer.toString());
		System.out.println("======================");
		//2.通过 valueOf 将一个int  转换为包装类型  静态的 可以通过类名.调用
		int c = 30;
		Integer integer = Integer.valueOf(c);
		System.out.println(integer);
		System.out.println(integer.toString());
=================================================================
    	//将包装类————>基本数据类型
		//3.intValue 将一个包装类转为int类型
		int b = integer.intValue();
		System.out.println(b);
==================================================================
		//基本数据类型 ---->字符串
		int a = 20;
		String string = a+"";
==================================================================
		//将一个字符串 变为包装类
		Integer integer = Integer.valueOf(string);
		System.out.println(integer);
==================================================================
		//可以将 字符串直接转换为  int 数据
		String string = "9430";
		int a = Integer.parseInt(string);
		System.out.println(a);
==================================================================
		//运行报错  因为字符串中存在不能转换为int类型的字符
		String string = "123 45 67";
		
		Integer integer = Integer.valueOf(string);
		System.out.println(integer);
		异常信息:
		Exception in thread "main" java.lang.NumberFormatException: For input string: 			"123 45 67"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Integer.parseInt(Integer.java:492)
	at java.lang.Integer.valueOf(Integer.java:582)
	at cn.baizhi.day14.Demo.main(Demo.java:7)
自动装箱拆箱
从jdk5开始  提供了 基本数据类型和包装类型的自动转换
自动装箱:将基本数据类型自动转换成包装类型
自动拆箱:将包装类型自动转换为基本类型

案例:
Integer integer = 20;//自动装箱  
int a = integer;//自动拆箱
System.out.println(a);

常量池
案例:
//120在内存中只有一份  两个引用都指向了120
Integer integer1 = 120;
Integer integer2 = 120;
		
System.out.println(integer1==integer2);//true


Integer integer1 = 128;
Integer integer2 = 128;
System.out.println(integer1==integer2);//false
System.out.println(integer1.equals(integer2));//true

常量池:当范围在byte之内 -128---127  会直接用常量池中数值 不会创建新的对象
当超过了这个范围 就会创建新的对象
包装类的应用
案例:
package cn.baizhi.day14;

public class Demo {
	public static void main(String[] args) {
		//在学校中  考试的成绩 	0   缺考 
		
		//考试为0分的学生
		Student student1 = new Student("桑邦豪", 0);
		//缺考的学生
		Student student2 = new Student("古煜");
		
		System.out.println(student1.name+"--"+student1.score);
		System.out.println(student2.name+"--"+student2.score);
	}
}

class Student{
	String name;
	Integer score; // 0  0   缺考  null
	
	public  Student() {
		
	}
	public  Student(String name) {
		this.name = name;
	}
	public  Student(String name,int score) {
		this(name);
		this.score = score;//自动装箱
	}
}

String类

创建字符串的方式
第一种:创建的字符串放在了串池中 共享
		//第一种方式
		String string1 = "hello";
		//hello放在了串池中共享
		String string2 = "hello";
		System.out.println(string1==string2);//true

第二种:
		//第二种  通过构造方法创建字符串
		String string = new String("hello");
注意:这行代码创建了两个对象  一个对象 hello字符串 放在了串池中用于共享  一个通过new创建 放到了堆里
案例:
package cn.baizhi.day14;

public class DemoString {
	public static void main(String[] args) {
		//第二种  通过构造方法创建字符串
		//string1 堆里
		String string1 = new String("hello");
		//string2 串池中
		String string2 = "hello";
		System.out.println(string1==string2);//false
		System.out.println(string1.equals(string2));//true
		
	}
}
字符串拼接
String string1 = "hello";
String string2 = "hello";
System.out.println(string1==string2);
string1+="world";
System.out.println(string1==string2);
//字符串不变性 

可变字符串 在当前的字符串后进行追加  
StringBuffer:jdk1.0 线程安全的  但是效率低  一般不用 舍弃
StringBuilder:jdk5  线程不安全的  效率高  常用

案例:
package cn.baizhi.day14;

public class DemoString {
	public static void main(String[] args) {
		StringBuilder stringBuilder1 = new StringBuilder("hello");
		System.out.println(stringBuilder1.hashCode());//地址
		StringBuilder stringBuilder2 = new StringBuilder("hello");
		System.out.println(stringBuilder1==stringBuilder2);//false
		stringBuilder1.append("world");
		System.out.println(stringBuilder1.hashCode());//地址是一样的
	}
}

String类中的方法
1.charAt(int index) 返回 char指定索引处的值。
	案例:
		String string = "asdg";
		char c1  = string.charAt(0);
		char c2  = string.charAt(1);
		char c3  = string.charAt(2);
		char c4  = string.charAt(3);
		System.out.println(c1);
		System.out.println(c2);
		System.out.println(c3);
		System.out.println(c4);
=================================================================
2.contains(CharSequence s) 当且仅当此字符串包含指定的char值序列时才返回true
	案例:
		String string = "asdfgh";
		boolean b = string.contains("z");
		if (b) {
			System.out.println("存在");
		} else {
			System.out.println("不存在");
		}
==================================================================
3.toCharArray() 将此字符串转换为新的字符数组。
	案例:
	package cn.baizhi.day14;


	public class DemoString {
		public static void main(String[] args) {
			String string = "aldfghwsg";
			char[] cs = string.toCharArray();
		
		//遍历
			for (int i = 0; i < cs.length; i++) {
				System.out.print(cs[i]+" ");
			}
	}
}
=================================================================
4.equals(Object anObject) 将此字符串与指定对象进行比较。
	案例:
		String string1 = "刘洋";
		String string2 = "刘洋";
		boolean b = string1.equals(string2);
		System.out.println(b);
=================================================================
5.indexOf(int ch) 返回指定字符第一次出现的字符串内的索引。
	案例:
		String string = "adgdalsdghjasdfl";
		int index = string.indexOf('s');
		System.out.println("第一次出现的下标是:"+index);
=================================================================
6.int indexOf(int ch, int fromIndex) 返回指定字符第一次出现的字符串内的索引,以指定的索引开始搜索。  
	案例:
		String string = "asnfgkshjlfg";
		int index = string.indexOf('s',2);
		System.out.println("下标是:"+index);
=================================================================
7.int indexOf(String str) 返回指定子字符串第一次出现的字符串内的索引。
	案例:
		String string = "asnflgkshjlflg";
		int index1 = string.indexOf("fl");
		System.out.println("下标为:"+index1);//3
=================================================================
8.int indexOf(String str, int fromIndex) 返回指定子串的第一次出现的字符串中的索引,从指定的索引开始。
	案例:
		int index2 = string.indexOf("fl",4);
		System.out.println("下标:"+index2);//11
=================================================================
9.length() 返回此字符串的长度。
	案例:
		String string = "asjghflwsdg";
		int length = string.length();
		System.out.println("字符串的长度是:"+length);
	引申案例:
		String string = "asjghflwsdg";
		//遍历字符串
		for (int i = 0; i < string.length(); i++) {
			System.out.print(string.charAt(i)+":");
		}
=================================================================
10.trim() 返回一个字符串,其值为此字符串,并删除任何前导和尾随空格。
		案例:
		String string = "  shghg  shf  glj  sdfg   ";
		System.out.println("之前:"+string.length());
		String string2 = string.trim();
		System.out.println(string2);
		System.out.println("之后:"+string2.length());
=================================================================
11.split(String regex) 将此字符串分割为给定的 regular expression的匹配。
	案例:
	String string = "asdf:h:sfaa:dsf:aafa:aaf:raf:aa:h:a:t:aaf:a:f";
		String[] strings = string.split(":");
		
		//遍历
		for (int i = 0; i < strings.length; i++) {
			System.out.print(strings[i]+"-");
		}
=================================================================

大数据运算

案例:
		double d1 = 1.0;
		double d2 = 0.9;
		System.out.println(d1-d2);
		
		double d3 = 0;
		System.out.println(d1/d3);

		结果:
		0.09999999999999998
		Infinity  无限的

大数据运算:BigDecimal 类可以实现doublefloat的精确计算
案例:
		BigDecimal b1  = new BigDecimal("1.0");
		BigDecimal b2 = new BigDecimal("0.9");
		System.out.println(b1.add(b2));//1.9 加
		System.out.println(b1.multiply(b2));//乘  0.90
		System.out.println(b1.subtract(b2));//减 0.1
		//System.out.println(b1.divide(b2));
		System.out.println(b1.divide(b2, 8, BigDecimal.ROUND_HALF_UP));

对于除法:可能会出现无限循环小数 就会报异常信息  
解决方法:public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode)
参数:
	divisor:除数
	scale:小数点后保留的位数
	roundingMode:舍入的规则   ROUND_HALF_UP:四舍五入                        
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值