1.常用包
2.Object类
类 Object 是类层次结构的根类。每个类都使用 Object 作为超类。所有对象(包括数组)都实现这个类的方法。
(1) == :基本数据类型比较的是数值的大小 引用数据类型比较的是引用的地址
equals 默认比较两个对象的地址 若比较两个对象的内容时需要重写这个方法
package ObjectDemo;
import java.util.Objects;
public class Student {
private String name;
private int id;
public Student() {
}
public Student(String name, int id) {
this.name = name;
this.id = id;
}
/*@Override
public boolean equals(Object o) {
//表示两个引用指向同一个地址
if (this == o) {
return true;
}
if (o == null) {
return false;
}
if (o instanceof Student) {
Student student = (Student) o;
if (this.getName().equals(student.getName()) && this.getId() == student.getId()) {
return true;
}
}
return false;
}
@Override
public boolean equals(Object o) {
Student student = (Student) o;
return this.getId() == student.getId();
}*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return this.id == student.id &&
Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, id);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", id=" + id +
'}';
}
}
package ObjectDemo;
public class StudentTest {
public static void main(String[] args) {
//new 出来的对象引用地址肯定不一样
Student student1 = new Student("gao", 1001);
//Object中的toString方法默认打印全类名@十六进制 未重写toString方法时
/*System.out.println(student1);//ObjectDemo.Student@305375
System.out.println(student1.toString());//ObjectDemo.Student@305375*/
//重写toString后
System.out.println(student1);
System.out.println(student1.toString());
Student student2 = new Student("gao", 1001);
Student student3 = new Student("xie", 1001);
System.out.println(student1 == student3); //false
System.out.println(student1.equals(student3));//false
System.out.println(student1.equals(student2)); //true
}
}
3.包装类
Integer类
package ObjectDemo;
public class IntegerDemo01 {
public static void main(String[] args) {
//根据参数指定的整数构造对象
Integer it1 = new Integer(10);
System.out.println("it1 = " + it1);
//将Integer类型对象,转变为int的数值,叫做手动拆箱
int i = it1.intValue();
System.out.println(i);
System.out.println("===============");
//将int类型的数值,转变为Integer类型的对象。叫做手动装箱
Integer integer = Integer.valueOf(i);
System.out.println(integer);
//根据参数指定的字符串构造对象
Integer it2 = new Integer("20");
System.out.println("it2 = " + it2);
System.out.println("===============");
//JDK1.5开始提供了自动拆箱和装箱的机制
Integer i1 = 12;
int i2 = i1;
System.out.println(i1);
System.out.println(i2);
System.out.println("=================");
//将字符串类型转为int类型并返回
String str = "123";
int i3 = Integer.parseInt(str);
System.out.println(i3);
}
}
4.数学处理类
Bigdecimal
package BigdecimalDemo;
import java.math.BigDecimal;
public class BigdecimalTest01 {
public static void main(String[] args) {
double d1 = 0.1 + 0.2;
System.out.println(d1);
System.out.println("===============");
BigDecimal bd1 = new BigDecimal("0.1");
BigDecimal bd2 = new BigDecimal("0.23");
System.out.println(bd1.add(bd2));
System.out.println(bd1.subtract(bd2));
System.out.println(bd1.multiply(bd2));
//System.out.println(bd1.divide(bd2));//除不尽报错
System.out.println(bd1.divide(bd2, BigDecimal.ROUND_HALF_UP));
}
}
5.String类
(1)常量池
package StringDemo;
public class StringDemo01 {
public static void main(String[] args) {
String str1 = "abc";
String str2 = "123";
System.out.println(str1 == str2);//比较地址 false
String str3 = new String("abc");
String str4 = new String("abc");
System.out.println(str1 == str3);//false
System.out.println(str3 == str4);//false
String str5 = "abc";
System.out.println(str1 == str5);//true
}
}
(2)
package StringDemo;
public class StringDemo02 {
public static void main(String[] args) {
String str = new String(" let me give you some color to see see ! ");
System.out.println(str.charAt(8)); //e
System.out.println("字符串的长度为" + str.length());//44
System.out.println(str.contains("me"));//true
System.out.println(str.toUpperCase());// LET ME GIVE YOU SOME COLOR TO SEE SEE !
System.out.println(str.toLowerCase());// let me give you some color to see see !
System.out.println(str.trim());//let me give you some color to see see !
System.out.println(str.startsWith("Let"));//false 应该为空白格
System.out.println(str.startsWith(" "));//true
System.out.println(str.endsWith("!"));//false
byte[] brr ={97,98,99,100,101};
String str3 = new String(brr,0,2);
System.out.println(str3);//ab 97对应的字符a 98对应的字符b
}
}
5.StringBuilder类和StringBuffer类
package ObjectDemo;
public class StringBuilderDemo01 {
public static void main(String[] args) {
StringBuilder sb1 = new StringBuilder();
System.out.println("初始容量为:" + sb1.capacity());
System.out.println("长度为:" + sb1.length());
System.out.println("-----");
StringBuilder sb2 = new StringBuilder("heihei");
System.out.println("初始容量为:" + sb2.capacity());
System.out.println("长度为:" + sb2.length());
System.out.println("==============");
System.out.println(sb2.insert(0, "gao"));
System.out.println(sb2.append("666"));
System.out.println(sb2.delete(0, 3));
sb2.replace(0, 5, "haha");
System.out.println(sb2.reverse());
System.out.println(sb2.indexOf("h"));
}
}
6.日期相关的类
Date类
package dates;
import java.util.Date;
import java.util.logging.SimpleFormatter;
public class DateTest01 {
public static void main(String[] args) {
Date date = new Date();
System.out.println(date);
Date date1 = new Date(10000);
System.out.println(date1);
date1.setTime(1000);
System.out.println(date1.getTime());
}
}
SimpleFormatter类
package dates;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTest01 {
public static void main(String[] args) {
Date date = new Date();
System.out.println(date);
Date date1 = new Date(10000);
System.out.println(date1);
date1.setTime(1000);
System.out.println(date1.getTime());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format =simpleDateFormat.format(date);
System.out.println(format);
}
}
7.可变形参
可变参数只能出现在参数列表的最后
package dates;
public class Demo01 {
public static void main(String[] args) {
System.out.println(add(1, 2));
System.out.println(add(1, 2, 3));
System.out.println(add(1, 2, 3, 4));
System.out.println(add(1, 2, 3, 4, 5));
}
public static int add(int... args) {
int sum = 0;
for (int i = 0; i < args.length; i++) {
sum += args[i];
}
return sum;
}
}