1.Object类

Object类位于java.lang包中,java.lang包包含着Java最基础和核心的类,在编译时会自动导入;

Object类是所有Java类的根基类,如果在类的声明中未使用extends关键字指明其基类,则默认基类为Object类

1.1toString()方法

Object类中定义有public String toString()方法,其返回值是 String 类型,描述当前对象的有关信息。

在进行String与其它类型数据的连接操作时(如:System.out.println(“info”+person)),将自动调用该对象类的 toString()方法.

可以根据需要在用户自定义类型中重写toString()方法

1.2equals()方法

  1. Object类中定义有: public boolean equals(Object obj) 方法提供定义对象是否 “相等” 的逻辑。
  2. Object 的 equals 方法 定义为: x.equals (y) 当 x 和 y是同一个对象的应用时返回 true 否则返回 false
  3. JDK提供的一些类,如String,Date等,重写了Object的equals方法,调用这些类的equals方法x.equals (y) ,当x和y所引用的对象是同一类对象且属性内容相等时(并不一定是相同对象),返回 true 否则返回 false。
  4. 可以根据需要在用户自定义类型中重写equals方法。
  5. 如果没有重写equals()方法,则默认调用Object类的equals方法,比较两个对象是否相等

2.字符串相关类

2.1String类

我们可以通过不同的形式构建String的对象,最常见的是字符串常量的形式,直接使用一对 "" 即可,也可以通过构造器的方式,还可以通过字符数组和字节数组的形式来构建。

String类对象是不可变对象。所以在使用String方法后,要返回新的字符串。

String一经初始化后,就不会在改变其内容了。对String字符串

的操作实际上对其副本(原始拷贝)的操作,原来的字符串一点都没有改变

在java中String类型,可以有两种实例化方式;

一种是这样的String a = "hello";

另一种是这样的String b = new String("hello");

如果直接用“==”比较他俩个是否想相等,System.out.println(a == b);

输出的结果为false;

原因是什么呢?

原因是字符串a实例化后再堆内存上开辟了一地址,他有一个地址编号假设为0x0111;

字符串b使用new实例化后再在对堆上看一个地址,他的地址编号为0x0112;

使用“==”比较,比较的是他们的内存地址,所以他们在内存上的地址是不一样的,故为false;

如要要比较两个字符串的内容是否相等的话使用String类中有一个方法,为equals,来比较;

String与StringBuilder、StringBuffer的区别

String是不可变字符序列,StringBuilder和StringBuffer是可变字符序列。
执行速度StringBuilder > StringBuffer > String。
StringBuilder是非线程安全的,StringBuffer是线程安全的。

3.时间处理类

在标准Java类库中包含一个 Date 类。它的对象表示一个特定的瞬间,精确到毫秒

Date 分配 Date 对象并初始化此对象,以表示自从标准基准时间(称为“历元(epoch)”,即 1970 年 1 月 1 日 00:00:00 GMT)以来的指定毫秒数。

当前时间

//当前毫秒数 

long timeNum =System.currentTimeMillis(); 

System.out.println(timeNum); 

//当前日期 

Date nowDate =new Date(); 

System.out.println(nowDate); 

Date

// 时间点 东八区 1970年1月1日 08:00:0 

Date date =new Date(0L); 

System.out.println(date); 

//指定一个时间 

Date myDate =new Date(189712329239L); 

System.out.println(myDate); 

System.out.println(myDate.getTime()); //获取长整型数字 

//修改时间 

myDate.setTime(0L); 

System.out.println(myDate);

字符串的表示形式与日期互换,使用日期转换器SimpleDateFormat ,代码如下:

public static void main(String[] args) throws ParseException { 

Date myDate2 =new Date(0L); 

//字符串的形式 

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd 

HH:mm:ss"); 

//转字符串 

String dateStr =dateFormat.format(myDate2); 

System.out.println(dateStr); 

//转成日期 

myDate = dateFormat.parse("2020-02-25 11:16:30"); //这里有异常 

System.out.println(myDate.getTime()); 

}

注意parse()存在 的异常 ParseException

jdk8的日期类

LocalTime:代表时间,可以通过时、分、秒来构造

LocalDate:代表日期,可以通过年、月、日构造,并且年/月/日的值都是符合日常

使用习惯的

LocalDateTime:表示日期和时间,可以通过年、月、日、时、分、秒来构造,也

