hashCode方法将内存地址转换为一个整数
package com.xie.changyonglei.demo01; public class Application { public static void main(String[] args) { User user1 = new User(); User user2 = new User(); //hashCode方法将内存地址转换为一个整数 System.out.println(user1.hashCode()); System.out.println(user1.hashCode()); System.out.println("=============="); System.out.println(user2.hashCode()); System.out.println(user2.hashCode()); //== 比较基本类型时,比较数值本身是否相等 //== 比较引用类型时,比较变量中存放的地址 System.out.println(user1 == user2); User user3 = user1; System.out.println(user1 == user3); } }
返回对应的运行时类
package com.xie.changyonglei.demo02; public class Application { public static void main(String[] args) { User user = new User(); Object object = new User(); Class c1 = user.getClass(); Class c2 = object.getClass(); System.out.println(c1); System.out.println(c2);//class com.xie.changyonglei.demo02.User } } class User{}
默认的对象.equals(对象)和==是一样的效果,比较内存地址
String类型使用equals会比较每个字符是否相等,因为重写了Object的equals方法
如果想要比较对象的内容是否相等,需要重写equals方法
快捷键:Alt+Insert,选择equals() and hasCode()
package com.xie.changyonglei.demo03; import java.util.Objects; public class Application { public static void main(String[] args) { User user1 = new User("张三",11); User user2 = new User("李四",12); User user3 = new User("张三",11); System.out.println(user1.equals(user2)); System.out.println(user1.equals(user3)); } } class User{ private String name; private int age; public User(String name,int age){ this.name = name; this.age = age; } //如果想要比较对象的内容是否相等,需要重写equals方法 @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; return age == user.age && Objects.equals(name, user.name); } }
package com.xie.changyonglei.demo04; public class Applicatio { public static void main(String[] args) throws CloneNotSupportedException { Student student = new Student("aaa",1); Student student1 = (Student) student.clone(); System.out.println(student == student1); System.out.println(student.equals(student1)); } } class Student implements Cloneable{ private String name; private int age; public Student(String name,int age) { this.name = name; this.age = age; } @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } }
垃圾回收机器(Garbage Collection),也叫GC,垃圾回收器主要有一下特点:
String[] split = s.split(",");
String的内容是不可变的-----StringBuilder内容是可变的
StringBuilder是一个可变的字符串类,我们可以把它看成是一个容器,作用:提高字符串的操作效率;而且不会开辟很多空间,浪费资源。
构造方法:
sb.reverse(); //反转前是:蓝色红色绿色 //反转后是:色绿色红色蓝
public int length():返回长度(字符出现的个数)
StringBuilder和String相互转化
String s = "aaa"; StringBuilder sb = new StringBuilder(s); String Str = sb.toString();
StringBuilder sb = new StringBuilder(); sb.append("红色"); System.out.println(sb);//红色 sb.append("蓝色"); System.out.println(sb);//红色蓝色 sb.append("绿色"); System.out.println(sb);//红色蓝色绿色
public StringBuffer deleteCharAt(int index): //删除指定位置的字符,并返回字符串缓冲区本身 public StringBuffer delete(int start,int end): //删除从指定位置开始指定位置结束的内容,并返回字符串缓冲区本身
public StringBuffer replace(int start,int end,String str): //从start开始到end用str替换 也是返回字符串缓冲区本身
public String substring(int start): //从指定位置截取到末尾 public String substring(int start,int end): //截取从指定位置开始到结束位置,包括开始位置,不包括结束位置
StringBuilder和StringBuffer基本相似,两个类的构造器和方法也基本相同。不同的是:StringBuffer是线程安全的,而StringBuilder则没有实现线程安全功能,所以性能略高。
基本数据类型 | 对应的包装类 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
自动装箱:将基本数据类型转换成对应的包装类类型
自动拆箱:将包装类转换成对应的基本数据类型
以Integer为例:
package com.xie.changyonglei.demo05; public class Application { public static void main(String[] args) { //最大最小值 System.out.println(Integer.MAX_VALUE); System.out.println(Integer.MIN_VALUE); //实例 Integer i1 = new Integer(100); Integer i2 = new Integer("99");//只能传数字字符串,否则报错 System.out.println(i1+"--"+i2);//默认 .intValue() 方法转换为int //int 转换为 String int i = 100; String str1 = i + ""; String str2 = String.valueOf(i); String str3 = Integer.toString(i); System.out.println(str1 + "--" + str2 + "--" + str3); //String 转换为 int String str = "66"; int int1 = Integer.parseInt(str); int int2 = new Integer(str).intValue(); int int3 = Integer.valueOf(str); System.out.println(int1 + "--" + int2 + "--" + int3); //10进展转换为其他进制 int i3 = 100; System.out.println(Integer.toBinaryString(i3));//二进展 System.out.println(Integer.toOctalString(i3));//八进展 System.out.println(Integer.toHexString(i3));//十六进展 //其他进制转换为10进展 System.out.println(Integer.parseInt("100",8));//64 System.out.println(Integer.parseInt("AA",16));//170 //自动装箱拆箱 Integer i4 = 100; //=Integer.valueOf(100); 自动装箱 System.out.println(i4);//=i4.intValue() 自动拆箱 i4 += 200;//=Integer.valueOf(i4.intValue()+200) System.out.println(i4.getClass() + "--" + i4); } }
基本数学运算的方法
math类常用方法 1.public static int abs(int a) 返回绝对值 2.public static double ceil(double a) 返回大于等于参数的最小double值,等于一个整数,向上取整 3.public static double floor(double a) 返回小于等于参数的最小double值,等于一个整数,向下取整 4.public static int round(float a) 按照四舍五入返回最接近参数的int 5.public static max(int a, int b) 返回最大值 6.public static min(int a, int b) 返回最小值 7.public static double pow(double a, double b) 返回a的b次幂 8.public static double random() 返回值为double的正值 [0.0, 1.0)随机数
package com.xie.changyonglei.demo06; import java.util.Random; public class Rantest { public static void main(String[] args) { Random r1 = new Random(13);//无参每次随机数不同,有参每次随机数相同 System.out.println(r1.nextDouble());//和Math.random()一样0.0~1.0 System.out.println(r1.nextInt()); //获取int的最大最小之间的数 System.out.println(r1.nextLong());//获取long的最大最小之间的数 System.out.println(r1.nextInt(15 ));//0到15之间的随机数 } }
package com.xie.changyonglei.demo06; import java.util.Date; public class SysDemo { public static void main(String[] args) { long time = System.currentTimeMillis(); System.out.println(time); Date date = new Date(time); System.out.println(date); System.gc(); //显式调用gc System.exit(1); //终止JVM的运行 System.out.println("------"); } }
超出int范围的整数运算时使用
package com.xie.changyonglei.demo06; import java.math.BigInteger; public class BigIntgeterDemo { public static void main(String[] args) { System.out.println(Integer.MAX_VALUE); System.out.println(Integer.MAX_VALUE+1); BigInteger bigInteger = new BigInteger("2147483647"); System.out.println(bigInteger); BigInteger bigInteger1 = bigInteger.add(new BigInteger("1")); System.out.println(bigInteger1); System.out.println(bigInteger1.longValue()); System.out.println(bigInteger1.intValue()); } }
float和double计算容易出现精度丢失情况,设计BigDecimal,不可变、任意精度、有符合十进制数
package com.xie.changyonglei.demo06; import java.math.BigDecimal; public class BigDecimalDemo { public static void main(String[] args) { double d1 = 0.01; double d2 = 0.09; System.out.println(d1 + d2);//0.09999999999999999 BigDecimal bd1 = new BigDecimal(0.01); BigDecimal bd2 = new BigDecimal(0.09); System.out.println(bd1.add(bd2));//0.09999999999999999687749... BigDecimal bd3 = new BigDecimal("0.01"); BigDecimal bd4 = new BigDecimal("0.09"); System.out.println(bd3.add(bd4));//加 System.out.println(bd3.subtract(bd4));//减 System.out.println(bd3.multiply(bd4));//乘 System.out.println(bd4.divide(bd3));//除 } }
表示特定的瞬间,精确到毫秒
package com.xie.changyonglei.demo06; import java.util.Date; public class DateDemo { public static void main(String[] args) { Date date = new Date(); System.out.println(date); Date date2 = new Date(12345678999L); System.out.println(date2); } }
格式化、规范化。日期--文本,文本--日期
package com.xie.changyonglei.demo06; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class SimpleDateFormatDemo { public static void main(String[] args) throws ParseException { Date date = new Date(); System.out.println(date); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sdf.format(date)); String str = "2022-01-01 12:12:12"; SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sdf1.parse(str)); } }
快速获取年月日时分秒等
package com.xie.changyonglei.demo06; import java.util.Calendar; public class CalendarDemo { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); System.out.println(calendar); System.out.println(calendar.get(Calendar.YEAR)); System.out.println(calendar.get(Calendar.MONTH)); System.out.println(calendar.get(Calendar.DAY_OF_MONTH)); System.out.println(calendar.get(Calendar.DAY_OF_WEEK)); System.out.println(calendar.get(Calendar.DAY_OF_YEAR)); System.out.println(calendar.get(Calendar.HOUR)); System.out.println(calendar.get(Calendar.HOUR_OF_DAY)); System.out.println(calendar.get(Calendar.MINUTE)); System.out.println(calendar.get(Calendar.SECOND)); } }
返回该对象的字符串表示:全类路径名称+@+hashCode的十六进制
package com.xie.changyonglei.demo07; public class test01 { public static void main(String[] args) { User user = new User("xiexie",18); System.out.println(user.toString());//com.xie.changyonglei.demo07.User@1b6d3586 System.out.println(user);//com.xie.changyonglei.demo07.User@1b6d3586 } } class User{ private String name; private int 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; } public User(String name, int age) { this.name = name; this.age = age; } }
默认toString方法输出内容意义不大,可以重写
package com.xie.changyonglei.demo07; public class test01 { public static void main(String[] args) { User user = new User("xiexie",18); System.out.println(user.toString());//User{name='xiexie', age=18} System.out.println(user);//User{name='xiexie', age=18} } } class User{ private String name; private int 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; } public User(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } }