Java-学习常用API总结

常用API工具类

1. StringBuffer和StringBuilder

  1. 解决的问题:我们知道,字符串常量一旦声明则不可改变,而字符串对象改变的仅仅是其中保存的地址的引用的指向,而且在使用字符串进行频繁的修改操作时,会产生大量的冗余空间,造成内存的大量浪费,而StringBuffer和StringBuilder类就是来解决此类问题的,即StringBuffer和StringBuilder类方便了用户进行字符串内容的频繁修改之上,用于解决因频繁修改字符串而产生大量冗余空间的问题。但是面对字符串的操作时,优先使用String,只有在需要频繁修改的时候才使用StringBuffer类。
  2. StringBuffer和StringBuilder的区别:
    相同点:两者底层都有char类型的可变长数组作为字符串的保存空间,与ArrayList类似。
    不同点:StringBuffer 线程安全,但是效率较低,StringBuilder线程不安全,但是效率较高。
  3. Constructor:
    StringBuffer( ) :创建一个未存储任何字符串信息的空StringBuffer空间,底层初始化一个16个字符的char类型的空数组。
    StringBuffer(String str) :根据提供的String类型字符串创建对应的StringBuffer空间,底层char类型数组的容量为str.length + 16 来初始化,保存str。
/*
 * StringBuffer构造方法演示
 * StringBuilder同理
 */
public class Demo1 {
	public static void main(String[] args) {
		StringBuffer stringBuffer = new StringBuffer();
		StringBuffer stringBuffer2 = new StringBuffer("有参构造");

		System.out.println(stringBuffer);
		System.out.println(stringBuffer2);
	
	}
}
  1. Method(展示StringBuffer区别与String类的方法):
    添加方法:
        append(Object obj) :在StringBuffer类对象中,添加另外的数据,不管传进来的是什么类型,都当做字符串处理。
        insert(int index , Object obj) :在指定的下标位置,添加其他的内容,当做字符串处理。
/*
 * StringBuffer和StringBuilder
 * 中的append和insert方法演示
 */
public class Demo2 {
	public static void main(String[] args) {
		StringBuffer stringBuffer = new StringBuffer(
					"武汉加油,中国加油,世界加油");
		
		
		stringBuffer.append(",必须的");
		stringBuffer.append(2020);
		stringBuffer.append(new Demo1());
		
		System.out.println(stringBuffer.toString());
		
		stringBuffer.insert(3, "磊哥最帅");
		
		System.out.println(stringBuffer.toString());
	}
}

修改方法:
    replace(int start , int end , String str) :从指定下标位置start开始,到end - 1 结束,使用指定字符串str替换整个范围。
    setCharAt(int index,char ch) :使用指定的字符替换指定下标位置的字符。

/*
 * StringBuffer和StringBuilder中的修改方法
 */
public class Demo4 {
	public static void main(String[] args) {
		StringBuffer stringBuffer = new StringBuffer("1000Phone教育");
		
		StringBuffer replace = stringBuffer.replace(1, 5, "999");
		System.out.println(replace);//会返回一个新的字符串,旧字符串也会产生修改
		System.out.println(stringBuffer);
		
		stringBuffer.setCharAt(0, '6');
		System.out.println(stringBuffer);
		
	}
}

删除和反序:
    delete(int start,int end) :删除指定范围内的字符,[start,end - 1)。
    deleteCharAt(int inex) :删除指定下标位置的字符。
    reverse( ) :字符串逆序。

/*
 * StringBuffer和StringBuilder的删除,逆序方法
 */
public class Demo5 {
	public static void main(String[] args) {
		StringBuffer stringBuffer = new StringBuffer("牛肉包,正香");
		
		System.out.println(stringBuffer);
		
		StringBuffer delete = stringBuffer.delete(4, 5);
		//返回的新的字符串
		System.out.println(delete);
		//修改操作之后的原字符串
		System.out.println(stringBuffer);
		
		StringBuffer deleteCharAt = stringBuffer.deleteCharAt(3);
		System.out.println(deleteCharAt);
		System.out.println(stringBuffer);
		
		stringBuffer.reverse();
		System.out.println(stringBuffer);
	}
}

