java中常用的工具类

本文深入探讨了Java中常用的工具类,包括String类的特性与方法、StringBuilder与StringBuffer的性能对比、Math类的数学运算、Date与Calendar类的日期时间操作、DecimalFormat类的格式化、System类的系统操作及Runtime类的运行时环境管理。

java中常用的工具类

1.String类

  • String类可以存储字符序列,它不是基本数据类型,而是引用类型。
  • String类内部使用字符数组来保存字符串。
  • 字符串对象是不可变的,对字符串对象的任何修改都将创建一个新的对象,而原来的字符串对象保存不变。
public class Test {
	public static void main(String[] args) {
		//str1是对象变量
		String str1 = "value1";
		//concat方法将参数字符串连接到此字符串的末尾
		String str2 = str1.concat("value2");
		System.out.println(str1); //value1,不改变原字符串对象
		System.out.println(str2);//value1value2
		str1 = "value3";
		System.out.println(str1);//value3
		}
}
  • 所有的字符串字面值都被存储在字符串常量池中。如果编译器发现了一个字符串字面值,它首先会检查字符串常量池中有没有,如果有的话直接重用,如果没有,它会创建一个新的字符串对象,并放入常量池中。
public class Test {
	public static void main(String[] args) {
		String str1 = "value"; //创建一个字符串对象,放入字符串常量池,并将其引用赋值给str1
		String str2 = "value";//从字符串常量池中获取对象,并将引用赋值给str2
		System.out.println(str1 == str2); //true,str1和str2引用同一个字符串对象
		}
}
public class Test{
	public static void main(String[] args) {
		String str1 = "value"; //创建一个字符串对象,放入字符串常量池,并将其引用赋值给str1
		String str2 = new String("value");//使用new操作符总是创建对象
		System.out.println(str1 == str2); //false
		}
}
String类的常用方法

获取字符串的信息

public class Test{
	public static void main(String[] args) {
		//char charAt(int paramInt),获取指定索引处的字符
		System.out.println("abcdefg".charAt(2)); // c
		
		//boolean contains(CharSequence s),测试此字符串是否包含指定的字符序列
		System.out.println("abcdefg".contains("cde"));//true
		
		//int length(),返回此字符串的长度。
		System.out.println("ABCDEFGH".length());//8
		
		//boolean startsWith(String prefix),测试此字符串是否以指定的前缀开始。
		System.out.println("abcdefghij".startsWith("abc")); //true
		
		//boolean endsWith(String suffix),测试此字符串是否以指定的后缀结束。
		System.out.println("abcdefghij".endsWith("ij")); //true
		
		//int indexOf(int ch),返回指定字符在此字符串中第一次出现处的索引。
		System.out.println("abcdefghij".indexOf('c'));//2
		
		/*int indexOf(int ch, int fromIndex),返回在此字符串中第一次出现指定字符处的索引,从
指定的索引开始搜索。*/
		System.out.println("abcdefghij".indexOf('c', 3));//-1
		
		//int indexOf(String str),返回指定子字符串在此字符串中第一次出现处的索引。
		
		/*int indexOf(String str, int fromIndex),返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。*/
		
		//int lastIndexOf(int ch),返回指定字符在此字符串中最后一次出现处的索引。
		
		/*int lastIndexOf(int ch, int fromIndex),返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。*/
		
		//int lastIndexOf(String str),返回指定子字符串在此字符串中最后一次出现处的索引。
		
		/*int lastIndexOf(String str, int fromIndex),返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。*/
		
		//boolean equalsIgnoreCase(String anotherString)
		
		//将此 字符串 与另一个 字符串 比较,不考虑大小写。
		
		System.out.println("ABC".equalsIgnoreCase("abc"));//true
		}
}

字符串操作方法 字符串对象是不可变的,对字符串对象的任何修改都将创建一个新的对象,而原来的字符串对象保存不变。

public class Test{
	public static void main(String[] args) {
		String str = "abcdefghijk";
		
		//String concat(String paramString),将指定字符串连接到此字符串的结尾。
		System.out.println(str.concat("lmn"));//abcdefghijklmn
		System.out.println(str);//abcdefghijk
		
		//String replace(char oldChar, char newChar)
		//返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
		System.out.println("012301230123".replace('0', '4'));//412341234123
		
		//String replace(CharSequence target, CharSequence replacement)
		//返回一个新的字符串,它是通过用 replacement 替换此字符串中出现的所有 target 得到的。
		System.out.println("012301230123".replace("01", "45"));//452345234523
		
		//String toLowerCase()
		//将此 String 中的所有字符都转换为小写。
		System.out.println("ABCDEFGHIJ".toLowerCase()); //abcdefghij
		
		//String toUpperCase()
		//将此 String 中的所有字符都转换为大写。
		System.out.println("abcdefghij".toUpperCase()); //ABCDEFGHIJ
		
		//String trim()
		//返回字符串的副本,忽略前导空白和尾部空白。
		System.out.println(" ab cd ".trim()); //ab cd
		
		//String substring(int beginIndex),获取从指定索引处开始的子字符串
		System.out.println("abcdefghij".substring(3)); //defghij
		
		//String substring(int beginIndex,int endIndex)
		//获取beginIndex和endIndex之间的子字符串,包含开始字符,不包含结束字符。
		System.out.println("abcdefghij".substring(3,7)); //defg
		}
}

2.StringBuilder和StringBuffer

  • 频繁的使用+操作符进行字符串拼接会创建大量的字符串对象,更加优雅的做法是使用StringBuilder和StringBuffer。
  • StringBuilder不是线程安全的,但是执行效率高。
  • StringBuffer是线程安全的,但是执行效率低。
  • StringBuffer的方法和StringBuilder的方法一样。
