21天养成好习惯_第十八天
作者:互联网
21天养成好习惯_第十八天
今天继续学习Java的常用类的知识
基本类型对应的包装类图示
object类方法
- object是所有类的父类,所以每个类都有其基本的方法
- getClass() 获的此对象的类型
- hashCode() 得到此对象的hash值, 类似于C++的指针
- toString() 得到此对象的String表示, 一般会由程序员重写以满足程序员需要
- equals() 默认判断是否是同一个对象的引用 , 一般会由程序员重写以满足程序员的需要
- finalize() 对象销毁的时候调用 , 类似于C++的析构函数
其中需要提一下的是重写equals方法的步骤
1. 比较两个引用是否指向同一个对象
2. 判断obj是否为null
3. 判断两个引用指向的实际对象类型是否一致
4. 强制类型装换
5. 依次比较各个属性值是否相同
String类方法大杂烩
package BUPALAOYE;
public class StringDemo3 {
public static void main(String[] args) {
String str = "java是最好的编程语言,java牛逼,java真香";
System.out.println(str.length());
System.out.println(str.charAt(str.length()-1));
System.out.println(str.contains("java"));
System.out.println(str.toCharArray());
System.out.println(str.indexOf("java"));//返回第一次出现
System.out.println(str.indexOf("java",5));
//从下标5开始匹配字符串的下标 (小重载)
System.out.println(str.lastIndexOf("java"));//返回最后一次出现的下标
System.out.println("startWith('java') : " + str.startsWith("java"));
System.out.println("endWith(\"java\") : " + str.endsWith("java"));
System.out.println("----------------------");
String str2 = " hello World ";
System.out.println("trim before : "+str2);
System.out.println("trim after : "+str2.trim());//剔除字符串的前后字符
System.out.println("toUpperCase() : " + str2.toUpperCase());
System.out.println("toLowerCase() : " + str2.toLowerCase());
String str3 = str2.replace("World", "has been replaced");
System.out.println(str3);
//其原来的字符串并没有改变
String[] arr = str3.split("[ ,]+");
//这代表可以用'空格'或'逗号'切割,加号代表把连续的空格当一个
for (String s : arr) {
System.out.println(s);
}
//利用equals和compare比较大小
String str4 = "hello";
String str5 = "Hello";
System.out.println(str4.equals(str5)); //比较是否相等
System.out.println(str4.equalsIgnoreCase(str5));//忽略大小写
System.out.println(str4.compareTo(str5)); //返回int 第一个不相同的Assic值
System.out.println(str4.compareToIgnoreCase(str5));//忽略大小写
String str6 = "abc";
String str7 = "abcdef";
String str8 = "bbcddsaf";
System.out.println(str6.compareTo(str7));
System.out.println(str6.compareTo(str8));
//compare 优先返回不相同字符的差,若都相同,则返回字数的差
}
}
运行结果:
26
香
true
java是最好的编程语言,java牛逼,java真香
0
13
20
startWith('java') : true
endWith("java") : false
----------------------
trim before : hello World
trim after : hello World
toUpperCase() : HELLO WORLD
toLowerCase() : hello world
hello has been replaced
hello
has
been
replaced
false
true
32
0
-3
-1
Process finished with exit code 0
装箱和拆箱
定义:装箱是将值类型装换成引用类型的过程;拆箱就是将引用类型转换成值类型的过程;
参考资料: 装箱和拆箱
标签:第十八天,养成,java,21,System,String,str,println,out 来源: https://blog.csdn.net/qq_51280198/article/details/121213605