常用API

1. String

1.1. String概述

String 是字符串类 , 很多语言中String都是基本数据类型 , 但是在Java中是引用类型

String底层是char数组 , 所以String很多特性都是数组的特性 , 比如String有length()的方法 , 用来保存长度

并且字符串一旦确定 , 不能修改

String类型比较 , 一定要用equals方法 , 并且String中已经覆写了equals方法, 比较的是值

String类型的值 , 使用""表示(双引号)

1.2. 基本使用

public class String_01 {
	public static void main(String[] args) {
		String_01 ss = new String_01();
		//直接指向常量池
		String ss1 = "asd";
		//指向堆内存地址
		String b = new String("bbb");
	}
}

1.3. 不要频繁拼接

会创建五个对象 , a,c,d,ac,acd

1.4. 构造方法

public class String_02 {
	public static void main(String[] args)  throws Exception{
		//1 字符数组转字符串
		char[] arr = {'a','b','c'};
		String s1 = new String(arr);
		System.out.println(s1);
		
		//-------------------反射
		Class clz = s1.getClass();
		Field field = clz.getDeclaredField("value");
		field.setAccessible(true);
		char[] arr1 = (char[]) field.get(s1);
		arr1[0] = 'x';
		System.out.println(s1);
		
		//1 无参构造
		s1 = new String();
		s1 = new String("aaaaa");
		
		//3 字节数组转字符串
		byte[] bytes = {97,98,99};
		s1 = new String(bytes);
		System.out.println(s1);
		
		//4 字节数组转字符串 , 从0开始取两个 , 下标0开始
		s1 = new String(bytes,0,2);
		System.out.println(s1);
	}
}

1.5. 常用方法


public class String_03 {
    public static void main(String[] args) {
        // 获取字符串中某索引对应的字符
        String str = "          abcdefg.java         ";
        char c1 = str.charAt(1);
        System.out.println(c1);
        // 去除字符串中两边的空格
        str = str.trim();
        System.out.println(str);
        // 判断是否以指定字符串结尾
        System.out.println(str.endsWith(".java"));
        // 判断是否以指定字符串开头
        System.out.println(str.startsWith("a1"));
        // 转大写
        str = str.toUpperCase();
        // 转小写
        str = str.toLowerCase();
        // 截取字符串 , 获取下标2开始到最后的子字符串(包含2)
        String str1 = str.substring(2);
        System.out.println(str1);
        // 截取字符串,获取下标2开始(包含)到5结束(不包含)的子字符串
        String str2 = str.substring(2, 5);
        System.out.println(str2);
        // 转字符数组
        char[] arr1 = str.toCharArray();
        // 转字节数组
        byte[] arr2 = str.getBytes();
        // 替换 , 如果替换的新字符是空字符串,则意味着把所有a删除
        String str3 = str.replace("a", "");
        str3 = str.replace("a", "1");
        // 加all 则支持正则表达式,当未出现正则表达式语法时,则没有区别
        str3 = str.replaceAll("a", "1");
        System.out.println(str3);
        // 以指定字符串分割为字符串数组(注意,支持正则表达式)
        str = "a,b,c,d,e";
        String[] arr3 = str.split(",");
        for (int i = 0; i < arr3.length; i++) {
            System.out.println(arr3[i]);
        }
        // 不区分大小写比较
        System.out.println("abCDe".equalsIgnoreCase("AbcDe"));

        // valueOf : 调用对象的toString方法,可以解决null值空指针
        Object o = null;
        String str4 = String.valueOf(o);
        System.out.println(str4);

        // indexOf : 查找某字符的起始索引,找不到返回-1
        int index = "asdhasdnakjs".indexOf('a');
        index = "asdhasdnakjs".indexOf("sadas");
        // 97 = a
        index = "asdhasdnakjs".indexOf(97);
        System.out.println(index);
        index = "asdhasdnakjs".lastIndexOf(97);
        System.out.println(index);
    }
}

1.6. 注意事项

字面量字符串相加时 , 会自动吧+去掉 "a"+"b" = "ab"

public class String_04 {
	public static void main(String[] args) {
		String a = "a";
		String b = "b";
		String c = "ab";
		String d = new String("ab");
		String e = "a"+"b";
		String f = "a"+b;
		String g = a+b;
		//不相等 , 因为d是new的 , 所以必须用equals判断相等
		System.out.println(c==d);
		//相等
		System.out.println(c==e);
		//不相等
		System.out.println(c==f);
		System.out.println(c==g);
		//因为两个变量相加 , 编译后会使用String.valueOf进行判断
		//如果是null则会赋值为字符串"null"所以此时相加得  nullb
		a = null;
		g = a+b;
		System.out.println(g);
	}
}

2. StringBuffer和StringBuilder

2.1. 概述

StringBuffer 和 StringBuilder 都是可拼接的字符串

当我们需要频繁拼接字符串时,优先使用这两个,而不是String

StringBuffer : 线程安全 , 多线程环境下可能出问题

StringBuilder : 非线程安全 , 多线程环境下可能出现问题

默认容量是16 , 扩容为2倍+2

