JAVA--常用API

1.String

1.1是什么

String是Java.lang 包下的,使用不需要导入

String是字符串类,底层实现就是一个char数组,所以很多数组的特征就是字符串的特征

private final char value[];

1 字符串一旦创建,不能修改

2 创建对象方式有两种 : 1 "abc" 直接字面量形式创建 , 2 new String("abc") 形式创建

如果是String s1 = new String("abc")的方式,肯定会在堆内存创建对象,然后变量s1肯定也是保存堆内存的引用

如果是 String s2 = "abc";的方式,变量s2 直接指向常量池中的"abc"对象,

如果常量池中没有abc,那就创建一个abc,然后再让s2保存他

为了提高字符串的访问效率问题,虚拟机使用了一种缓存机制,每个字符串都会在常量池中保存一份

1.2基本使用

public static void main(String[] args) {
		String s1 = new String("abc");
		String s2 = "abc";
		String s3 = "abc";
		// 创建1个堆内存对象, 1个常量池对象
		// true
		System.out.println(s2 == s3);
		// false
		System.out.println(s2 == s1);
		// true
		System.out.println(s2.equals(s1));
	}

1.3构造方法

public static void main(String[] args) {
		// 创建方式
		// 1
		String s1 = "abc";
		// 2
		String s2 = new String();
		// 3 
		String s3 = new String("abc");
		// 字节数组
		byte[] bytes = {97,98,99,100,101};
		String s4 = new String(bytes);
		// abcde
		System.out.println(s4);
		// 字节数组中一部分转为字符串
		// 下标1开始(包含)  截取2个
		String s5 = new String(bytes,1,2);
		// bc
		System.out.println(s5);
		
		// 字符数组,用法同字节数组
		char[] chars = {'a','b','c','d','e'};
		String s6 = new String(chars);
		String s7 = new String(chars,2,2);
		// cd
		System.out.println(s7);
	}

1.4常用方法

public static void main(String[] args) {
		String s1 = "qwert";
		// char  charAt(int index) : 获取指定索引对应的字符值
		char c1 = s1.charAt(2);
		// e
		System.out.println(c1);
		// int length() : 获取字符串长度
		System.out.println("asdsa".length());
		
		// boolean endsWith (String suffix) : 判断是否以指定字符串结尾
		// boolean startsWith : 判断开始
		System.out.println("asnjkdasjkh.java".endsWith(".java"));
		
		// boolean equals(Object obj) : 判断是否相等
		System.out.println("assd".equals("asSd"));
		// 不区分大小写,判断是否相等
		System.out.println("assd".equalsIgnoreCase("asSd"));
		
		// byte[]  getBytes() : 把字符串转换为字节数组并返回
		byte[] bytes = "asd".getBytes();
		
		// int indexOf(String str) : 获取指定字符串的起始索引 , 如果不存在 返回-1
		System.out.println("asdhjkshjabf".indexOf("hj"));
		// 同上,找最后一次出现的索引
		System.out.println("asdhjkshjabf".lastIndexOf("hj"));
		
		// String replace(String s1,String s2) : 替换指定字符 
		// 把所有的a 换成1
		System.out.println("a.scvhda.fas.cfasf".replace("a", "1"));
		// replaceAll 同上, 只不过,支持正则表达式
		System.out.println("ascv.hda.fascfa.sf".replaceAll("a", "1"));
		
		// String substring(int begin) : 截取指定下标开始(包含),到末尾的子字符串
		// defg
		System.out.println("abcdefg".substring(3));
		// String substring(int beginIndex , int endIndex) : 
		// 截取指定下标开始(包含),到指定下标结束(不包含)的子字符串
		// de
		System.out.println("abcdefg".substring(3,5));
		
		// char[] toCharArray() : 返回对应的字符数组
		char[] chars = "asdasd".toCharArray();
		
		// String toUpperCase() :  转大写
		// String toLowerCase() : 转小写
		System.out.println("casnfDASFcasf".toUpperCase());
		System.out.println("casnfDASFcasf".toLowerCase());
		
		// String trim() : 删除字符串两边的空格
		System.out.println("   sd jjjjjj                sadasdsa            ".trim());
		
		// static String valueOf(Object obj) : 调用对象的toString方法,并且可以解决空指针异常
		Object o = new Object();
		o = null;
		// (obj == null) ? "null" : obj.toString();
		System.out.println(o);
	}

1.5注意事项

字符串比较要使用equals

