1. StringBuffer 类
/* * StringBuffer 是一个存储字符的容器 * StringBuffer 底层是依赖了一个字符数组才能存储字符数据的。该字符串数组默认的初始容量式16,如果字符数组的长度不够使用,自动增加1倍。 * * 容器的具备行为 * * 增加: append(boolean b) 任意类型的数据 * insert(int offset, char c) * * * 删除 delete(int start, int end) * deleteCharAt(int index) * * * 修改 : reverse() * replace(int start, int end, String str) * setCharAt(int index, char ch) * * 查看 capacity() * * StringBuilder 线程不太安全,但是推荐 * * * */ public class test_1{ public static void main(String args[]){ // 先使用StringBuffer 的构造函数创建一个字符串缓冲类 StringBuffer sb = new StringBuffer(); sb.append("abc"); sb.insert(2, "xiaoming"); sb.reverse(); System.out.println("huanchong: " + sb); } }
2. System 类
import java.lang.reflect.Array; import java.util.Arrays; import java.util.Properties; /* * 在 System 类提供的设施中,有标准输入、标准输出和错误输出流; * 对外部定义的属性和环境变量的访问;加载文件和库的方法; * 还有快速复制数组的一部分的实用方法。 * * arraycopy(Object src, int srcPos, Object dest, int destPos, int length) * 从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。 * * currentTimeMillis() 返回以毫秒为单位的当前时间。 * * exit(int status) 终止当前正在运行的 Java 虚拟机 * 非 0 的状态码表示异常终止。 * 在 try 里面 System.exit(0); // jvm 退出 * 在catch 里面 System.exit(1); * * * gc() 建议 JVM 运行垃圾回收器。 * finalize() 如果一个对象被垃圾回收器回收的时候,会先调用对象 finalize()方法 * * getenv(String name) 根据环境变量的名字,获取指定的环境变量值 * * getProperties(key) * 确定当前的系统属性。 * * currentTimeMillis() 获取当前系统 返回以毫秒为单位的当前时间 * * */ class Person{ String name; /* * 快捷键 shift+alt+s * 然后选择Generate Constructors from Superclass * 或者Generate Constructors using Fields, * 分别构造空参和带参数的构造函数 * */ public Person(String name) { this.name = name; } @Override protected void finalize() throws Throwable { super.finalize(); System.out.println(this.name + " 被回收了"); } } public class test_1{ public static void main(String args[]){ int[] srcArr = {10,12,14,16,19}; int[] destArr = new int[4]; System.arraycopy(srcArr, 2, destArr, 1, 2); // System.exit(0); //jvm 退出 System.out.println("目标数组的元素: " + Arrays.toString(destArr)); System.out.println("环境变量: " + System.getenv("JAVA_HOME")); Properties prop= System.getProperties(); prop.list(System.out); String value = System.getProperty("os.name"); System.out.println("当前系统 " + value); for(int i=0; i<4; i++) { new Person("fzx " +i); System.gc(); } } }
3. Runtime
import java.io.IOException; /* * 类 Runtime 该类主要代表了应用程序的环境 * * 每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接。 * 可以通过 getRuntime 方法获取当前运行时。 * * 应用程序不能创建自己的 Runtime 类实例。 * * exec(String command) 在单独的进程中执行指定的字符串命令。 * * freeMemory() 返回 Java 虚拟机中的空闲内存量,是以字节为单位 * * maxMemory() 返回 Java 虚拟机试图使用的最大内存量。 * * totalMemory() 返回 Java 虚拟机中的内存总量 * * */ public class test_1{ public static void main(String args[]) throws Exception{ Runtime timet =Runtime.getRuntime(); // Process process = timet.exec("G:\\java\\Runtime类.exe"); // Thread.sleep(3000); //让当前程序停止3秒 // process.destroy(); System.out.println("空闲内存量 " + timet.freeMemory() ); System.out.println("试图使用的最大内存量 " + timet.maxMemory()); System.out.println("虚拟机中的内存总量 " + timet.totalMemory()); } }
运行结果:
空闲内存量 265832912 试图使用的最大内存量 4273995776 虚拟机中的内存总量 268435456
4. 日期
import java.text.Format; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.Year; import java.util.Calendar; import java.util.Date; /* * 类 Date java.util.Date * * Calendar 类 ,但是构造函数是 protected 不能直接创建对象 * 因此 class 之后要 extends Calendar * * 如果不继承class 不继承这个类, 则需 用静态方法获取这个类 * Calendar 提供了一个类方法 getInstance,以获得此类型的一个通用的对象。 * Calendar 的 getInstance 方法返回一个 Calendar 对象, * 其日历字段已由当前日期和时间初始化: * * 日期格式化类 SimpleDateFormat * 作用1: 可以把日期转换成指定格式的字符串 format(Date date, StringBuffer toAppendTo, FieldPosition pos) * 将给定的 Date 格式化为日期/时间字符串,并将结果添加到给定的 StringBuffer * 作用2: parse(String text, ParsePosition pos) * 解析字符串的文本,生成 Date。 * */ public class test_1{ public static void main(String args[]) throws Exception { // Date date = new Date(); Calendar calendar = Calendar.getInstance(); // 使用默认时区和语言环境获得一个日历。 System.out.println("年 : " + calendar.get(calendar.YEAR)); System.out.println("月 : " + (calendar.get(calendar.MONTH)+1)); System.out.println("日 : " + calendar.get(calendar.DATE)); System.out.println("时 : " + calendar.get(calendar.HOUR_OF_DAY)); System.out.println("分 : " + calendar.get(calendar.MINUTE)); System.out.println("秒 : " + calendar.get(calendar.SECOND)); Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); String time = dateFormat.format(date); System.out.println("当前系统时间 " + time); String birthday = "1995年10月29日 06:06:06"; // 这个字符串需要和SimpleDateFormat格式一致 Date date2 = dateFormat.parse(birthday); System.out.println("fzx的阴历生日是: " + date2); } }
运行结果:
年 : 2018 月 : 4 日 : 28 时 : 22 分 : 18 秒 : 41 当前系统时间 2018年04月28日 22:18:41 fzx的阴历生日是: Sun Oct 29 06:06:06 CST 1995
5. Math 类 和 random 类,验证码
import java.util.Random; /* * Math 数学类 * abs() 绝对值 * ceil(double a) 向上取整 * floor(double a) 向下取整 * round(float a) 返回最接近参数的 int。 四舍五入 * random()返回带正号的 double 值,该值大于等于 0.0 且小于 1.0。 * 返回值是一个伪随机选择的数,在该范围内(近似)均匀分布。 * * 随机数类 Random * */ public class test_1{ public static void main(String args[]) { Random random = new Random(); int rand = random.nextInt(10); System.out.println("随机数: " + rand); // 产生的随机数在 0--10之间 char[] arr = {'付', '祖', '贤', '女', '侠', '博', '客'}; StringBuilder hh = new StringBuilder(); for(int i =0; i<4; i++) { int index = random.nextInt(arr.length); hh.append(arr[index]); } System.out.println("验证码: " + hh); } }运行结果:
随机数: 7 验证码: 贤贤客侠
6. Thread 类
/* * 进程: 正在执行的程序作为一个进程,进程负责内存空间的划分 * 单核的CPU在一个时间只能执行一个应用程序,各个应用程序在抢CPU资源 * * 线程 Thread : * * 任何一个java程序,jvm在运行的时候都会创建一个main线程执行main方法中所有的代码 * * 一个java 应用程序至少有 2 个线程 * jvm 创建的 一个 主线程 是负责 main 方法代码的执行,一个是垃圾回收器线程 * * 创建新执行线程有两种方法。 * 一种方法是 * 1. 将类声明为 Thread 的子类。 * 2. 该子类应重写 Thread 类的 run 方法。? * 目的: 自定义线程的任务代码就写在run方法中,自定义线程负责了 run 方法 * * 3. 接下来可以分配并启动该子类的实例,创建Tread子类对象,并且调用 start 方法开启线程 * 一个线程一旦开启,那么线程会执行 run 方法中的代码, * run 方法千万不能直接调用,直接调用run方法 就相当于调用一个普通方法而已,并没有开启新线程 * * */ public class test_1 extends Thread { /* * 右键source --> Override method * */ @Override public void run() { for(int i=0;i<10; i++) { System.out.println("自定义的线程 : " + i); } } public static void main(String args[]) { test_1 hh= new test_1(); // hh.run(); hh.start(); for(int i=0;i<10; i++) { System.out.println("main 线程 : " + i); } } }
运行结果:
main 线程 : 0 自定义的线程 : 0 main 线程 : 1 自定义的线程 : 1 main 线程 : 2 自定义的线程 : 2 main 线程 : 3 自定义的线程 : 3 main 线程 : 4 自定义的线程 : 4 main 线程 : 5 自定义的线程 : 5 main 线程 : 6 自定义的线程 : 6 main 线程 : 7 自定义的线程 : 7 main 线程 : 8 自定义的线程 : 8 main 线程 : 9 自定义的线程 : 9
7. 线程常用的方法
package test.test_1; /* * Thread(String name) 初始化线程的名字 getName() 返回线程的名字 setName(String name) 设置线程对象名 sleep() 线程睡眠指定的毫秒数。 getPriority() 返回当前线程对象的优先级 默认线程的优先级是5 setPriority(int newPriority) 设置线程的优先级 虽然设置了线程的优先级,但是具体的实现取决于底层的操作系统的实现(最大的优先级是10 ,最小的1 , 默认是5)。 currentThread() 静态方法,哪个线程执行就返回这个线程的对象 返回CPU正在执行的线程的对象 * */ public class test_1 extends Thread { public test_1 (String name) { super(name); } @Override public void run() { // System.out.println("this: " + this); // System.out.println("当前对象: " + Thread.currentThread()); for(int i=0;i<5;i++) { System.out.println(this.getName()+ ": " + i); // 为什么在这里不能抛出异常,只能捕获?? 子类抛出异常必须小于或等于父类异常 /* try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } */ } } public static void main(String args[]) throws Exception { // test_1 hh = new test_1(); // System.out.println("线程名称: " + hh.getName()); test_1 hhhh = new test_1("付祖贤"); // System.out.println("线程名称2: " + hhhh.getName()); // hhhh.sleep(1000); // 静态方法,是主线程在睡眠,因为这句代码是主线程执行 hhhh.setName(" 女侠 "); hhhh.setPriority(10); // 优先级的数字越大,优先级越高,优先级的范围为: 1~10 hhhh.start(); for(int i=0;i<5;i++) { System.out.println(Thread.currentThread().getPriority()+ ": " + i); } Thread mainthread = Thread.currentThread(); System.out.println("main: " + mainthread.getName()); // 线程的优先级默认为5 System.out.println("自定义的线程优先级: " + hhhh.getPriority()); System.out.println("主线程优先级: " + Thread.currentThread().getPriority()); } }
运行结果:
5: 0 5: 1 女侠 : 0 5: 2 女侠 : 1 5: 3 女侠 : 2 5: 4 女侠 : 3 main: main 女侠 : 4 自定义的线程优先级: 10 主线程优先级: 5