public class String_05 {
	public static void main(String[] args) {
		StringBuilder sb1 = new StringBuilder();
		StringBuffer sb2 = new StringBuffer();
		//进行字符串拼接
		sb1.append("a");
		//因为返回的是this , 所以可以进行链式调用
		sb1.append("b").append("f").append(11);
		sb1.append("b").append("f").append(11);		
		sb1.append("b").append("f").append(11);		
		sb1.append("b").append("f").append(11);
		
		//反转
		sb1.reverse();
		//转换为String
		String str = sb1.toString();
		//数组容量
		System.out.println(sb1.capacity());
		//已经有的元素个数
		System.out.println(sb1.length());
		
	}
}

3. 包装类

3.1. 概述

就是将基本数据类型封装成对象。因为在对象描述中就可以定义更多的属性和行为对该基本数据类型进行操作。我们不需要自己去对基本类型进行封装,JDK已经为我们封装好了。

3.2. 包装类的使用

public class Integer_01 {
	public static void main(String[] args) {
		//基本数据类型
		int i =1;
		//包装类类型(引用类型)
		Integer i2 = new Integer(11);
		byte b =2;
		Byte b2 = new Byte(b);
		Long l = new Long(222l);
		System.out.println(i2);
		
	}
}

3.3. Integer

3.3.1. Integer的基本使用

3.3.2. 相互转换

public class Integer_02 {
	public static void main(String[] args) {
	
		int i1 = 22;
		Integer i2 = new Integer(33);
		//1 int ---> Integer
		i2 = new Integer(i1);
		i2 = Integer.valueOf(i1);
		
		//2 Integer -- > int
		i1 = i2.intValue();
	}
}

3.3.3. 常用方法

		// 用于把包装类类型转换为int类型
		i1 = i2.intValue();
		// 用来把int转换为包装类类型
		i2 = Integer.valueOf(i1);
		// 也可以把纯数字字符串转换为Integer类型
		i2 = Integer.valueOf("23");
		// 把纯数字的字符串,转换为int类型
		i1 = Integer.parseInt("465");
		Double.parseDouble("23.2");
		Long.parseLong("21312");
		Double.valueOf(22);

3.3.4. 自动装箱和自动拆箱

	public static void main(String[] args) {
		//int类型的值 可以直接赋值给Integer类型 自动装箱
		Integer i1 = 11;
		//编译后  ,就等于Integer i1= Integer.valueOf(11);
		//Integer类型的值 可以直接赋值给int类型 自动拆箱
		int i2 = i1;
		//编译后 相当于int i2 = i1.intValue();
		
		
		
//此时,i2是基本类型 , 但是参数需要Object类型 , 
        //而int 无法转换成Object类型
//所以 i2会先进行自动装箱为Integer类型 , 
        //然后在发生向上转型(多态)为Object类型
//所以 基本类型 也可以赋值给Object类型 , 
        //即使因为会发生自动装箱
		
		
		m1(i2);
		
		
	}
	public static void m1(Object obj) {
		System.out.println(obj);
	}
}

3.3.5. 深入整型常量池

3.3.6. 总结

总结来说 : 常量池就是提前准备好一些对象 , 当我们要保存数据时 , 发现已经 创建好了 , 就直接拿走 , 不需要重新创建

总结 :

        当我们通过 new Integer(xx)创建对象的时候 , 不管值是多少 互相用==比较永远都是false

当我们使用 Integer i1 = xxx 这种方式的时候 , 编译之后 会转换成Integer.valueOf(); 这种方式会经过整型常量池

假如 这个值在-128到127之间 则不需要new对象 而是使用常量池中已经创建好的对象

否则 不在范围中 , 这还是会new Integer(xxx);这种方式

因此

        Integer i1 = 101;

        Integer i1 = 201; 201不在-128到127之间 则i1 == i2是false

注意 Integer类型之间的比较 还是要用equals进行比较才最好

4. System

4.1. 概述

System类代表系统,系统级的很多属性和控制方法都放置在该类的内部。 该类位于java.lang包。

由于该类的构造器是private的,所以无法创建该类的对象,也就是无法实 例化该类。其内部的成员变量和成员方法都是static的,所以也可以很方便 的进行调用。

成员变量

System类内部包含in、out和err三个成员变量,分别代表标准输入流

(键盘输入),标准输出流(显示器)和标准错误输出流(显示器)。

成员方法

native long currentTimeMillis():

该方法的作用是返回当前的计算机时间,时间的表达格式为当前计算机时

间和GMT时间(格林威治时间)1970年1月1号0时0分0秒所差的毫秒数。

void exit(int status):

该方法的作用是退出程序。其中status的值为0代表正常退出,非零代表

异常退出。使用该方法可以在图形界面编程中实现程序的退出功能等。

4.2. 常用方法

public class System_01 {
	public static void main(String[] args) {
		//获取当前时间对应的毫秒数(1970.1.1 8点到现在的毫秒数)
		long startTime = System.currentTimeMillis();
		System.out.println(startTime);
		//建议垃圾回收
		System.gc();
		//JVM关机
		System.exit(0);
		//不会执行
		System.out.println(111);
	}
}

5. Date

5.1. 概述

获取时间和对时间操作

5.2. 构造方法

package _04_Date;

import java.util.Date;

public class Date_01 {
	public static void main(String[] args) {
		//创建当前系统时间对象
		//注意导包要导入util的包
		Date date = new Date();
		//时间原点到指定毫秒数的时间(1970.1.1 8:00:00)
		date = new Date(1000);
		System.out.println(date);
		
		//获取当前系统时间的前十分钟
		date = new Date(System.currentTimeMillis() - 1000*60*10);
		System.out.println(date);
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值