public static void main(String[] args) {
		// 这种字面量拼接,编译的时候,就把+去掉了,就等于 "ab"
		String s1 = "a"+"b";
		String s2 = "ab";
		// true
		System.out.println(s1 == s2);
		
		String a = "a";
		String s3 = a + "b";
		// false , 因为 变量,编译时值是不能确定的,所以不会优化掉+
		System.out.println(s1 == s3);
		
		final String a1 = "a";
		String s4 = a1+"b";
		// true , final 修饰 不能更改,所以编译时就能确定值,所以可以优化掉+
		System.out.println(s4 == s1);
	}

2.StringBuffer 和 StringBuilder

2.1 是什么

String 、StringBuffer、StringBuilder 的区别
String 是字符串类型,定长,一旦确定,长度不可修改,不适合做频繁的字符串拼接工作
而 StringBuffer和StringBuilder 是变长,适用于做字符串拼接
StringBuilder 非线程安全,效率较快,但是多线程环境下可能出现问题
StringBuffer 线程安全,效率相对较慢,但是多线程环境下,依然不会出现问题
StringBuilder 和 StringBuffer 底层也是char数组,只不过该数组长度可变(会进行自动扩容)
默认的容量为16,扩容是 原来的 (length<<1) +2 是原来的 length的2倍+2

2.2使用

public static void main(String[] args) {
		// 创建对象
		StringBuilder sb = new StringBuilder();
		// 拼接字符串
		sb.append("a");
		// 可以链式调用
		sb.append("c").append("b").append("d");
		
		// 反转
		sb.reverse();
		
		// 已有元素个数(拼接了几个字符了)
		System.out.println(sb.length());
		// 数组长度(容量大小)
		System.out.println(sb.capacity());
		
		// 转换为String
		String string = sb.toString();
		System.out.println(string);
	}

3.包装类

3.1是什么

包装类 : 封装了基本类型的一些操作,更加方便
 byte   -->  Byte
 short  -->  Short
 char  -->  Character
 int  -->  Integer
 long  --> Long
 float --> Float
 double --> Double
 boolean  --> Boolean
对应的八种包装类,都在java.lang下,使用不需要导入
另外也都覆写了equals方法等,比较也是比较的值的大小

3.2 Integer

3.2.1基本使用

public static void main(String[] args) {
		// 最大值和最小值
		System.out.println(Integer.MAX_VALUE);
		System.out.println(Integer.MIN_VALUE);
		System.out.println(Long.MAX_VALUE);
		System.out.println(Long.MIN_VALUE);
		// 创建对象
		Integer i1 = new Integer(10);
		// 可以传入纯数字的字符串
		Integer i2 = new Integer("123");
		// 123  因为包装类都覆写了toString方法
		System.out.println(i2);
		// 编译可以通过,但是运行会报错
		//  java.lang.NumberFormatException: For input string: "123a"
		i2 = new Integer("123a");
	}

3.2.2 常用方法

public static void main(String[] args) {
		// int ---> Integer
		Integer i1 = new Integer(23);
		// 用于把int 转换为Integer类型
		i1 = Integer.valueOf(23);
		// 也可以把String转换为Integer
		i1 = Integer.valueOf("23");
		// intVlue : 把Integer类型的值转换为int类型
		int i2 = i1.intValue();
		Double d1 = new Double(2.3);
		double d2 = d1.doubleValue();
		
		// 重要  static int  parseInt(String str) : 把纯数字的字符串转换为int类型
		int i3 = Integer.parseInt("123");
		
		// static String toBinaryString(int value) : 把int类型,转换为二进制格式的字符串
		String string = Integer.toBinaryString(28);
		System.out.println(string);
		// 八进制
		System.out.println(Integer.toOctalString(17));
		// 十六进制
		System.out.println(Integer.toHexString(36));
		
	}

3.2.3 自动装箱和拆箱

自动拆箱和自动装箱,是java1.5提出,1.5之前都不支持
装箱 : 基本类型到引用包装类型 是装箱 int-->Integer
拆箱 : 包装类到基本类型 是拆箱 Integer-->int

public static void main(String[] args) {
		// 1.5之前
		// 装箱
		Integer i1 = Integer.valueOf(23);
		// 拆箱
		int i2 = i1.intValue();
		// 1.5开始
		// 自动装箱
		Integer i3 = 43; // 编译之后,就等于 i3 = Integer.valueOf(43);
		// 自动拆箱
		int i4 = i3;  // 编译后,就等于 i4 = i3.intValue();
		// 98 是int类型 会先进行自动装箱 为 Integer类型 Integer.valueOf(98)
		// 然后再发生向上转型(多态) 转换为Object类型
		m1(98);
		// 自动装箱为Integer类型 Integer.valueOf();
		m2(29, 10);
	}
	// 需要接收Object类型,就意味着,可以传递任何数据
	public static void m1(Object obj){
		// Integer中覆写了toString,所以打印98
		System.out.println(obj);
	}
	public static void m2(Integer i1 , Integer i2){
		// 自动拆箱为int类型,  intValue
		System.out.println(i1-i2);
	}

