java常用类库(java基础篇)

1.StringBuffer类
String类特点:一种是直接赋值,只会开辟一块堆内存空间,而且对象可以自动入池,另一种方式使用构造方法完成,会开辟凉快空间,有一块空间将成为垃圾,并且不会自动入池,但是可以intern()方法手工入池
字符串常量一旦声明不可改变,而字符串对象可以改变,但是改变的是其内存地址的指向;通过以上的几个特点就可以清楚的发现,String类是表示字符串使用最多的类,但是其不适合于被频繁修改的字符串操作上,所以在这种情况下,往往可以使用StringBuffer类,即:StringBuffer类方便用户进行内容修改。在String类之中使用"+"作为数据库的连接操作,而在StringBuffer类之中使用append()方法进行数据库的连接

eg

1.StringBuffer类 方便用户进行内容的修改 不用开辟新空间 添加用append方法 字段的反复相加

public class StringbufferDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		StringBuffer buf = new StringBuffer();
		buf.append("hello").append(" java");
		func(buf);
		System.out.println(buf);
	}
	
	public static void func(StringBuffer temp) {
		temp.append("\n").append(" !!!");
	}

}

如果看到CharSequence 传字符串

//		String str = "Hello World";
//		StringBuffer buf = new StringBuffer(str);//String --> StringBuffer 
//		System.out.println(buf);
		
//		StringBuffer buf = new StringBuffer();
//		buf.append("hello").append(" java");
//		System.out.println(buf.toString());//Stringbuffer --> String

	public static void func(StringBuffer temp) {
		temp.append("\n").append(" !!!");
	}
	
	public static void func(CharSequence te) {
		te.toString();
	}

1.2.reverse 反转操作 反转序列

//		StringBuffer buf = new StringBuffer();
//		buf.append("hello java");
//		System.out.println(buf.reverse());//反转

1.3.replace 替换

		StringBuffer buf = new StringBuffer();
		buf.append("hello java .");
		System.out.println(buf.replace(6, 12, "java"));

1.4 insert 插入

		StringBuffer buf = new StringBuffer();
		buf.append("world .").insert(0, "hello ");
		System.out.println(buf);
2.Runtime类 采用单例模式
在jvm进程中 运行时都会产生runtime这个类 不能直接new 要获得只能通过getRuntime获得
得到这个对象可以获得jvm信息
循环过后 内存max没变 total 变大 free释放 gc垃圾回收后 free变大
作用 监控控制台界面

eg

	Runtime run = Runtime.getRuntime();
	
	System.out.println(run.maxMemory());
	System.out.println(run.totalMemory());
	System.out.println(run.freeMemory());
	
	String str = "";
	for(int x = 0 ;x < 300000 ; x++) {
		str += str;
	}
	System.out.println(run.maxMemory());
	System.out.println(run.totalMemory());
	System.out.println(run.freeMemory());
	run.gc();
	System.out.println(run.maxMemory());
	System.out.println(run.totalMemory());
	System.out.println(run.freeMemory());

jvm处理方式 1.jvm不定期执行处理 2.手工

exit终止当前的java虚拟机
		print();
		System.out.println("main...");
	}
	
	public static void print() {
		for(int x = 0 ;x < 1000 ; x++) {
			if(x>50) {
				Runtime.getRuntime().exit(-1);//直接结束程序
//				return;//结束方法
			}
			System.out.println(x);
		}
	}
3.System类
静态 取得当前时间 
currentTimeMillis 时间

eg

public static void main(String[] args) {
	// TODO Auto-generated method stub
	long start = System.currentTimeMillis();//开始
	
	String str = "";
	for(int x = 0 ;x < 50000 ; x++) {
		str += str;
	}
	//System.out.println(str);

	long end = System.currentTimeMillis();//结束
	
	System.out.println(end - start);//运行时间差
}

system.gc() = 

finalize 垃圾回收调用这个方法
final 终结器
finally 异常的出口