可以通过一个LocalDate和LocalTime来构造。可以调用with、plus、minus方法来修

改LocalDateTime对象,但是修改后原对象的值不变,会返回一个新的对象

格式化: DateTimeFormatter 类,它的使用也非常简单,调用静态方法

ofPattern 生成指定匹配格式的实例;调用实例的 format 方法可以将

TemporalAccessor 对象转换成指定格式的日期时间字符串,实际上这个

TemporalAccessor 是个接口,前面介绍的

LocalTime/LocalDate/LocalDateTime 都间接实现了这个接口;调用实例的

parse 方法可以将时间日期字符串转换为 TemporalAccessor 对象,进而得到对

应的 LocalTime/LocalDate/LocalDateTime 对象

//今年 

System.out.println("今年"+Year.now()); 

System.out.println("指定年" + Year.of(2022)); 

//6月 

System.out.println("月份:"+Month.FEBRUARY); 

//今天不包含时分秒 

LocalDate today = LocalDate.now(); 

System.out.println("今天:"+today); 

//此时此刻 

LocalDateTime now = LocalDateTime.now(); 

System.out.println("现在:"+now); 

//您的生日 

LocalDate yourBirthDate = LocalDate.of(1999, Month.JUNE, 15); 

System.out.println("生日:"+yourBirthDate); 

//您的学习时间 

LocalDateTime dateTime = LocalDateTime.of(2020, 2, 25, 12, 30,40); 

System.out.println("时间:"+dateTime);

jdk8的格式化类

//日期转换器 

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd 

HH:mm:ss"); 

//格式化日期字符串 

LocalDateTime now = LocalDateTime.now(); 

String nowStr = now.format(formatter); //放入格式器 

System.out.println(nowStr); 

String dateStr= "2020-02-25 11:23:04"; 

//转成日期 

LocalDateTime date= LocalDateTime.parse(dateStr, formatter);//放入格式 

器

//获取日 

System.out.println(date.getDayOfMonth());

4.Math类

java.lang.Math 提供了一系列静态方法用于科学计算;其方法的参数和返回值类型一般为 double 型。

System.out.println(Math.abs(-10)); //10 获取-10的绝对值 

System.out.println(Math.pow(3, 2)); //9.0 3的平方 

System.out.println(Math.max(21, 12));//21 大数 

System.out.println(Math.sqrt(9)); //3.0 开方 

System.out.println(Math.ceil(15.2)); //16.0 向上取整 

System.out.println(Math.floor(9.9)); //9.0 向下取整 

System.out.println(Math.ceil(-15.2)); //-15.0 

System.out.println(Math.floor(-9.9)); //-10.0 

System.out.println(Math.round(1.3)); // 1 四舍五入

5.File类

文件夹 与文件 都抽象为File,构建 File 的方式

File(String pathname) ; 可以是绝对路径,也可以是相对路径(盘符)

File(File parent, String child) 

File(String parent, String child) ; 抽象一个指定路径下的 文件

分割符:静态常量

路径分隔符: ; pathSeparator

目录分隔符: \ / separator

常见方法

public boolean canRead() 

public boolean canWrite() 

public boolean exists() 

public boolean isDirectory() 

public boolean isFile() 

public boolean isHidden() 

public long lastModified() 

public long length() 

public String getName() 

public String getPath() 

public String getAbsolutePath() 

public boolean createNewFile() 

public boolean mkdir() 

public boolean mkdirs() 

public boolean delete()



public class FileDemo01 { 

public static void main(String[] args) { 

System.out.println(File.pathSeparator); 

System.out.println(File.separator); 

//绝对路径 与文件建立联系 

File src = new File("E:\\test\\03.gif"); 

src = new File("E:/test/03.gif"); 

src = new 

File("E:"+File.separator+"test"+File.separator+"03.gif");System.out.println(src.exists()); 

//相对路径 

File src2 = new File("E:/test","03.gif"); 

src2 = new File(new File("E:/test"),"03.gif"); 

System.out.println(src.exists()); 

} 

}