3.2.4 深入自动装箱-整型常量池

整型常量池 : 当我们需要进行自动装箱的时候,会自动调用Integer.valueOf()方法
在该方法中,会先经过整型常量池进行判断,如果值 在-128~127 直接,则不需要创建对象
因为会预先往整型常量池中存储-128~127 这256个对象,
所以如果在这个范围内,就直接把已创建的对象返回
但是如果是直接new的方式, 是不经过常量池的,直接在堆内存创建对象

public static void main(String[] args) {
		Integer i1 = 23; // 等于 是 i1 = Integer.valueOf(23)
		Integer i2 = 23;
		// true
		System.out.println(i1 == i2);
		Integer i3 = new Integer(44);
		Integer i4 = new Integer(44);
		// false
		System.out.println(i3 == i4);
		// true
		System.out.println(i3.equals(i4));

		i1 = 265; // 等于是 i1 = new Integer(265)
		i2 = 265;
		// false
		System.out.println(i1 == i2);
		// true
		System.out.println(i1.equals(i2));
	}

4 System

public static void main(String[] args) {
		// 计算时间差
		// 获取当前系统时间对应的毫秒数 , 1000毫秒=1秒
		long startTime = System.currentTimeMillis();
		System.out.println("执行的功能");
		long endTime = System.currentTimeMillis();
		System.out.println("耗时时间" + (endTime - startTime));
		// 打印
		System.out.println("打印语句");
		// 建议垃圾回收
		System.gc();
		// JVM关机 0 表示正常退出
		// 一般用于图形化界面关闭窗口
		System.exit(0);
		// 执行不到
		System.out.println("===");
	}

 5.Date

5.1是什么

时间类, java.util.Date
时间原点 : 1970.01.01 00:00:00 000
但是 东八区时间为 1970.1.1 08:00:00 000

5.2常用方法

public static void main(String[] args) {
		// 无参构造 获取当前系统时间
		Date d1 = new Date();
		// 有参构造 , 传入毫秒数,从时间原点(格林威治时间)到指定毫秒数对应的时间
//		d1 = new Date(1000*60*60);
		System.out.println(d1);
		// 星期 一周的第几天
		System.out.println(d1.getDay());
		// 日
		System.out.println(d1.getDate());
		// 月从0开始, 0是1月,  1是2月
		System.out.println(d1.getMonth());
		// 当前日期对应毫秒数
		System.out.println(d1.getTime());
		// 把时间转换为字符串
		System.out.println(d1.toString());
	}

5.3时间格式化

public static void main(String[] args) throws ParseException {
		// 创建时间对象
		Date d1 = new Date();
		// 创建格式化对象
		// y 年  M 月 d 日 H 小时 m 分 s 秒 S 毫秒
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss SSS");
		// 把时间对象,按照指定格式进行格式化,返回格式化之后的字符串
		// Date --> String
		String strDate = sdf.format(d1);
		System.out.println(strDate);
		
		// String --> Date
		// 字符串格式 要和 SimpleDateFormat 格式一致,否则会报错
		String str  = "2022年04月12日 09:41:00 860";
		d1 = sdf.parse(str);
		System.out.println(d1);
	}

5.4Calendar

public static void main(String[] args) {
		// 获取当前日历
		Calendar c = Calendar.getInstance();
		// System.out.println(c);
		// 获取 今天是本周的第几天, 周日是第一天
		System.out.println(c.get(Calendar.DAY_OF_WEEK));
		System.out.println(c.get(7));
		// 本月第几天,多少号
		System.out.println(c.get(Calendar.DAY_OF_MONTH));
		System.out.println(c.get(Calendar.YEAR));
		System.out.println(c.get(1));
		// 0是1月,1是2月 以此类推
		System.out.println(c.get(Calendar.MONTH));
		System.out.println(c.get(Calendar.DAY_OF_MONTH));
		// 12小时
		System.out.println(c.get(Calendar.HOUR));
		// 24小时
		System.out.println(c.get(Calendar.HOUR_OF_DAY));
		System.out.println(c.get(Calendar.MINUTE));
		System.out.println(c.get(Calendar.SECOND));
	}

6 Math

