常用类:
一、Object:
Class Object是类层次结构的根类,所有类都直接或者间接的继承自该类,所有对象(包括数组)都实现了这个类的方法。
1、构造方法:
Object() :子类构造方法中,第一行默认有一个super()调用的就是这里的(指的是类没有继承其他的父类了)
2、Object类中的方法:
(1) public int hashCode() : 返回对象的哈希码值。
注意:这里的哈希码值是根据哈希算法计算出来的一个值。这个值和地址有关系,但是这里返回的地址值并不是实际的地址值。
(2) public final Class getClass() : 返回的是该对象的类对象。
返回此Object的运行时类。 返回的类对象是被表示类的static synchronized方法锁定的对象。
(3) public String toString() : 返回对象的字符串表示形式。
这个方法返回一个等于下列值的字符串:
getClass().getName() + '@' + Integer.toHexString(hashCode())
所以我们应该在子类中进行重写toString()方法。
(4) public static String toHexString(int i) : 返回整数参数的字符串表示形式。即将哈希值转化一个地址值。
(5) public boolean equals(Object obj) : 指示一些其他对象是否等于此。
Object类中的equals方法实现底层依旧是 == :
==:
基本数据类型的时候: 比较的是两个值是否一样。
引用数据类型的时候: 比较的是两个对象的地址值是否一样。
equals: 只能比较引用数据类型。
所以我们应该在子类中重写equals()方法。
总结:
子类若是没有重写equals方法,使用的是父类Object类中的方法,比较的是地址值。
子类要是重写了equals方法,比较的是成员变量值是否相同。
(6)protected void finalize() : 简单理解这个方法为用于垃圾回收的,什么时候回收,不确定。
(7)protected Object clone() : 创建并返回此对象的副本。
首先,如果此对象的类不实现接口Cloneable ,则抛出CloneNotSupportedException 。
一个类要想使用clone(),就必须实现Cloneable 接口。并且该类中重写clone方法,通过super()关键字调用。
拷贝在IT行业中常见两种:
(1)浅拷贝:
浅拷贝是指我们拷贝出来的对象的内部引用类型变量和原来的对象内部引用类型变量的地址值是一样的 (指向的是同一个对象)
但是整个拷贝出来的对象和新对象不是同一个地址值。
clone()是浅拷贝,拷贝的时候,对象中的引用数据类型变量的地址值不会变化,会创建一个新的对象赋值。 (2)深拷贝:
全部拷贝对象的内容,包括内存的引用类型也进行拷贝,拷贝的时候,重新创建一个对象,成员变量值和原来被拷贝的一样。
但是后续再对拷贝后的引用数据类型变量做修改,不会影响到原来被拷贝的 (因为指向的不是一个对象) 。
import java.util.Objects; class Student extends Object { private String name; private int age; public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } //重写toString()方法 @Override public String toString() { return "Student2{" + "name='" + name + ''' + ", age=" + age + '}'; //return "姓名:" + name + ",年龄:" + age; //或者自己想修改输出格式 } //重写equals()方法 //s1.equals(s2) @Override public boolean equals(Object o) { //this -- s1 //o -- s2 if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; return age == student.age && Objects.equals(name, student.name); } } //测试类 public class ObjectDemo { public static void main(String[] args) { // 1、public int hashCode() : 返回对象的哈希码值。 Student s1 = new Student(); System.out.println(s1.hashCode()); // 1163157884 Student s2 = new Student(); System.out.println(s2.hashCode()); // 1956725890 //s1与s3哈希码值一样 Student s3 = s1; System.out.println(s3.hashCode()); // 1163157884 System.out.println("========================================="); // 2、public final Class getClass() : 返回的是该对象的类对象。 Student s4 = new Student(); System.out.println(s4.getClass()); //class com.wy.day13.changyonglei.Student // Class stuClass = s4.getClass(); // //Class类下的public String getName()方法: 返回由类对象表示的实体的名称(类,接口,数组类,原始类型或void),作为String 。 // System.out.println(stuClass.getName()); //com.wy.day13.changyonglei.Student //链式编程 System.out.println(s4.getClass().getName()); //com.wy.day13.changyonglei.Student System.out.println("========================================"); //3、public String toString() : 返回对象的字符串表示形式。 Student s5 = new Student(); // System.out.println(s5.toString()); //com.wy.day13.changyonglei.Student@1540e19d // System.out.println(s5.getClass().getName()+"@"+Integer.toHexString(s5.hashCode())); //com.wy.day13.changyonglei.Student@1540e19d //重写toString()方法后: Student s6 = new Student("昭阳", 28); System.out.println(s6.toString()); //Student2{name='昭阳', age=28} System.out.println("========================================"); // 5、public boolean equals(Object obj) : 指示一些其他对象是否等于此。 Student s7 = new Student("昭阳", 28); Student s8 = new Student("昭阳", 28); System.out.println(s7 == s8); // false Student s9 = s7; System.out.println(s7 == s9); // true System.out.println(s7.equals(s8)); // equals没重写前是false,重写后是true System.out.println(s7.equals(s9)); // true System.out.println(s7.equals(s7)); // true 重写equals()方法后: Student s10 = new Student("米彩", 27); System.out.println(s7.equals(s8)); //true System.out.println(s7.equals(s10)); //false } }
二、键盘录入工具:Scanner
用于键盘录入数据,在程序中使用。
1、构造方法:
public Scanner(InputStream source) : 构造一个新的Scanner ,产生从指定的输入流扫描的值。
2、获取键盘上的int类型的数据
nextInt()
3、获取字符串数据
next(): 不会接收到特殊字符
nextLine(): 会接收到特殊字符,如换行符
import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { //创建键盘录入对象 Scanner sc = new Scanner(System.in); //键盘录入一个int类型的数据 //public int nextInt() int num = sc.nextInt(); System.out.println(num); //键盘录入一个字符串 //public String next() String s1 = sc.next(); System.out.println(s1); //public String nextLine() : 将此扫描仪推进到当前行并返回跳过的输入。 //nextLine()可以接收一些特殊的字符,如换行符rn n t // String s2 = sc.nextLine(); // System.out.print(s2); } }
三、字符串 : String
(1)字符串是由多个字符组成的一串数据(字符序列)。
(2)字符串可以看成是字符数组。
1、通过观察API发现 :
(1)String代表的是字符串。属于java.lang包下,所以在使用的时候不需要导包。
(2)String类代表字符串。 Java程序中的所有字符串文字( 例如"abc" )都被实现为此类的实例。(对象)
(3)字符串不变: 它们的值在创建后不能被更改。
字符串是常量,不能改变指的是一旦被赋值,字符串在常量池中的值不能被改变。
2、构造方法 :
(1)public String()
(2)public String(byte[] bytes)
(3)public String(byte[] bytes,int offset,int length)
(4)public String(char[] value)
(5)public String(char[] value,int offset,int count)
(6)public String(String original) public int length() : 返回此字符串的长度 ,如果字符串中没有字符,返回0。
public class StringDemo1 { public static void main(String[] args) { //字符串是常量,它的值在创建后不能被改变 String string = "hello"; string += "world"; System.out.println(string); // helloworld //String构造方法 : // (1) public String() String s = new String(); System.out.println(s); //String类中重写toString()方法 //查看字符串的长度 //public int length() : 返回此字符串的长度 ,如果字符串中没有字符,返回0 System.out.println("字符串s的长度为:" + s.length()); // 0 System.out.println("=========================================="); // (2) public String(byte[] bytes) : 根据一个字节数组创建出一个字符串对象 byte[] bytes = {97, 98, 99, 100, 101}; String s2 = new String(bytes); System.out.println("s2: " + s2); // s2: abcde (其实是:"abcde") System.out.println("字符串s2的长度为:" + s2.length()); // 5 System.out.println("==========================================="); // (3) public String(byte[] bytes,int index,int length) : //将字节数组中的一部分转化成字符串 // int index : 开始索引 int length : 长度() byte[] bytes1 = {97, 98, 99, 100, 101}; String s3 = new String(bytes1, 1, 3); System.out.println("s3: " + s3); // s3 : bcd System.out.println("字符串s3的长度为:" + s3.length()); // 3 System.out.println("==========================================="); //(4) public String(char[] value) : 将字符数组转成一个字符串 char[] c = {'a', 'b', 'c', 'd', '我', '爱', '冯'}; String s4 = new String(c); System.out.println("s4: " + s4); // s4 : abcd我爱冯 System.out.println("字符串s4的长度为:" + s4.length()); // 7 System.out.println("==========================================="); // (5) public String(char[] value,int index,int length) : //将字符数组的一部分转成字符串 // int index : 开始索引 int length : 长度 String s5 = new String(c, 4, 3); System.out.println("s5: " + s5); // s5 : 我爱冯 System.out.println("字符串s5的长度为:" + s5.length()); // 3 System.out.println("==========================================="); // (6) public String(String original) String s6 = "你好"; String s7 = new String(s6); System.out.println("s8: " + s7); // s7 : 你好 System.out.println("字符串s8的长度为:" + s7.length()); // 2 } }
3、== 与 equals 区别:
(1)== : 比较引用数据类型的时候,比较的是地址值。
(2)equals : String类中使用equals方法比较的是字符串的值,因为String类中重写了equals方法。
4、字符串相加规则 :
(1)字符串如果是变量相加 : 先在常量池中开辟空间,然后再做拼接。
(2)字符串如果是常量相加 : 先相加,然后再去常量池中去找。如果找到了就返回,如果没有找到就开辟新的空间,存储拼接后的值。
public class StringDemo2 { public static void main(String[] args) { String s1 = new String("HelloWorld"); //先在常量池中查找"HelloWorld",找到了把地址赋值给s2 String s2 = "HelloWorld"; System.out.println(s1 == s2); // false System.out.println(s1.equals(s2)); // true System.out.println("============================================"); //看程序写结果 : String s3 = new String("hello"); String s4 = new String("hello"); System.out.println(s3 == s4); // false System.out.println(s3.equals(s4)); // true String s7 = "hello"; String s8 = "hello"; System.out.println(s7 == s8); // true System.out.println(s7.equals(s8)); // true System.out.println("============================================="); //字符串相加 : String st1 = "hello"; String st2 = "world"; String st3 = "helloworld"; String st4 = "hello" + "world"; //字符串常量相加 : 先相加,然后再去常量池中去找。 //如果找到了就返回,如果没有找到就开辟新的空间,存储拼接后的值。 System.out.println(st3 == st4); // true //字符串变量相加 : 先在常量池中开辟空间,然后再做拼接。 String st5 = st1 + st2; System.out.println(st3 == st5); // false // System.out.println(st3 == (st1 + st2)); // false System.out.println(st3.equals(st1 + st2)); // true } }
5、String类的判断功能(方法) :
(1)boolean equals(Object obj) : 比较字符串中的内容是否相同, 区分大小写。
(2)boolean equalsIgnoreCase(String str) : 比较字符串中的内容是否相同, 忽略大小写。
(3)boolean contains(String str) : 判断字符串中是否包含指定的字符串。如果包含返回true,反之返回false。 区分大小写。
(4)boolean startsWith(String str) : 测试此字符串是否以指定字符串开头。 区分大小写。
(5)boolean endsWith(String str) : 测试此字符串是否以指定字符串结尾。 区分大小写。
(6)boolean isEmpty() : 判断字符串是否是空字符串。
//注意: "" 不等于 null : String s1 = ""; String s2 = null; System.out.println(s1.equals(s2)); // false System.out.println(s2.equals(s1)); // NullPointerException : 字符串比较的时候,null不能放在前面
public class StringDemo3 { public static void main(String[] args) { String s1 = "helloworld"; String s2 = "Helloworld"; String s3 = "HelloWorld"; // (1) boolean equals(Object obj) : 比较字符串中的内容是否相同, 区分大小写。 System.out.println(s1.equals(s2)); // false System.out.println(s1.equals(s3)); // false System.out.println("==================================="); // (2) boolean equalsIgnoreCase(String str) : 比较字符串中的内容是否相同, //忽略大小写。 System.out.println(s1.equalsIgnoreCase(s2)); // true System.out.println(s1.equalsIgnoreCase(s3)); // true System.out.println("==================================="); // (3) boolean contains(String str) : 判断字符串中是否包含指定的字符串。如果包含返回true,反之返回false。 //区分大小写。 System.out.println(s1.contains("Hello")); // false System.out.println(s1.contains("hel")); // true System.out.println(s1.contains("owo")); // true System.out.println("==================================="); // (4) boolean startsWith(String str) : 测试此字符串是否以指定字符串开头。 //区分大小写。 System.out.println(s1.startsWith("hel")); // true System.out.println(s1.startsWith("Hel")); // false System.out.println(s1.startsWith("hell34")); // false System.out.println("==================================="); // (5) boolean endsWith(String str) : 测试此字符串是否以指定字符串结尾。 //区分大小写。 System.out.println(s1.endsWith("rld")); // true System.out.println(s1.endsWith("rlD")); // false System.out.println("==================================="); // (6) boolean isEmpty() : 判断字符串是否是空字符串。 System.out.println(s1.isEmpty()); // false System.out.println("==================================="); //需求1:比较s6的值是否和s7的值一样 String s6 = null; String s7 = "hello"; if (s6 != null) { if (s6.equals(s7)) { System.out.println("s6与s7的值是一样的"); } } else { System.out.println("s6是null值"); } //需求2:判断s6的值是否是hello if ("hello".equals(s6)) { //将"hello"放前面调方法 System.out.println("s6的值是hello"); } else { System.out.println("s6的值不是hello"); } } }
6、String类的获取功能(方法) :
(1)int length() : 获取字符串的长度。
(2)char charAt(int index) : 返回指定索引处的字符。
(3)int indexOf(int ch) : 返回指定字符第一次出现在字符串内的索引。
(4)int indexOf(String str) : 返回指定小字符串第一次出现在大字符串内的索引。
(5)int indexOf(int ch,int fromIndex) : 返回指定字符第一次出现在字符串内的索引,以指定的索引开始搜索。
(6)int indexOf(String str,int fromIndex) : 返回指定小字符串第一次出现在大字符串内的索引,以指定的索引开始搜索。
(7)String substring(int start) : 从指定位置处截取字符串,包括开始截取的位置,截取到末尾。
(8)String substring(int start,int end) : 截取字符串的一部分。截取的串从start位置开始,到end-1的位置结束。
public class StringDemo4 { public static void main(String[] args) { String s = "helloworld"; // (1) int length() : 获取字符串的长度。 System.out.println("字符串s的长度为:" + s.length()); // 字符串s的长度为:10 System.out.println("=================================="); // (2) char charAt(int index) : 返回指定索引处的字符,超出索引范围报错。 //范围: 0 <= index <= length()-1 System.out.println(s.charAt(4)); // o System.out.println(s.charAt(0)); // h // System.out.println(s.charAt(100)); //StringIndexOutOfBoundsException System.out.println(s.charAt(9)); // d System.out.println("=================================="); // (3) public int indexOf(int ch) : 返回指定字符第一次出现在字符串内的索引。 //找到返回索引值,没有找到返回-1。 System.out.println(s.indexOf('o')); // 4 System.out.println(s.indexOf(97)); // -1 (97指的是'a') System.out.println("=================================="); // (4) public int indexOf(String str) : 返回指定小字符串第一次出现在大字符串内的索引。 //找到返回索引值,没有找到返回-1。 System.out.println(s.indexOf("owo")); // 4 System.out.println(s.indexOf("owe")); // -1 System.out.println("=================================="); // (5) public int indexOf(int ch,int fromIndex) : 返回指定字符第一次出现在字符串内的索引,以指定的索引开始搜索。 //找到返回索引值,没有找到返回-1。 //如果找到了,返回的是字符在整个字符串中的索引 System.out.println(s.indexOf("l", 4)); // 8 System.out.println(s.indexOf("l", 1000)); // -1 System.out.println(s.indexOf("p", 4)); // -1 System.out.println("=================================="); // (6) public int indexOf(String str,int fromIndex) : 返回指定小字符串第一次出现在大字符串内的索引,以指定的索引开始搜索。 //找到返回索引值,没有找到返回-1。 //如果找到了,返回的是字符在整个字符串中的索引 System.out.println(s.indexOf("owo", 4)); // 4 System.out.println(s.indexOf("owo", 1000)); // -1 System.out.println(s.indexOf("owe", 1000)); // -1 System.out.println("=================================="); // (7) String substring(int start) : 从指定位置处截取字符串,包括开始截取的位置,截取到末尾。 //若给的索引值不存在,报错。 System.out.println(s.substring(3)); // loworld // System.out.println(s.substring(100)); //StringIndexOutOfBoundsException System.out.println("=================================="); // (8) String substring(int start,int end) : 截取字符串的一部分。截取的串从start位置开始,到end-1的位置结束,[,)。 //若给的索引值不存在,报错。 System.out.println(s.substring(3, 8)); // lowor // System.out.println(s.substring(1,20)); //StringIndexOutOfBoundsException } }
7、String类的转换功能(方法) :
(1)byte[] getBytes() : 将字符串转成字节数组。
(2)char[] toCharArray() : 将字符串转成字符数组。
(3)static String valueOf(char[] chs) : 将字符数组转成字符串。
(4)static String valueOf(int i) : 将int类型的数据转成字符串。 多用在 *** 作数据库时。
(5)String toLowerCase() : 将字符串中的内容全部转小写。
(6)String toUpperCase() : 将字符串中的内容全部转大写。
(7)String concat(String str) : 将指定字符串拼接到大字符串的后面。
*很重要* (8)public String[] split(String regex) : 将字符串按照指定格式拆分成新的字符串。
public class StringDemo5 { public static void main(String[] args) { String s = "HelloWorLD"; // (1) public byte[] getBytes() : 将字符串转成字节数组。 byte[] bytes = s.getBytes(); for (int i = 0; i < bytes.length; i++) { if (i == 0) { System.out.print("[" + bytes[i] + ","); } else if (i == bytes.length - 1) { System.out.print(bytes[i] + "]"); } else { System.out.print(bytes[i] + ","); } } //[72,101,108,108,111,87,111,114,76,68] System.out.println(); System.out.println("================================="); // (2) char[] toCharArray() : 将字符串转成字符数组。 char[] chars = s.toCharArray(); for (int i = 0; i < chars.length; i++) { if (i == 0) { System.out.print("[" + chars[i] + ","); } else if (i == chars.length - 1) { System.out.print(chars[i] + "]"); } else { System.out.print(chars[i] + ","); } } // [H,e,l,l,o,W,o,r,L,D] System.out.println(); System.out.println("================================="); // (3) static String valueOf(char[] chs) : 将字符数组转成字符串。 //static修饰的方法,可以直接通过String类名调用 String s1 = String.valueOf(chars); System.out.println(s1); // HelloWorLD System.out.println("================================="); // (4) static String valueOf(int i) :将int类型的数据转成字符串。多用在操作数据库时。 String s2 = String.valueOf(100); // 将数字100转换成字符串"100" System.out.println(s2); // 100 (其实是"100") System.out.println("================================="); // (5) String toLowerCase() : 将字符串中的内容全部转小写。 // String s = "HelloWorLD"; String s3 = s.toLowerCase(); System.out.println(s3); // helloworld System.out.println("================================="); // (6) String toUpperCase() : 将字符串中的内容全部转大写。 String s4 = s.toUpperCase(); System.out.println(s4); // HELLOWORLD System.out.println("================================="); // (7) String concat(String str) : 将指定字符串拼接到大字符串的后面。 String s5 = s.concat("hadoop"); System.out.println(s5); // HelloWorLDhadoop System.out.println("================================="); // (8) public String[] split(String s) :将字符串按照指定格式拆分成新的字符串。 String s6 = "hello world hello java world"; String[] strings = s6.split(" "); //以" "为界拆分 for (int i = 0; i < strings.length; i++) { System.out.print(strings[i] + " "); // hello world hello java world } } }
8、String类的其他功能(方法) :
(1)替换功能 :
(1)String replace(char old,char new) : 将新的字符替换字符串中指定的所有字符,并返回一个替换后的字符串。
(2)String replace(String old,String new) : 将新的字符串替换字符串中指定的所有字符串,并返回一个替换后的字符串。
(2)去除字符串两空格 :
(1)String trim() : 去除字符串两边的若干个空格。 (3)按字典顺序比较两个字符串 :
(1)int compareTo(String str) : 比较字符串是否相同, 区分字符ASCII码大小写。。
(2)int compareToIgnoreCase(String str) : 比较字符串是否相同, 比较的字符统一大小写。
public class StringDemo6 { public static void main(String[] args) { String s = "helloworodl"; // (1) String replace(char old,char new) : 将新的字符替换字符串中指定的所有字符,并返回一个替换后的字符串。 String s1 = s.replace('l', 'q'); System.out.println(s); // helloworodl System.out.println(s1); // heqqoworodq System.out.println("===================================="); // (2) String replace(String old,String new) : 将新的字符串替换字符串中指定的所有字符串,并返回一个替换后的字符串。 String s2 = s.replace("owo", "==="); System.out.println(s); // helloworodl System.out.println(s2); // hell===rodl System.out.println(); String s3 = s.replace("owo", "@@@@"); System.out.println(s); // helloworodl System.out.println(s3); // hell@@@@rodl System.out.println(); //如果被替换的字符串不存在, 则返回原本的字符串。 String s4 = s.replace("qwer", "LOL"); System.out.println(s); // helloworodl System.out.println(s4); // helloworodl System.out.println("===================================="); // (1) String trim() : 去除字符串两边的若干个空格。 String s5 = " hello world "; System.out.println(s5); // hello world System.out.println(s5.trim()); //hello world System.out.println("===================================="); // (1) int compareTo(String str) : 比较字符串是否相同, 区分字符ASCII码大小写。 //如果相同返回0; 若不同则按位比较,结果是前者字符ASCII码减去后者字符ASCII码。 String s6 = "hello"; // h的ASCII码值是 104 e: 101 String s7 = "hello"; String s8 = "abc"; // a: 97 // String s9 = "qwe"; // q: 113 String s9 = "hwe"; // h: 104 w: 119 System.out.println(s6.compareTo(s7)); // 0 System.out.println(s6.compareTo(s8)); // 7 (h - a : 104 - 97 = 7) System.out.println(s6.compareTo(s9)); // -18 (e - w : 101-119=-18) System.out.println(); // 若按位比较直到其中一个字符串比完,而另一个还没比完,那么结果是前者字符串个数减去后者字符串个数。 String s10 = "hel"; System.out.println(s6.compareTo(s10)); //2 (s6个数:5 s10个数:3 5-3=2) System.out.println("====================================="); // (2)int compareToIgnoreCase(String str) : 比较字符串是否相同。比较的字符统一大小写。 String s11 = "Abc"; // A:65 a:97 System.out.println(s6.compareTo(s11)); // 39 (104-65=39) System.out.println(s6.compareToIgnoreCase(s11)); // 7 (104-97=7) } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)