1、Object
1)Object类概述
Object:Class Object是类Object结构的根。
每个class都有Object作为超类。
所有对象(包括数组)都实现了这个类的方法。
2)Object构造方法:
public Object()
3)Object类的成员方法:
public String toString() 返回对象的字符串表示形式
一般来,toSting()返回的是一个对象的字符串。
//toString()方法等价于以下内容:
//getClass().getName() + '@' + Integer.toHexString(hashCode())
//this.getClass().getName()+"@"+Integer.toHexString(this.hashCode)
// com.shujia.java.day14.Student2@4554617c
@Override public String toString(){ return name+age; }public class Test { public static void main(String[] args) { Student22 s1 = new Student22(); System.out.println(s1.toString());//null0 s1.setName("小花"); s1.setAge(18); System.out.println(s1.toString());//小花18 System.out.println(s1.getClass().getName()+"@"+Integer.toHexString(s1.hashCode())); // com.shujia.java.day14.Student22@4554617c System.out.println(s1);//小花18 } }
public int hashCode() 返回对象的哈希码值。
public final Class getClass() 表示 类对象的运行时类的Class对象。
Class类中的方法:
public String getName() 返回由类对象表示的实体的名称(类,接口,数组类,原始类 型或void),作为String 。
public class Student extends Object {
}
public class StudentTest1 {
public static void main(String[] args) {
Student s1 = new Student();
System.out.println(s1.hashCode()); // 1163157884
Student s2 = new Student();
System.out.println(s2.hashCode()); // 1956725890
Student s3 = s1;
System.out.println(s3.hashCode()); // 1163157884
Student s4 = new Student();
System.out.println(s4.getClass()); //class com.shujia.java.day14.Student
Class c = s4.getClass();
System.out.println(aClass.getName()); //com.shujia.java.day14.Student
System.out.println("用链式编程改进:");
System.out.println(s4.getClass().getName());//com.shujia.java.day14.Student
}
}
public boolean equals(Object obj) 指示一些其他对象是否等于此。
这个方法默认比较的是地址值。
==: 基本数据类型:比较的是值是否相同 引用数据类型:比较的是地址值是否相同 equals: 因为equals是属于Object类中的方法,只有对象可以去调用,而基本数据类型不属于对象 引用类型:默认情况下比较的是地址值,需要根据情况去重写,而重写的时候不需要我们自己去重写 可以自动生成,但是里面的实现逻辑需要自己去写@Override public boolean equals(Object obj) { //Object obj = new Student3(); // return true; //s1 -- this //s4 -- obj //想要访问父类中成员,就要向下转型 if (this == obj){ return true; } Student3 s = (Student3)obj; return this.name.equals(s.getName()) && this.age == s.getAge(); }
public class StudentTest3 { public static void main(String[] args) { Student3 s1 = new Student3("朱丹", 18); Student3 s2 = new Student3("朱丹", 18); System.out.println(s1 == s2);//false Student3 s3 = s1; System.out.println(s1 == s3); // true System.out.println("***********************"); System.out.println(s1.equals(s2)); //ture System.out.println(s1.equals(s1)); //true System.out.println(s1.equals(s3)); //true Student3 s4 = new Student3("陈俊荣", 18); System.out.println(s1.equals(s4)); } }
protected void finalize()
throws Throwable
当垃圾收集确定不再有对该对象的引用时,垃圾收集器在对象上调用该对象。
简单来说,finalize就是用于垃圾回收的。
什么时候回收不确定。(面试题)
protected Object clone()
throws CloneNotSupportedException创建并返回此对象的副本。
返回的是这个实例的一个克隆,被Object类接收
clone会创建一个新的对象出来。
Interface Cloneable使用clone方法必须实现这个杰奎
但是我们通过观察API发现,该接口没有任何抽象方法,那么实现这个接口有什么意义呢?
如果没有重写该接口就直接调用,会报错CloneNotSupportedException
像这样没有任何方法的接口我们称之为标记接口
public class Student4 implements Cloneable{ private String name; private int age; public Student4() { } public Student4(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; } @Override public String toString() { return "Student4{" + "name='" + name + ''' + ", age=" + age + '}'; } @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } }
public class StudentTest4 { public static void main(String[] args) throws CloneNotSupportedException { Student4 s1 = new Student4(); s1.setName("小花"); s1.setAge(18); System.out.println(s1.toString());//Student4{name='小花', age=18} Object s1Clone = s1.clone(); //向下转型 Student4 sClone = (Student4) s1Clone; System.out.println(s1.getName()+"---"+s1.getAge());//小花---18 System.out.println(sClone.getName()+"---"+sClone.getAge());//小花---18 Student4 s2 = s1; //堆内存中始终只有一个对象 System.out.println(s2.getName()+"---"+s2.getAge());//小花---18 s2.setName("小白"); s2.setAge(20); System.out.println(s1.getName()+"---"+s1.getAge());//小白---20 System.out.println(sClone.getName()+"---"+sClone.getAge());//小花---18 } }
2、Scanner
1)Scanner类概述
JDK5以后用于获取用户的键盘输入
2)构造方法
public Scanner(InputStream source)
3)Scanner类中的成员方法
public int nextInt()
public String nextLine()
import java.util.Scanner;
public class ScannerTest {
public static void main(String[] args) {
//创建键盘录入对象
Scanner sc = new Scanner(System.in);
//判断输入的数据是否符合接收的数据类型
// if(sc.hasNextInt()){
// int i = sc.nextInt();
// System.out.println(i);
// }else {
// System.out.println("输入的数据不是int类型");
// }
// int i = sc.nextInt();
// int j = sc.nextInt();
// System.out.println("i: "+i+",j: "+j);
//
// String s = sc.nextLine();
// String s2 = sc.nextLine();
// System.out.println("s: "+s+",s2: "+s2);
//先输入字符串再输入int类型
// String s = sc.nextLine();
// int i = sc.nextInt();
// System.out.println("s: "+s+",i: "+i);
//先输入int类型再输入字符串类型
//这里会出现问题:输入完int类型的数据之后,就自动输出了,还没来得及出入字符串
//为什么呢?
//nextLine()将我们的换行符给接收到了
//怎么解决呢?
//方式1:创建另一个新的Scanner对象
//方式2:输入一个东西就去判断一下是否符合类型 (开发中使用方式2去解决这个问题)
int i = sc.nextInt();
//创建一个新的Scanner对象
Scanner sc2 = new Scanner(System.in);
String s = sc2.nextLine();
System.out.println("s: "+s+",i: "+i);
}
}
3、String
1)String类概述
字符串是由多个字符组成的一串数据(字符序列) 字符串可以看成是字符数组
字符串是常量,一旦被赋值,就不能被改变
2)构造方法
public String()
public String(byte[] bytes)
public String(byte[] bytes,int offset,int length)
public String(char[] value)
public String(char[] value,int offset,int count)
public String(String original)
3)字符串的方法
public int length()返回此字符串的长度。
public class StringDemo1 {
public static void main(String[] args) {
String s = new String();
System.out.println("s: " + s);//s:
System.out.println("字符串s的长度:" + s.length());//字符串s的长度:0
System.out.println("*********************************");
//public String(byte[] bytes) 将一个字节数组转成一个字符串
byte[] b = {97, 98, 99, 100, 101};
String s1 = new String(b);
System.out.println("s1: " + s1);//s1: abcde
System.out.println("字符串s1的长度为:" + s1.length());//字符串s1的长度为:5
System.out.println("*********************************");
//public String(byte[] bytes,int index,int length)
//将字节数组中的一部分截取出来变成一个字符串
String s2 = new String(b, 1, 3);
System.out.println("s2: " + s2);//s2: bcd
System.out.println("字符串s2的长度为:" + s2.length());//字符串s2的长度为:3
System.out.println("*********************************");
//public String(char[] value)
//将一个字符数组转化成一个字符串
char[] c = {'a', 'b', 'c', 'd', '我', '爱', '冯', '提', '莫'};
String s3 = new String(c);
System.out.println("s3: " + s3);//s3: abcd我爱冯提莫
System.out.println("字符串s3的长度为:" + s3.length());//字符串s3的长度为:9
System.out.println("*********************************");
//public String(char[] value,int index,int count)
//将字符数组中的一部分截取出来变成一个字符串
String s4 = new String(c, 4, 5);
System.out.println("s4: " + s4);//s4: 我爱冯提莫
System.out.println("字符串s4长度:" + s4.length());//字符串s4长度:5
System.out.println("*********************************");
//public String(String original)
String s5 = "qwerdf";
String s6 = new String(s5);
System.out.println("s6: " + s6);//s6: qwerdf
System.out.println("字符串s6的长度为:" + s6.length());//字符串s6的长度为:6
}
}
4)string类面试题
String s = new String(“hello”)和String s = “hello”;的区别?
1、==比较引用数据类型比较的是地址值
2、equals默认比较的是地址值,但是由于String类中重写了该方法,所以比较的 是内容
3、String s = new String(“hello”) 会在堆内存中创建对象
5)字符串的创建
1)字符串如果是变量相加,是先开辟空间,然后再拼接
2)字符串如果是常量相加,是先加,然后再去常量池里找,如果找到了就返回,如果 找不到就创建
6)String类的判断功能
boolean equals(Object obj)
boolean equalsIgnoreCase(String str)
boolean contains(String str)
boolean startsWith(String str)
boolean endsWith(String str) boolean isEmpty()
public class StringDemo6 {
public static void main(String[] args) {
String s1 = "helloworld";
String s2 = "Helloworld";
String s3 = "HelloWorld";
//boolean equals(Object obj) 比较字符串中的内容是否相同 区分大小写
System.out.println(s1.equals(s2)); //false
System.out.println(s1.equals(s3)); //false
System.out.println("***********************");
//boolean equalsIgnoreCase(String str)
//比较字符串中的内容是否相同 忽略大小写
System.out.println(s1.equalsIgnoreCase(s2)); //true
System.out.println(s1.equalsIgnoreCase(s3)); //true
System.out.println("************************");
//boolean contains(String str)
//判断大的字符串中是否包含小的字符串,如果包含,返回的是true,否则就是false
//区分大小写
System.out.println(s1.contains("Hello")); //false
System.out.println(s1.contains("leo")); //false
System.out.println(s1.contains("hello")); //true
System.out.println("************************");
//boolean startsWith(String str)测试此字符串是否以指定的前缀开头。
//区分大小写
System.out.println(s1.startsWith("hel")); //
System.out.println(s1.startsWith("h")); //
System.out.println(s1.startsWith("he")); //
System.out.println(s1.startsWith("he34")); //false
System.out.println(s1.startsWith("H")); //false
System.out.println("************************");
//boolean endsWith(String str)
//区分大小写
System.out.println(s1.endsWith("orld")); //true
System.out.println(s1.endsWith("orlD")); //false
System.out.println("************************");
//boolean isEmpty()
System.out.println(s1.isEmpty()); //false
System.out.println("************************");
String s4 = "";
String s5 = null;
System.out.println(s4 == s5); //false
System.out.println(s4.isEmpty()); //true
//由于s5的对象都没有,所以不能够调用方法,报错,空指针异常
// System.out.println(s5.isEmpty()); //NullPointerException
String s6 = "bigdata";
String s7 = null;
//需求:在不知道s6和s7的内容前提下,想要与字符串"hadoop"进行内容比较
// System.out.println(s6.equals("hadoop"));
// System.out.println(s7.equals("hadoop"));
//在进行字符串内容比较的时候,为了防止出现空指针异常,将变量放后面
//推荐写法
System.out.println("hadoop".equals(s6));
System.out.println("hadoop".equals(s7));
}
}
7)String类的获取功能:
int length()
char charAt(int index)
int indexOf(int ch)
int indexOf(String str)
int indexOf(int ch,int fromIndex)
int indexOf(String str,int fromIndex)
String substring(int start)
String substring(int start,int end)
public class StringDemo7 {
public static void main(String[] args) {
String s = "helloworld";
//int length() 获取字符串的长度
System.out.println(s.length());
System.out.println("***************************");
//public char charAt(int index)返回char指定索引处的值
//从0开始到length()-1
System.out.println(s.charAt(7));
System.out.println(s.charAt(0));
// System.out.println(s.charAt(11));//StringIndexOutOfBoundsException
System.out.println("***************************");
//public int indexOf(int ch)返回指定字符第一次出现的字符串内的索引。
//从0开始到length()-1
//传入的是ASCLL码值
System.out.println(s.indexOf(108)); //2
System.out.println(s.indexOf('l')); //2
System.out.println("***************************");
//public int indexOf(String str)
// 返回指定子字符串第一次出现的字符串内的索引。
//字符串第一个字符索引
System.out.println(s.indexOf("owo")); //4
//当子字符串不存在的时候,返回的是-1
System.out.println(s.indexOf("poi")); //-1
System.out.println("***************************");
// public int indexOf(int ch,int fromIndex)
// 返回指定字符第一次出现的字符串内的索引,以指定的索引开始搜索。
System.out.println(s.indexOf('l',4)); //8
System.out.println(s.indexOf('p',4)); //-1
System.out.println(s.indexOf('l',40)); //-1
System.out.println(s.indexOf('p',40)); //-1
System.out.println("***************************");
//int indexOf(String str,int fromIndex)
//返回指定字符串第一次出现的字符串内的索引,以指定的索引开始搜索。
System.out.println("***************************");
//public String substring(int beginIndex)
// 返回一个字符串,该字符串是此字符串的子字符串。
// 子字符串以指定索引处的字符开头,并扩展到该字符串的末尾。
System.out.println(s.substring(3)); //loworld
// System.out.println(s.substring(20)); //StringIndexOutOfBoundsException
System.out.println("***************************");
//public String substring(int beginIndex,int endIndex)
// 返回一个字符串,该字符串是此字符串的子字符串。
// 子串开始于指定beginIndex并延伸到字符索引endIndex - 1 。
//左闭右开 [ , )
System.out.println(s.substring(4,7)); //owo
System.out.println(s.substring(1,20)); //StringIndexOutOfBoundsException
}
}
8)String类的转换功能:
byte[] getBytes()
char[] toCharArray()
static String valueOf(char[] chs)
static String valueOf(int i)
String toLowerCase()
String toUpperCase()
String concat(String str)
public class StringDemo8 {
public static void main(String[] args) {
String s = "HelloWorLD";
//public byte[] getBytes()
// 使用平台的默认字符集将此String编码为字节序列,将结果存储到新的字节数组中。
byte[] b1 = s.getBytes();
// System.out.println(b1); //[B@4554617c 数组没有重写toString()方法 打印的是地址值
for(int i=0;i 字符数组
char[] chars = s.toCharArray();
for(int i = 0;i 字符串
//static String valueOf(char[] chs)
System.out.println(chars);
System.out.println("*********************");
//static String valueOf(int i)
//将int类型的数据转化成字符串类型
String s1 = String.valueOf(100); // "100" --> 100
System.out.println(s1);
System.out.println("*********************");
//String toLowerCase()
//将字符串内容全部转化成小写
String s2 = s.toLowerCase(); //HelloWorLD
System.out.println(s2); //helloworld
System.out.println("*********************");
// String toUpperCase()
//将字符串内容全部转大写
String s3 = s.toUpperCase();//HelloWorLD
System.out.println(s3);//HELLOWORLD
System.out.println("*********************");
//public String concat(String str)将指定的字符串连接到该字符串的末尾。
//将小括号里面的字符串拼接到调用方法的字符串后面
String s4 = "Hello";
String s5 = "World";
String s6 = s4+s5;
String s7 = s5.concat(s6);
System.out.println("s4: "+s4); //Hello
System.out.println("s5: "+s5); //World
System.out.println("s6: "+s6); //HelloWorld
System.out.println("s7: "+s7); //WorldHelloWorld
// String s8 = "";
String s8 = null;
// System.out.println(s8.concat(s5));//NullPointerException
// System.out.println(s5.concat(s8));//NullPointerException
}
}



