api
- api文档,对jdk的方法签名和注解,对jdk的使用说明
- 不熟悉的类可以查看api来查找其可以使用方法
- 最常使用的类,另讨论
包装类
- 包装类:把基本数据类型包装为对象类型,此过程称为装箱
- 手工装箱,Integer obj1 = new Integer(n);
- 自动装箱,Integer obj3 = 200; //缺省写法,编译器自动补齐代码
- 自动装箱,首先调用的时valueof()方法求其值
- 自动装箱,若数值在[-128,127],则会返回缓冲好的对象,返回同数值时时同一个对象
- 自动装箱,若数值在上面之外的则会new一个Integer对象
- 把对象中的基本值取出来,称为拆箱
- int a = obj1.intValue(); //手动拆箱
- int b = obj2; //自动拆箱,编译器补齐代码
- int c = obj1 + obj2; //自动拆箱
- Integer类:decode();//解码,把字符串解码成数字;
- String.valueof(n);//把任意的类型转成字符串
//例
@Test //可直接运行注解
public void name() { //方法名
String string = "123"; //字符串1
String string2 = "234"; //字符串2
int a = Integer.parseInt(string); //String 转int
Integer integer = a; //自动装箱
Integer integer2 = Integer.parseInt(string2); //转换int之后自动装箱
int sum = integer.intValue() + integer2.intValue(); //手动拆箱求和
int product = integer * integer2; //自动拆箱求积
System.out.println(string + " + " + string2 + " = " + sum); //打印输出
System.out.println(string + " * " + string2 + " = " + product); //打印输出
}
基本数据类型 | 包装类 |
---|
boolean | Boolean |
byte | Byte |
short | Short |
int | Integer |
long | Long |
char | Character |
float | Float |
doble | Double |
JUnit4库
- @Test,修饰方法,光标右键菜单点run可以直接运行
- 注意导入包
Date类及其相关类
- 日期,时间处理类,jdk7及之前使用,有很大的诟病,比较麻烦
Long millis = System.currentTimeMillis();//当前相对时间,从1970年首日零点开始计算(计算机诞生) - java8由LocalDate及其相关类代替
Calendar类
- 日历类
- 使用不方便,后面受使用LocalDate代替
SimpleDateDteFormat
- String SimpleDateDteFormat(“yyyy-MM-dd HH:mm:ss”);//格式日期工具类字母不可乱写
- Date() 无参,当前时间
jdk8,日期的处理方式
- LocalDate类
- LocalTime类
- LocalDateTime类
- 和string类似,不会修改原来的,只会产生新的
- 不再常量区
Math类
BigInteger类
- 大整数类,可存储所有的整数,无限制
- 和string类似,不会修改原来的数据,只会产生新的
- BigInteger(String str);
BigDecimal类
public class DateTest {
@Test //clendar类,日历类,包含诸多信息,保留数组中,可以通过方法来调用
public void testName2() throws Exception {
Calendar calendar = Calendar.getInstance();
System.out.println(calendar);
//calendar.getYear();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH); // 月份数据比实际小1
int day = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(year);
System.out.println(month);
System.out.println(day);
//calendar.setYear(2008);
calendar.set(Calendar.YEAR, 2008);
calendar.set(Calendar.MONTH, 7);
calendar.set(Calendar.DAY_OF_MONTH, 10);
System.out.println(calendar.getTime());
calendar.add(Calendar.YEAR, 11);
calendar.add(Calendar.MONTH, 4);
calendar.add(Calendar.DAY_OF_MONTH, -500); // 向前走500天
System.out.println(calendar.getTime());
// 获取日历对象, 把它变成你的生日 , 推算你的百岁是哪天.
//add ,set ,get,get方法需要传入之中的常量来寻找
}
@Test //Date类和SimpleDateFormat类,Date类可以获取当前时间util,simple来格式化date类,根据提前传入的字符串,
public void testName() throws Exception {
long millis = System.currentTimeMillis(); // 当前时间毫秒数
System.out.println(millis);
Date date = new Date(); // 当前时间对象
System.out.println(date);
// 日期格式化器, y表示年, M表示月, d表示日, H表示小时, m表示分钟, s表示秒
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String string = dateFormat.format(date); // 把日期对象格式化成相应的字符串
System.out.println(string);
String string2 = "1998-03-22 15:22:30";
Date parse = dateFormat.parse(string2); // 把符合格式的日期字符串解析成日期对象, 字符串要严格匹配模式字符串
System.out.println(parse);
Date date2 = new Date(2008, 8, 10); // 这个构造器很烂, 简直没法用, 因为年会多1900, 月会多1
System.out.println(date2);
String format = dateFormat.format(millis); // 还可以格式化毫秒, 很方便
System.out.println(format);
// 写一个小时钟
}
}
class Simple {
@Override //调用函数来体现gc函数的执行,执行打印,由虚拟机来调用
protected void finalize() throws Throwable {
System.out.println("我要死了.....");
}
}
public class SystemTest {
@Test //测试gc回收器,
public void testName2() throws Exception {
Simple simple = new Simple();
simple = null;
System.gc(); // gc()方法不是马上执行, 而是交由另外的后台线程去执行.
}
@Test //测试finlly和return以及exit()函数的作用效果和优先性
public void test1() {
System.out.println("begin");
try {
if (true) {
//return;
System.exit(0); // 终止JVM
}
} finally {
System.out.println("finally");
}
System.out.println("end");
}
@Test //测试currentTimeMillis()函数和System类
public void testName() throws Exception {
long currentTimeMillis = System.currentTimeMillis();
System.out.println(currentTimeMillis);
}
}