2. Math

  • 常用方法:
        public static double abs(double a) :取绝对值。
        public static double ceil(double a) :向上取整。
        public static double floor(double a) :向下取整。
        public static double round(double a ) :四舍五入。
        public static double random( ) :产生一个随机数,从0.0 - 1.0之间。
/*
 * Math工具类当中的常用方法
 */
public class Demo001 {
	public static void main(String[] args) {
		//1.取绝对值
		System.out.println(Math.abs(2.3));
		System.out.println(Math.abs(-2.3));
		
		//2.向上取整
		System.out.println(Math.ceil(4.6));
		System.out.println(Math.ceil(-4.6));
		
		//3.向下取整
		System.out.println(Math.floor(3.4));
		System.out.println(Math.floor(-3.4));
		
		//4.四舍五入
		System.out.println(Math.round(2.4));
		System.out.println(Math.round(2.5));
		System.out.println(Math.round(-2.4));
		System.out.println(Math.round(-2.5));
		System.out.println(Math.round(-2.6));
	}
}

3. Date日期类(大部分方法被淘汰)

  • 常用方法:
        public Date( ) :创建一个Date类对象,对应当前时间,精度在毫秒值。
        public Date(long date) :根据时间戳毫秒数,创建对应的Date对象。
        public long getTime( ) :通过Date类对象获取对应当前时间的毫秒数。
/*
 * Data类演示
 */
public class Demo1 {
	public static void main(String[] args) {
		Date date = new Date();
		System.out.println(date);
		
		File file = new File("E:/aaa/1.txt");
		long lastModified = file.lastModified();
		
		Date date2 = new Date(lastModified);
		System.out.println(date2);
		
		System.out.println(System.currentTimeMillis());
		System.out.println(date.getTime());
		System.out.println(date2.getTime());
		
	}
}

4. SimpleDateFormat

  • Constructor:
        public SimpleDateFormat(String pattern) :创建一个SimpleDateFormat对象。
  • Method:
        public String format(Date date):根据构造方法指定的匹配要求,转换Date为字符串。
        public Date parse(String fomatStr):按照指定的匹配规则,解析对应的字符串,返回一个Date类型的数据。
  • pattern:
        年:y
        月:M
        日:d
        时(24):H
        分:m
        秒:s
        毫秒:S
/*
 * DateFormat抽象类
 * SimpleDateFormat 演示
 */
public class Demo2 {
	public static void main(String[] args) throws ParseException {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
					"yyyy年MM月dd日 HH:mm:ss");
		
		String format = simpleDateFormat.format(new Date());
		System.out.println(format);
		
		Date parse = simpleDateFormat.parse("2020年3月11日 23:33:33");
		System.out.println(parse);
		String format2 = simpleDateFormat.format(parse);
		System.out.println(format2);
	}
}

5. Calendar日历类

  • 概述:Calendar日历类,替换了很多Date类的方法,把很多属性都作为静态的属性,通过一些特定方法来获取,比Date处理日期数据更加方便,且Calendar类是一个abstract类,没有自己的类对象,通过特定的方法getInstance( ) 获取对象。
  • 获取对象的方法:
        public static Calendar getInstance( ) :默认为当前系统时区的Calendar类对象。
  • Method:
        public int get(int field) : 返回特定数据的数值。
        public void set(int field , int value) :设置特定字段对应的数据。
  • 常用字段名:
        YEAR:年
        MONTH:月,从0开始,使用时可以加1。
        DAY_OF_MONTH:日。
        HOUR:12小时制,时。
        HOUR_OF_DAY:24小时制,时。
        MINUTE:分钟。
        SECOND:秒。
        DAY_OF_WEEK:周几,周日为1,使用时可以减1。
/*
 * 日历类Canlender
 * 演示
 */
