Object类,Scanner的使用(了解),String类的概述和使用(掌握)

1.Object类(掌握)
(1)Object是类层次结构的根类,所有的类都直接或者间接的继承自Object类。
(2)Object类的构造方法有一个,并且是无参构造
这其实就是理解当时我们说过,子类构造方法默认访问父类的构造是无参构造
(3)要掌握的方法:
A:toString()
返回对象的字符串表示,默认是由类的全路径+'@'+哈希值的十六进制表示。
这个表示其实是没有意义的,一般子类都会重写该方法。

B:equals()
比较两个对象是否相同。默认情况下,比较的是地址值是否相同。
而比较地址值是没有意义的,所以,一般子类也会重写该方法。

(4)要了解的方法:
A:hashCode() 返回对象的哈希值。不是实际地址值,可以理解为地址值
代码: public Student(String name, int age) {
super();
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;
}
@Override
//调用 对象名。toString() /对象名
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";

}


package class01;

public class ToStringTest {
public static void main(String[] args) {
Student s=new Student();

System.out.println(s.toString());//Student [name=null, age=0]


System.out.println(s);//Student [name=null, age=0]
/*s.toString();*/
}

}

B:getClass() 返回对象的字节码文件对象,反射中我们会详细讲解

package class01;

public class GetClassTest {
public static void main(String[] args) {
//getClass()返回对象运行时的类
Student s=new Student();
Class c=s.getClass();//运行时类


String name=c.getName();//返回该类名称
System.out.println(name);
}

}


C:finalize() 用于垃圾回收,在不确定的时间

D:clone() 可以实现对象的克隆,包括成员变量的数据复制,
但是它和两个引用指向同一个对象是有区别的。


(5)两个注意问题;
A:直接输出一个对象名称,其实默认调用了该对象的toString()方法。

2.Scanner的使用(了解)
(1)在JDK5以后出现的用于键盘录入数据的类。
(2)构造方法:
A:常用的格式
Scanner sc = new Scanner(System.in);
(3)基本方法格式:
A:hasNextXxx() 判断是否是某种类型的
B:nextXxx() 返回某种类型的元素


(4)要掌握的两个方法
A:public int nextInt()

B:public String nextLine()


代码: package Class02;

import java.util.Scanner;

public class ScannerTest {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("请输入");

/*String st=input.next();*/
String st=input.nextLine();
System.out.println(st);
}

}



3.String类的概述和使用(掌握)
(1)多个字符组成的一串数据。
其实它可以和字符数组进行相互转换。
(2)构造方法:
A:public String()
B:public String(byte[] bytes)
C:public String(byte[] bytes,int offset,int length)
D:public String(char[] value)
E:public String(char[] value,int offset,int count)
F:public String(String original)
下面的这一个虽然不是构造方法,但是结果也是一个字符串对象
G:String s = "hello";
(3)字符串的特点
A:字符串一旦被赋值,就不能改变。
注意:这里指的是字符串的内容不能改变,而不是引用不能改变。
B:字面值作为字符串对象和通过构造方法创建对象的不同
String s = new String("hello");和String s = "hello"的区别?
* 有。前者会创建2个对象,后者创建1个对象。


(4)字符串的面试题(看程序写结果)
A:==和equals()
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1 == s2);// false
System.out.println(s1.equals(s2));// true

String s3 = new String("hello");
String s4 = "hello";
System.out.println(s3 == s4);// false
System.out.println(s3.equals(s4));// true

String s5 = "hello";
String s6 = "hello";
System.out.println(s5 == s6);// true
System.out.println(s5.equals(s6));// true
(5)==和equals的区别
* ==:比较引用类型比较的是地址值是否相同
* equals:比较引用类型默认也是比较地址值是否相同,
而String类重写了equals()方法,比较的是内容是否相同。


char[] char1={'a','b','c','d','e'};

String str6 =new String(char1);
System.out.println(str6);

String str7=new String(char1,1,3);//截取数组长度从下标1开始,长度为3结束。
System.out.println(str7);

另一种:byte:
byte[] byte1={1,2,3,4,5,6};
String str4=new String(byte1);//把byte转换为字符串
System.out.println(str4);



String str5=new String(byte1,2,2);//数组下标2开始,截取数组长度为二
System.out.println(str5);

原文地址:https://www.cnblogs.com/hdj1073678089/p/7308380.html