Day15 object、包装类、String
一、equals()方法
指定某个对象和当前对象是否"相等"(地址)
重写后,可以比较两个对象内容是否相等
String s1 = new String("abc");
String s2 = new String("abc");
System.out.println(s1==s2);//new表示在堆中开辟新的空间存储数据,双等号比较的是new出来的两块对内存
System.out.println(s1.equals(s2));//String类中重写了equals方法,比较的是两个字符串内容是否相等
代码实现:
public boolean equals(Object obj){
if(this == obj){
return true;
}
if(obj == null){
return false;
}
if(this.getClass() != obj.getClass()){
return false;
}
Student s = (Student)obj;
if(this.name.equals(s.name) && this.age == s.age && this.sex.equals(s.sex) && this.score == s.score) {
return true;
}
return false;
}
二、包装类
3.1 概念
基本数据类型对应的引用数据类型
3.2基本数据类型对应的包装类
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
3.3数据的装箱与拆箱
拆箱:将包装类型数据转换成基本类型数据
装箱:将基本类型数据包装成包装类型数据
装箱和拆箱的方法:
装箱:使用包装类中的构造方法,或静态valueOf方法int i = 5; Integer i = new Integer(5); //或 double d = 5.5; double d = Double.valueOf(d);
拆箱:使用包装类的xxValue()方法
Integer i = new Integer(5); int a = i.intValue();
JDK 5.0之后,自动装箱和拆箱
自动装箱:可以直接将基本数据类型赋给包装类
int a = 5; Integer i = a;
自动拆箱:直接将包装类对象数据赋值给基本类型变量
Integer i = 5; int a = i;
3.4基本数据类型和字符串之间的相互转换
基本类型转字符串:
1)字符串连接符:任何基本类型数据与字符串链接都变成字符串形式
int a = 5;
//将基本类型变量与一个空字符串链接
String str = a + "";
2)String类中的valueOf方法:
boolean boo = true;
String str = String.valueOf(boo);
字符串转基本类型:
1)包装类中的parseXXX方法:
注意:
i. 字符串不能直接转成字符类型,需要使用String类中的charAt()方法取字符串中的第一个字符
ii. 若字符串转数值类型时,若字符串中存在不能表示数值的字符时,抛出
java.lang.NumberFormatException异常
iii. 字符串转布尔类型时,当且仅当字符串是“true”时,结果为true,否则其他任意字符串转布尔类型结
果都是false
三、String 字符串
4.1概念
字符串数据是常量,存储在常量池中,常量池中不允许存储相同的数据,字符串可以直接将数据赋值给对象引用.
数据共享。
4.2字符串创建对象
String s = new String("abc");
4.3字符串常用方法
public int length();//返回字符串的长度
String s= new String("abcdef"); int len = s.length();//5
public char charAt(int index) //获取指定下标的字符
String s= new String("abcdef"); char c = s.charAt(3);//返回索引位置为3的字符
public String concat(String str) //在原字符串后拼接str
String s1 = "abc"; String s2 = "def"; String s3 = s1.concat(s2); System.out.println(s3);//abcdef
public boolean endsWith(String suffix) //判断是否为指定后缀
boolean b = s.endsWith("OK");//判断s字符串是否以OK结尾。
public boolean startsWith(String prefix)//判断是否为指定前缀
boolean b = s.startsWith("OK");//判断s字符串是否以OK开头。
public boolean equalsIgnoreCase(String anotherString) //忽略大小写比较
String s1 = "abc"; String s2 = "ABC"; boolean b = s1.equalsIgnoreCase(s2);//两个字符串长度相同,内容相同,忽略大小写,true
public int indexOf(int ch, int fromIndex) //从fromIndex开始查询ch首次出现的下标
/* int indexOf(int ch) 返回指定字符在此字符串中第一次出现处的索引。 int indexOf(int ch, int fromIndex) 返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。 int indexOf(String str) 返回指定子字符串在此字符串中第一次出现处的索引。 int indexOf(String str, int fromIndex) 返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。 */ int index = s1.indexOf('a');//获取a在s1字符串中首次出现的位置 int index = s1.indexOf('a',3);//获取'a'字符在s1字符串中从索引为3开始第一次出现的索引 //注意:查找字符串时,返回的是第一个字母的下标 int index3 = s.indexOf("abc");//获取"abc"字符串在s字符串中第一次出现的索引 //注意:查询没有结果时则返回-1 int index4 = s.indexOf("abc",4);//获取"abc"字符串在s字符串中从索引为4开始第一次出现的索引
public int lastIndexOf(int ch) //查询ch最后一次出现的下标
int index = s1.lastIndexOf('a');//查询字符a在s1字符串中最后一次出现的位置
public boolean isEmpty() //判断是否为空串
/* boolean isEmpty() 当且仅当 字符串的length() 为 0 时返回 true。 */ boolean b2 = s.isEmpty();//判断s字符串是否是空字符串
public String replace(char oldChar, char newChar) //替换
/* String replace(char oldChar, char newChar) 返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。 String replace(CharSequence target, CharSequence replacement) 使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。 */ String s = "hello java"; //注意:会替换原字符串中所有的指定字符 String s1 = s.replace('a','k'); System.out.println(s1); //注意:将要替换的字符串可以和被替换的字符串长度不相等,当做一个整体被替换掉 String s2 = s.replace("java","php"); System.out.println(s2);
public String[] split(String regex) //依据regex拆分
String s1="a,b,c,d,e,f"; String[] ss = s1.split(',');//根据,符号将字符串拆分成一个数组
public String substring(int beginIndex, int endIndex) //截取子串
/* String substring(int beginIndex) 返回一个新的字符串,它是此字符串的一个子字符串。 String substring(int beginIndex, int endIndex) 返回一个新字符串,它是此字符串的一个子字符串。 */ //从指定的下标开始截取后半部分 String str2 = str1.substring(3); System.out.println(str2); //包头不包尾:截取指定区间的子字符串 String str3 = str1.substring(3,9); System.out.println(str3);
public String toLowerCase() //转小写
String s1 = "ABC"; System.out.println(s1.toLowerCase());//abc
public String toUpperCase() //转大写
String s1 = "abc"; System.out.println(s1.toUpperCase());//ABC
public String trim() //去掉前后空格
String s1 = " abcdef "; System.out.println(s1.trim());//打印abcdef 前后空格消失
四、StringBuffer
字符串缓冲区:使用缓冲区的字符串进行操作要比直接操作字符串效率高。
适用多线程下在字符缓冲区进行大量操作的情况
5.1常用方法
5.1.1增加
/*
StringBuffer append(String str) 追加
StringBuffer insert(int offset, String str) 插入
*/
StringBuffer sb2 = sb1.append("hello");
//区别于String类:面盆理论
System.out.println(sb1);
System.out.println(sb2);
System.out.println(sb1 == sb2);
sb1.append("java");
System.out.println(sb1);
System.out.println(sb2);
//方法链
sb1.append("java").append("java").append("java").append("java");
System.out.println(sb1);
System.out.println(sb2);
//插入
sb1.insert(2,"hhhhhhhh");
System.out.println(sb1);
5.1.2删除
/*
StringBuffer delete(int start, int end)
StringBuffer deleteCharAt(int index)
*/
//删除指定区间的字符串
sb1.delete(2,3);
System.out.println(sb1);
//删除指定位置上的字符
sb1.deleteCharAt(0);
System.out.println(sb1);
5.1.3修改
/*
StringBuffer replace(int start, int end, String str)
void setCharAt(int index, char ch)
*/
//替换指定区间的字符串
sb1.replace(2,5,"nnnnnn");
System.out.println(sb1);
//替换指定位置上的字符
sb1.setCharAt(0,'x');
System.out.println(sb1);
5.1.4获取
//和String类中的用法相同
/*
indexOf
lastIndexOf
charAt
length
substring
*/
5.1.5反转(倒序、颠倒)
// StringBuffer reverse()
StringBuffer sb3 = new StringBuffer("my name is zhansan");
sb3.reverse();
System.out.println(sb3);
五、StringBuilder
StringBuilder类也是字符串缓冲区,类中的方法与StringBuffer类中的方法使用方法一样,区别在于StringBuilder类中的方法都是非线程安全的,而StringBuffer类中的方法都是线程安全的
适用于单线程下在字符缓冲区进行大量操作的情况
六、BigDecimal
public static void main(String[] args){ double d1 = 1.0; double d2 = 0.9; System.out.println(d1-d2);//得到的结果是近似值 }
很多时候,实际应用过程中需要精确计算,而double是近似值存储,不符合需求,所以使用BigDecimal
7.1 BigDecimal
位置:java.lang包下
作用:精确的计算浮点数
创建方式:BigDecimal bd = new BigDecimal(“1.0”);
方法:
BigDecimal add(BigDecimal bd); //加
BigDecimal subStract(BigDecimal bd); //减
BigDecimal multiply(BigDecimal bd); //乘
BigDecimal divide(BigDecimal bd); //除
代码实现:
public static void main(String[] args){
BigDecimal bd1 = new BigDecimal("1.0");
BigDecimal bd2 = new BigDecimal("0.9");
BigDecimal result1 = bd1.add(bd2);//1.9
System.out.println(result1);
BigDecimal result2 = bd1.subStract(bd2);//0.1
System.out.println(result2);
BigDecimal result3 = bd1.multiply(bd2);//0.90
System.out.println(result3);
BigDecimal result4 = bd1.divide(bd2);//这里出错,当进行除法运算时,需要指定保留的位数和取舍方式
System.out.println(result4);
}
使用BigDecimal(BigDecimal bd,int scal,RoundingMode mode);
参数scal:指定精确到小数点后几位
参数mode:指定小数部分的取舍模式,通常采用四舍五入
BigDecimal.ROUND_HALF_UP;