4.日期操作类
java.util.Date
Date()输出英文
getTime方法 毫秒数
		Date date = new Date();
		long num = date.getTime();
		System.out.println(date);
		System.out.println(num);//毫秒数
currentTimeMillis 获取当前毫秒值
		Date data = new Date(System.currentTimeMillis());
		System.out.println(data);
SimpleDateFormat 日期格式化
 年(yyyy)月(MM)日(dd)时(HH)分(mm)秒(ss)毫秒(SSS)

1.构造 2.日期格式化为字符串 format 3.字符串格式化为日期 parse
日期转字符串

	Date date = new Date();
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
	String str = sdf.format(date);
	System.out.println(str);

字符串转日期

String str = "2020-03-03 14:19:28:496";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
		try {
			Date date = sdf.parse(str);
			System.out.println(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
5.随机数类 Random 生成尾随机数流
nextInt 返回一个伪随机数
数学类Math类 java.long
round 返回最接近参数的值 四舍五入

eg

	System.out.println(Math.random());

	System.out.println(Math.round(-15.5));//四舍五入
6.大数操作类
如果数字超出范围 只能用字符串表示 如果想要计算可以使用 number子类下的
BigInteger 大 整数操作类 返回的都是BigInteger对象 

eg

	BigInteger bigA = new BigInteger("1234567890123456");
	BigInteger bigB = new BigInteger("123456789012345");
	System.out.println("+fa = "+bigA.add(bigB));
	System.out.println("-fa = "+bigA.subtract(bigB));
	System.out.println("*fa = "+bigA.multiply(bigB));
	System.out.println("/fa = "+bigA.divide(bigB));//结果是整除
	
	BigInteger res[] = bigA.divideAndRemainder(bigB);
	System.out.println("商:"+res[0]+" 余数 "+res[1]);

BigDecimal 大 小数操作类 保留小数 并四舍五入

		System.out.println(MyMath.rond(123.98765, 2));

class MyMath{
	public static double rond(double num,int scale) {
		BigDecimal big = new BigDecimal(num);
		BigDecimal result = big.divide(new BigDecimal(1),scale,BigDecimal.ROUND_HALF_UP);
		
		return result.doubleValue();
	}
}
7.数组操作类 Arrays uitl包下

二分搜索 二分查找 binarySearch 返回索引的位置 前提必须先排序 通过sort方法

	int data[] = {1,2,3,4,5,6,7,8,9,10};
	int index = Arrays.binarySearch(data, 3);
	
	System.out.println(index);

equals 数组比较

	int dataA[] = {1,2,3};
	int dataB[] = {1,2,3};
	System.out.println(Arrays.equals(dataA, dataB));

fill 数组填充 每个元素覆盖成指定元素

	Arrays.fill(dataA, 4);//数组的填充
	System.out.println(Arrays.toString(dataA));

sort数组的排序 数组中的所有元素都必须实现comparable接口

8.比较器comparable
为一组对象排序 区分对象
compareTo方法 返回int型数据 用户覆写时 < > =

comparator接口
compare方法 比较用来排序的两个参数
equals方法 指示某个对象是否等于此comparator 

eg

public class Person {
	private String name;
	private int age;
	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", 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;
	}
	
}

public class PersonComparetor implements Comparator<Person> {

	@Override
	public int compare(Person o1, Person o2) {
		// TODO Auto-generated method stub
		if(o1.getAge() > o2.getAge()) {
			return 1;
		}else if(o1.getAge() < o2.getAge()) {
			return -1;
		}else {
			return 0;
		}
	}
}

public class Test1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Person per[] = new Person[] {
			new Person("zhangsan", 25),	
			new Person("lisi", 23),	
			new Person("wangwu", 24),	
		};
		
		Arrays.sort(per, new PersonComparetor());//(数组,刚才写的对象)
		System.out.println(Arrays.toString(per));
	
		
	}

}

两种比较器的区别
comparable 是一个默认的实现好的接口 里面只有一个compareTo方法
comparator 比较器接口 需要单独定义一个比较器规则类 里面有两个方法 compare equals

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值