public class FileDemo02 { 

public static void main(String[] args) throws IOException { 

File src2 = new File("E:/test","03.gif"); 

boolean flag =src2.isFile(); //是否为文件

flag =src2.isDirectory(); 

System.out.println(flag); //获取文件名称或路径 

String name = src2.getName(); 

System.out.println(name); 

String path = src2.getPath(); 

path = src2.getAbsolutePath(); 

System.out.println(path); 

//文件是否存在

flag = src2.exists(); 

if(!flag){ //不存在创建 

src2.createNewFile(); 

} //创建目录 

src2 = new File("E:/test/test2/test3"); 

src2.mkdir(); //确保父路径存在,才能创建 

src2.mkdirs();// 如果父路径不存在,则创建 

//列出当前文件夹的子目录 

src2 = new File("E:/xp/20130401"); 

if(src2.isDirectory()){ //目录才有子目录 

File[] subFiles = src2.listFiles(new FilenameFilter(){ 

@Overridepublic boolean accept(File dir, String name) { 

/* 

System.out.println(name); 

if(name.contains("xls")){ 

return true; 

}*/ 

return !new File(dir,name).isDirectory(); 

} 

});

for(File temp:subFiles){ 

System.out.println(temp); 

} 

}

//delete() 

src2 = new File("E:/test","03.gif"); 

boolean flag2 =src2.delete(); 

System.out.println(flag2); 

} 

}



/** 

*递归打印文件夹 

*/

public static void printName(File src){ 

if(null ==src){ 

return; 

}

System.out.println(src.getPath()); 

if(src.isDirectory()){

for(File sub:src.listFiles()){ 

printName(sub); 

} 

} 

}

6.包装类(自动装箱和拆箱)

Java语言是一个面向对象的语言,但是Java中的基本数据类型却是不面向对象的。但是我们在实际使用中经常需要将基本数据转化成对象,便于操作。为了解决这个不足,在设计类时为每个基本数据类型设计了一个对应的类进行代表,这样八个和基本数据类型对应的类统称为 包装类(Wrapper Class) 。

对于包装类说,这些类的用途主要包含两种:

a、作为和基本数据类型对应的类类型存在,方便涉及到对象的操作。

b、包含每种基本数据类型的相关属性如最大值、最小值等,以及相关的操作方法

(这些操作方法的作用是在基本类型数据、包装类对象、字符串之间提供转化!)。

自动装箱拆箱时,对于-128-127之间的值,编译器仍然会把它当做基本类型处理。

7.Collections工具类

void sort(List) //对List容器内的元素排序,排序的规则是按照升序进行排序。

void shuffle(List) //对List容器内的元素进行随机排列

void reverse(List) //对List容器内的元素进行逆续排列

void fill(List, Object) //用一个特定的对象重写整个List容器

int binarySearch(List, Object)//对于顺序的List容器,采用折半查找的方法查找特定对象

Comparable接口

所有可以“排序”的类都实现了 java.lang.Comparable 接口, Comparable 接口

中只有一个方法 public int compareTo(Object obj)

返回 0 表示 this == obj ;

返回正数表示 this > obj ;

返回负数表示 this < obj 。

public class TestComparable { 

public static void main(String[] args) { 

List<Student> list = new ArrayList<Student>(); 

Student stu1 = new Student(1,"张三",100); 

Student stu2 = new Student(2,"张四",80); 

Student stu3 = new Student(3,"张五",90); 

list.add(stu1); 

list.add(stu2); 

list.add(stu3); 

System.out.println(list); 

Collections.sort(list); 

System.out.println(list); 

} 

}

class Student implements Comparable<Student> { 

int id; 

String name; 

int score; 

public Student(int id, String name, int score) { 

super(); 

this.id = id; 

this.name = name; 

this.score = score; 

}

public String toString(){ 

return name+score; 

}

@Override 

public int compareTo(Student o) { 

if(this.score>o.score){ 

return 1; 

}else if(this.score<o.score){

return -1; 

}else { 

return 0; 

} 

} 

}

Comparator 接口

Comparator比较器,可以根据需要定制特定的比较规则

List<Student> stus = new ArrayList<Student>(){ 

{ 

add(new Student("张三", 30)); 

add(new Student("李四", 20)); 

add(new Student("王五", 60)); 

} 

};

//对users按年龄进行排序 

Collections.sort(stus, new Comparator<Student>() { 

@Override 

public int compare(Student s1, Student s2) { 

// 升序 

//return s1.getAge()-s2.getAge(); 

return s1.getAge().compareTo(s2.getAge()); 

// 降序 

// return s2.getAge()-s1.getAge(); 

// return s2.getAge().compareTo(s1.getAge()); 

} 

});