public static void main(String[] args) {
		// 常用的一些方法都是静态方法,提供了科学计算方式
		// 绝对值
		System.out.println(Math.abs(-2.5));
		// 向上取整
		System.out.println(Math.ceil(15.00000001));
		// 向下取整
		System.out.println(Math.floor(16.999999));
		// 最大值
		System.out.println(Math.max(16.5, 13.5));
		// 最小值
		System.out.println(Math.min(16.5, 13.5));
		// 平方根 , 开平方
		System.out.println(Math.sqrt(16));
		// 立方根,开立方
		System.out.println(Math.cbrt(8));
		// 随机数 生成一个大于等于0 且 小于 1
		System.out.println(Math.random());
		// 四舍五入 
		System.out.println(Math.round(2.4));
		System.out.println(Math.round(3.5));
		// 负数的 .5 不进位
		System.out.println(Math.round(-2.5));
		
		// 四舍六入五留双
		// 2
		System.out.println(Math.rint(2.4));
		// 3
		System.out.println(Math.rint(2.6));
		// 2
		System.out.println(Math.rint(2.5));
		// 4
		System.out.println(Math.rint(3.5));
		
		// 几次幂  2的4次方
		System.out.println(Math.pow(2, 4));
		System.out.println(Math.PI);
	}

6.1练习

public static void main(String[] args) {
		// 生成a-z
		// 默认是大于等于0且小于1
		System.out.println(Math.random());
		// 大于等于0 且小于10
		System.out.println(Math.random() * 10);
		// 大于等于0 且小于等于10 的整数
		System.out.println(Math.floor(Math.random() * 11));
		// 10~20 整数
		System.out.println(Math.floor(Math.random() * 11 + 10));
		// Math.random()*(最大-最小+1) + 最小
		// a-z
		double result = Math.random() * ('z' - 'a' + 1) + 'a';
		System.out.println((char) result);
	}

7Random

public static void main(String[] args) {
		// 随机数,从0开始
		// 创建一个随机数生成器
		Random random = new Random();
		// 在int范围内,随机生成
		System.out.println(random.nextInt());
		// 可以指定数据,比如100 , 就是在100个数内随机生成(0~99)
		// 如果是100 就是 生成一个 大于等于 0 且 小于100的整数
		System.out.println(random.nextInt(100));
		// 生成一个0~1之间的小数
		System.out.println(random.nextDouble());
	}

7.1练习

public static void main(String[] args) {
		// 需求 生成一个1~10之间的数,可以包含1和10
		Random random = new Random();
		int result = random.nextInt(10) + 1;
		System.out.println(result);
		// 需求 : 生成10~20 , 包含10和20
		// nextInt(最大-最小+1) + 最小;
		result = random.nextInt(11) + 10;
		System.out.println(result);
		// 需求 : 生成 165~321 , 包含165和321
		result = random.nextInt(321 - 165 + 1) + 165;
		System.out.println(result);
		// 需求 : 生成a~z
		result = random.nextInt('z' - 'a' + 1) + 'a';
		System.out.println((char) result);
	}

8 Number

8.1

数字格式化 :
 # 任意数字 , 0-9任意单个数字
 , 千分位
 . 小数点
 0 补位

public static void main(String[] args) {
		// 创建数字格式化对象
		// 需求 : 千分位分割
		DecimalFormat df = new DecimalFormat("###,###");
		System.out.println(df.format(12343356.231));

		// 需求 : 千分位分割,并且保留两位小数
		df = new DecimalFormat("###,###.##");
		System.out.println(df.format(12343356.236));
		// 需求 : 千分位分割,并且保留4位小数,不够补0
		df = new DecimalFormat("###,###.####");
		System.out.println(df.format(12343356.236));
		// 需求 : 千分位分割,并且保留4位小数
		df = new DecimalFormat("###,###.0000");
		System.out.println(df.format(12343356.236));
	}

8.2BigDecimal

public static void main(String[] args) {
		// 精度更高的数据类型
		// 整数更高精度
		BigInteger bi = new BigInteger("22");
		// 小数更高精度
		BigDecimal bd = new BigDecimal(44.3);
		// 进行运算时  不能使用 + - *  /  % ,已经封装成方法
		BigDecimal b1 = new BigDecimal(15);
		BigDecimal b2 = new BigDecimal(5);
		// +
		BigDecimal v1 = b1.add(b2);
		// - 
		BigDecimal v2 = b1.subtract(b2);
		// * 
		BigDecimal v3 = b1.multiply(b2);
		// /
		BigDecimal v4 = b1.divide(b2);
		// %
		BigDecimal v5 = b1.remainder(b2);
		System.out.println(v1);
		System.out.println(v2);
		System.out.println(v3);
		System.out.println(v4);
		System.out.println(v5);
	}

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值