public class Test{
	public static void main(String[] args) {
		StringBuilder s1 = new StringBuilder();
		//将世追加到StringBuilder对象的末尾
		s1.append("世");
		//将界追加到StringBuilder对象的末尾
		s1.append("界");
		//将和追加到StringBuilder对象的末尾
		s1.append("和");
		//将平追加到StringBuilder对象的末尾
		s1.append("平");
		//将内容以字符串对象返回
		String str = s1.toString();
		System.out.println(str);
		// append方法将参数拼接到内部的字符数组中并返回this,因此可以进行后续拼接
		s1.append("1").append('2').append('3');
		System.out.println(s1);
		}
}

3.Math数学类

public class Test{
	public static void main(String[] args) {
		System.out.println(Math.sqrt(2.25));//平方根 1.5
		System.out.println(Math.abs(-2));//绝对值 2
		System.out.println(Math.pow(3, 3));//幂值 27.0
		System.out.println(Math.max(3, 4));//最大值4
		System.out.println(Math.min(3, 4));//最小值3
		System.out.println(Math.random());//大于等于0,小于1的double类型的数
		System.out.println(Math.round(14.5));//四舍五入 15
		//5-10之间的随机数
		int i = (int) (Math.random() * 5) + 5;
		System.out.println(i);
		}
}

4.Date日期类

java.util.Date类现在已经不推荐使用了,此类的很多方法都被标注为已过时。Date类对象存储从1970年1月1日0时0分0秒到创建时的毫秒数。

public class Day1614 {
	public static void main(String[] args) throws ParseException {
		Date date = new Date();
		//返回距1970年1月1日0时0分0秒的毫秒数
		System.out.println(date.getTime());//1562508319386
		System.out.println(date);//Sun Jul 07 22:06:35 CST 2019
		
		//对于日期的所有操作都需要将时间转换为毫秒数来进行修改。
		//增加6个小时
		date.setTime(date.getTime() + 6 * 60 * 60 * 1000);
		System.out.println(date);//Mon Jul 08 04:09:12 CST 2019
		
		//减少6个小时
		date.setTime(date.getTime() - 6 * 60 * 60 * 1000);
		System.out.println(date);//Sun Jul 07 22:10:25 CST 2019
		
		//可以使用SimpleDateFormat对日期进行格式化。
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss");
		String str = sdf.format(date);
		System.out.println(str);//2019-07-07:22:14:16
		
		//将一个字符串解析成一个日期对象
		Date d = sdf.parse("2019-07-07:22:14:16");
		System.out.println(d);//Sun Jul 07 22:14:16 CST 2019
		}
}

5.Calendar类

Calendar类可以对日期进行操作:增加和减少(年,月,日,时,分,秒) Calendar类是一个抽象类,不能直接创建实例。可以使用静态方法getInstance获得Calendar类的一个实例。

public class Test{
	public static void main(String[] args) {
		//获取一个Calendar实例
		Calendar cal = Calendar.getInstance();
		System.out.println(cal);
		
		//将月中的日修改为24
		cal.set(Calendar.DATE, 24);
		
		//修改月份为9月
		cal.set(Calendar.MONTH, 8);//8 - September
		
		//修改年为2010
		cal.set(Calendar.YEAR, 2010);
		System.out.println(cal.get(Calendar.YEAR));//2010
		
		System.out.println(cal.get(Calendar.MONTH));//8
		System.out.println(cal.get(Calendar.DATE));//24
		System.out.println(cal.get(Calendar.WEEK_OF_MONTH));//4
		System.out.println(cal.get(Calendar.WEEK_OF_YEAR));//39
		System.out.println(cal.get(Calendar.DAY_OF_YEAR));//267
		System.out.println(cal.getFirstDayOfWeek());//1 -> Calendar.SUNDAY
		
		//增加5日
		cal.add(Calendar.DATE, 5);
		System.out.println(cal.getTime());//Wed Sep 29 2010
	
		//增加1月
		cal.add(Calendar.MONTH, 1);
		System.out.println(cal.getTime());//Fri Oct 29 2010
		
		//增加2年
		cal.add(Calendar.YEAR, 2);
		System.out.println(cal.getTime());//Mon Oct 29 2012
		
		//将Calendar对象中的时间点转换为一个Date对象返回
		Date date = cal.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss");
		String str = sdf.format(date);
		System.out.println(str);
		}
}

6.DecimalFormat类

		DecimalFormat df1 = new DecimalFormat(".##");
		System.out.println(df1.format(1.12556));//1.12
		DecimalFormat df2 = new DecimalFormat(".##%");
		System.out.println(df2.format(0.12556));//12.55%

7.System类

		int[] arr1 = {1,2,3,4,5};
		int[] arr2 = new int[3];
		System.arraycopy(arr1, 1, arr2, 0, 3);//数组拷贝
		System.out.println(Arrays.toString(arr2));//[2,3,4]
		System.out.println(System.currentTimeMillis());//返回当前时间距1970年1月1日的毫秒数
		System.out.println(System.getenv("path"));//获取环境变量的值
		System.out.println(System.getProperty("java.version"));//获取系统属性
		System.out.println(System.err);//标准错误输出流
		System.out.println(System.in);//标准输入流
		System.out.println(System.out);//标准输出流

8.Runtime类

		Runtime runtime = Runtime.getRuntime();
		System.out.println(runtime.freeMemory());//java虚拟机空闲内存数
		System.out.println(runtime.maxMemory());//java虚拟机最大内存数

参考文献:
[1]https://www.jianshu.com/p/8c4ebddc9bb8
[2]http://tool.oschina.net/apidocs/apidoc?api=jdk_7u4

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值