public class Demo3 {
	public static void main(String[] args) {
		//1.得到一个Canlender类对象
		Calendar canlendar = Calendar.getInstance();
		
		int year = canlendar.get(Calendar.YEAR);
		
		int month = canlendar.get(Calendar.MONTH) + 1;
		
		int day = canlendar.get(Calendar.DAY_OF_MONTH);
	
		int hour = canlendar.get(Calendar.HOUR_OF_DAY);
		
		int minute = canlendar.get(Calendar.MINUTE);
		
		int second = canlendar.get(Calendar.SECOND);
		
		int dayweek = canlendar.get(Calendar.DAY_OF_WEEK) - 1;
		
		System.out.println(year + "-" + month + "-" + 
			day + " " + hour + ":" + minute + ":" + second + " " + dayweek);
		
		canlendar.set(Calendar.YEAR, 1890);
		
		Date time = canlendar.getTime();
		System.out.println(time);
		
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
								"yyyy年MM月dd日 HH:mm:ss E");
		String format = simpleDateFormat.format(time);
		System.out.println(format);
	}
}

6. System

  • 常用方法:
        public static long currentTimeMillis( ) :获取当前时间戳。
        public static Properties getProperties( ): 获取系统属性。
        public static void exit(int status) :退出当前程序。
        public static void arrayCopy(Object src , int srcPos , Object dest , int socDest , int length) :数组拷贝。
        public static void gc( ) :间接调用了Runtime类的gc( )。
        protected void finalize( ) :一个对象被回收时,希望在一个对象收尾的时候执行一些收尾工作,则对象所在的类可以实现一个finalize( )方法,该方法由Object定义。
public class Demo1 {
	public static void main(String[] args) {
		Properties properties = System.getProperties();
		properties.list(System.out);
		
		String property = System.getProperty("os.name");
		System.out.println(property);
		
		int[] arr = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10};
		int[] temp = new int[10];
		System.arraycopy(arr, 0, temp, 0, arr.length);
		
		System.out.println(Arrays.toString(temp));
		
		for (int i = 0; i < 100; i++) {
			System.out.println(System.currentTimeMillis());
		}
	}
}

7. Runtime

  • 在每一个JVM的进程之中,都会存在一个运行时的操作类对象,而这对象所属的类型就是Runtime类。观察Runtime类源码,发现并没有构造方法的定义,但实际上Runtime类的构造存在,只是不希望被外部所看见,因为被私有化了,这是一个标准的单例设计模式。
  • 取得对象的方法:
        public static Runtime getRuntime( ) ;
  • Method:
        public long maxMemory( ) :程序所能申请的最大内存。
        public long totalMemory( ) :目前程序使用的总内存。
        public long freeMemory( ) :目前程序使用后的剩余内存。
        public void gc( ) :GC机制。
        public Process exec(String exePath) : 开启一个程序。
public class Demo1 {
	public static void main(String[] args) throws IOException, 
					InterruptedException {
		Runtime runtime = Runtime.getRuntime();

		System.out.println(runtime.totalMemory());
		System.out.println(runtime.freeMemory());
		System.out.println(runtime.maxMemory());

		Process exec = runtime.exec(
			"\"D:\\Program Files\\Notepad++\\notepad++.exe\"");

		Thread.sleep(5000);

		exec.destroy();
	}
}

8. 包装类

  • 基本数据类型及对应的包装类:
        byte: Byte。
        short: Short。
        int: Integer。
        long: Long。
        float: Float。
        double: Double。
        boolean: Boolean布尔类型包装类。
        char: Character字符包装类。
  • 将字符串转为基本数据类型:
        public static byte parstByte(String str) ;
        public static short parstShort(String str) ;
        public static int parseInt(String str);
        public static long parseLong(String str);
        public static float parseFloat(String str);
        public static double parseDouble(String str);
        public static boolean parseBoolean(String str);
        public char[ ] toCharArray( );
  • 将基本数据类型转为字符串:
        public static String valueOf(Object obj);
public class Demo2 {
	public static void main(String[] args) {
		byte parseByte = Byte.parseByte("1");
		short parseShort = Short.parseShort("123");
		int parseInt = Integer.parseInt("100");
		long parseLong = Long.parseLong("1000");

		float parseFloat = Float.parseFloat("3.14");
		double parseDouble = Double.parseDouble("0.618");

		boolean parseBoolean = Boolean.parseBoolean("true");

		System.out.println(parseBoolean);

		// 传入的字符串无法解析对应的数据,NumberFormatException
//		System.out.println(Integer.parseInt("123abc"));

	}
}
发布了13 篇原创文章 · 获赞 13 · 访问量 4774

猜你喜欢

转载自blog.csdn.net/cccccv_/article/details/104679332