常用的类
Random随机数
使用Random函数。
随机数生成
Random r = new Random(种子数);
//种子数可以不写 System.out.println(r.nextInt());
//产生一个[0,1)的随机数 System.out.println(r.nextInt(10));
//产生一个[0,10)的随机数 System.out.println(r.nextBoolean());
//产生一个boolean类型的随机数
种子数
含有相同种子数生成的随机数相同
Random r1 = new Random(1);
Random r2 = new Random(1);
System.out.println(r2.nextInt() == r1.nextInt());//true
Math
System.out.println(Math.PI);//3.141592653589793
System.out.println(Math.E);//2.718281828459045
System.out.println(Math.abs(5));
System.out.println(Math.abs(-5));//绝对值
System.out.println(Math.cbrt(27));//立方根
System.out.println(Math.max(8.7,5.5));//最大值
System.out.println(Math.min(8.7,5.5));//最小值
System.out.println(Math.random());//[0,1)
System.out.println(Math.pow(3,4));//3的4次幂:81
System.out.println(Math.ceil(3.1));//4.0 ceil:天花板,向上取整
System.out.println(Math.floor(3.9));//3.0 floor:地板,向下取整
System.out.println(Math.round(3.5));//4 round:四舍五入 System.out.println(Math.round(-3.5));//-3 -3.5 + 0.5 向下取整
比较器
用于比较引用数据类型在集合中的排序
内部比较器
Comparable,只有一种比较规则,使用的类要继承Comparable接口,并且可以对compareTo进行重写。以Person类为例:
Person类:
需要继承implements Comparable
// 定义2个Person对象比较大小逻辑:年龄 0 -1 1
@Override
public int compareTo(Person o) {
if (o == null) {
return 0;
}
// 按姓名来比较,由小到大
return name.compareTo(o.name);
//按照年龄由小到大排序
//return this.age - o.age;
}
测试类:
Person p1 = new Person("zhangsan",19);
Person p2 = new Person("lisi",10);
Person p3 = new Person("wangwu",29);
Person p4 = new Person("zhaoliu",21);
Person [] personArr = {p1,p2,p3,p4};
Arrays.sort(personArr);
System.out.println(Arrays.toString(personArr));
外部比较器
新建一个比较类继承Comparator类,并且对Compare方法进行重写。
public class PersonCompare implements Comparator<Person> {
@Override
public int compare(Person p1, Person p2) {
// return p1.getAge() - p2.getAge();
return p1.getName().compareTo(p2.getName());
}
}
测试类:
Person p1 = new Person("zhangsan",19);
Person p2 = new Person("lisi",10);
Person p3 = new Person("wangwu",29);
Person p4 = new Person("zhaoliu",21);
Person [] personArr = {p1,p2,p3,p4};
Arrays.sort(personArr,new PersonCompare());
System.out.println(Arrays.toString(personArr));
日期
Date
Date d = new Date();
//横线表示为过期方法,月份从0开始
Date d = new Date(109,7,1) ;// 年份是加上1900 2009-8-1 0:00:00
Date d2 = new Date() ;//当前时间
System.out.println(d.equals(d2));//false
System.out.println(d.after(d2));//false
System.out.println(d.before(d2));//true
System.out.println(d.compareTo(d2));//-1
d2.setHours(14);//过期方法,改变了d2的时间
System.out.println(d2.getDate());//日
System.out.println(d2.getHours());//时
System.out.println(d2.getYear());//年:默认-1900
System.out.println(d2.toLocaleString());//2020-8-3 14:40:34
System.out.println(d.getTime());// 这个d时间点距离历元时间点之间的时间段(毫秒)
System.out.println(d2.toGMTString());//3 Aug 2020 06:39:39 GMT
System.out.println(d2.toString());//Mon Aug 03 14:39:39 CST 2020
Calendar
Calendar c = Calendar.getInstance();
Calendar c = Calendar.getInstance();
c.add(Calendar.YEAR, -1);//当前年份-1
c.add(Calendar.MONTH, -1);//当前月份-1
// c.clear(Calendar.YEAR);//清除年份:1970
// c.clear();//清除日期
System.out.println(c.get(Calendar.YEAR));
System.out.println(c.get(Calendar.MONTH));
c.set(2020, 4, 3);
c.set(2019, 4, 9, 10, 9, 7);
//两个方法同时利用:
// Calendar --> Date
System.out.println(c.getTime().toLocaleString());
// Date ---> Calendar
c.setTime(new Date());//设置为当前时间
System.out.println(c.getTime());
格式化时间
SimpleDateFormat sdf = new SimpleDateFormat();
括号中为自定义的字符串格式地址
String s = "03-08-2020 14:51:50" ;
String pattern = "dd-MM-yyyy HH:mm:ss" ;
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date d = sdf.parse(s);
System.out.println(d.toLocaleString());//2020-8-3 14:51:50
Date d = new Date();
String s = "dd-MM-yyyy HH:mm:ss" ;
SimpleDateFormat sdf = new SimpleDateFormat(s);
System.out.println(sdf.format(d));//03-08-2020 19:06:47
字符串
String
引用数据类型,底层是通过char[ ]实现的,是final类型,不可进行更改
- 可以存在堆或常量池
String s = new String("abc") ;
String s3 = new String("abc") ;//在堆中开辟新的空间
System.out.println(s == s3);//false,地址指向不同
String s1 = "abc" ;
System.out.println(s == s1);//false,s存放的空间在堆里,s1存放的东西在常量池
String s2 = "abc" ;// 去常量池中看,如果已经有了abc则直接使用,如果没有则重新创建并指向。
System.out.println(s1 == s2);//true,s1和s2都指向常量池中的“abc”
s2 = "def" ;//常量池里开辟新的空间
System.out.println(s1 == s2);//false
- 字符串的“增删改查”
String s = new String(" abc DEFcd ce ");
String s1 = "abcdefcc" ;
System.out.println(s.charAt(3));//c
System.out.println(s.concat(" seven"));//最后加上seven: abc DEFcd ce seven
System.out.println(s.endsWith(".txt"));//false,不是以txt结尾
System.out.println(s.indexOf("cd"));//cd存在的地方
System.out.println(s.startsWith("ab"));//false,不是以ab开头
System.out.println(s.substring(3,6));//[3,6)的字符
System.out.println(s.toLowerCase());//大写
System.out.println(s.toUpperCase());//小写
System.out.println(s.trim());// 去掉2头的空白字符
System.out.println(String.valueOf(true));//true
- inner改变字符串的地址从堆里改成常量池
System.out.println("-->"+s.intern());// 返回字符串在常量池中的引用,不改变字符串的内容
System.out.println(s == s.intern());//false,s在堆里,s.intern()在常量池
s = s.intern();//s的地址改成常量池
System.out.println(s == s.intern());//true
System.out.println(s == s1); //false,s1在常量池,s在堆里
System.out.println(s.length());//字符串长度
System.out.println(s.replace("cd", "hello"));//cd替换为hello
- 字符串划分
String s2 = "2020\\08\\03";
String [] arr = s2.split("\\\\");
System.out.println(Arrays.toString(arr));//[2020, 08, 03]
String s3 = "20200803";
String arr1[] = s3.split("");
System.out.println(Arrays.toString(arr1));//[2, 0, 2, 0, 0, 8, 0, 3]
StringBuffer与StringBuilder
StringBuffer:单线程,线程安全,效率低
StringBuilder:多线程,线程不安全,效率高
---StringBuffer---
StringBuffer sb = new StringBuffer("abc");
sb.append("def").append("abc");//给原字符串加新上的字符串
System.out.println(sb);//abcdefabc
sb.reverse();//倒序
System.out.println(sb.toString());//cbafedcba
---StringBuilder---
StringBuilder sb1 = new StringBuilder("abc");
sb1.append("hhh").append("kkk");
System.out.println(sb1.toString());
sb1.reverse();
System.out.println(sb1);
String典型案例
- 常量池+常量池==常量池
String str1 = "ab" + "cd";
// java--->class("abcd"),转换为class文件时能自动识别成常量
String str11 = "abcd";
System.out.println(str1 == str11); //false
- 常量池+堆!=常量池
String str2 = "ab";
String str3 = "cd";
String str4 = str2+str3;//str2和str3无法识别为常量池,而在堆里
String str5 = "abcd";
System.out.println((str4==str5));//false
- final定义的属性是常量属性
final String str2 = "ab"; //常量池
final String str3 = "cd";
String str4 = str2+str3; //常量
String str5 = "abcd";
System.out.println(str4==str5);//true
- 大小写转换
String s1="Hello";
String s2="hello";
String s3=s1.toLowerCase();
String s4=s2.toLowerCase();
System.out.println(s1==s3);// false
System.out.println(s2==s3);// false
System.out.println(s2==s4);// true
System.out.println(s3==s4);//false
- 字符串对象创建
String s1 = new String("Hello");
String s2 = new String("Hello");
下面的代码将创建几个字符串对象?
3个,s1,s2,“hello”
String s1 = new String("abc");
String s2 = new String("abc");
System.out.println(s1 == s2);//false
System.out.println(s1.equals(s2));//true
String s1 = "abc";
String s2 = new String("abc");
s2.intern();
System.out.println(s1 ==s2);//false
s2 = s2.intern();
System.out.println(s1 ==s2);//true
- 浅谈一下String, StringBuffer,StringBuilder的区别?
包装类
八大包装类型:
byte short int long float double char boolean对应的包装类型分别为 Byte Short Integer Long Float Double Char Boolean
以Integer为例:
Integer
/*
* Integer
*/
int i1 = 1;//基本
Integer i2 = new Integer(1);
Integer i3 = new Integer("1");
Integer i4 = 199;
int i5 = i2;
System.out.println(i2.byteValue());//1
System.out.println(i4.byteValue());//-57
System.out.println(i3.equals(i2));//true
int i6 = Integer.parseInt("5566");//字符串-->基本
System.out.println(i6);
System.out.println(Integer.toBinaryString(i6));//二
System.out.println(Integer.toHexString(i6));//十六
System.out.println(Integer.toOctalString(i6));//八
String s = Integer.toString(2);
String s1 = "" + 4;
Integer i8 = Integer.valueOf(5);
System.out.println(i8);//5
Integer i7 = Integer.valueOf("222");
System.out.println(i7);//222
System.out.println(Integer.MAX_VALUE);//2147483647
System.out.println(Integer.MIN_VALUE);//-2147483648
/*
* 1,(-128 127):
* 2,其它:相当于new
*/
Integer i1 = 1 ;
Integer i2 = 1 ;
Integer i3 = 130 ;// new Integer(130) 在堆中开辟空间
Integer i4 = 130 ;// new Integer(130) 在堆中开辟空间
System.out.println(i1 == i2);//true
System.out.println(i3 == i4);//false
BigDecimal和BigInteger
/*
* BigDecimal:大浮点型
* BigInteger:大整数型
* 理论上能够表示无限大的数
*/
BigDecimal b1 = new BigDecimal("123.12312312312333333345345") ;
BigDecimal b2 = new BigDecimal("34.345345345333335345345345") ;
System.out.println(b1.add(b2));//b1+b2
System.out.println(b1.subtract(b2));//-
System.out.println(b1.multiply(b2));//*
System.out.println(b1.divide(b2,3,BigDecimal.ROUND_DOWN));//b1/b2,保留3位小数,向下取
System.out.println(b1.divide(b2,3,BigDecimal.ROUND_UP));//b1/b2,保留3位小数,向上取
System.out.println(b1.divide(b2,3,BigDecimal.ROUND_HALF_UP));//b1/b2,保留3位小数,小数点后第四位>5向上取
BigInteger b3 = new BigInteger("2342342342375675");
BigInteger b4 = new BigInteger("234232353534534575");
System.out.println(b3.add(b4));
System.out.println(b3.subtract(b4));
System.out.println(b3.multiply(b4));
System.out.println(b3.divide(b4));
单元测试junit
用法:1.导包 2.写测试的方法,不要有参数,也不要返回 3.右键 junit
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class Demo01 {
@Before
public void beforeMethdod(){
System.out.println("before method ... ");
}
@After
public void afterMethdod(){
System.out.println("after method ... ");
}
@Test
public void testMethdod(){
System.out.println("hello first test method ....");
}
}
打印顺序为:before-->test-->after