一、String类
1、常用的属性
2、常用方法
3、方法对应例子
public class SystemDemo {
public static void main(String[] args) {
int[] arr1 = {5,6,7,0,6,6,1,2,3};
int[] arr2 = {12,14,67};
/**
* 第一个参数:要拷贝数组
* 第二个参数:要拷贝的数组的起始索引
* 第三个参数:目标数组
* 第四个参数:目标数组的索引起始位置
* 第五个参数:要拷贝的长度
*/
System.arraycopy(arr2, 1, arr1, 3, 2);
for(int i = 0; i < arr1.length; i++){
System.out.print(arr1[i]+"\t");
}
System.out.println();
//获得当前时间的毫秒数,从1970年到现在的毫秒数
long tm = System.currentTimeMillis();
System.out.println(tm);
long startTime = System.currentTimeMillis();
//运行的程序
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
System.out.println("程序的运行时间是:"+(endTime - startTime)+"毫秒");
//退出jvm
//System.exit(0);
System.out.println("------------");
//调用垃圾回收期
System.gc();
String path = System.getProperty("os.name");
System.out.println(path);
}
}
二、包装类
1、把基本数据类型转换成类,我们使用基本数据类型做进制转换很麻烦,对于临界值也不好判断,我们的包装类提供了很多方法供我们使用,这样会方便很多。Integer 类在对象中包装了一个基本类型 int的值。
2、Integer的构造器
2、数据转换
①int或Integer转换string
②String转换int或Integer
注意:8种基本数据类型和其相应的包装类之间可以自动的转换(自动装箱拆箱)
3、自动装箱和拆箱
装箱:把int类型转换正Integer--------------》new Integer(inti)
拆箱:把Integer转换成int------------》intValue()对象方法
装箱和拆箱是不需要我们主动调用的,是jvm自动给我们完成的。
Integer数据类型的默认值:null
int数据类型的默认值:0
建议大家在实际做项目时都使用Integer数据类型
4.例子
package cn.tx.clazz;
public class ClazzDemo2 {
/**
* 面试题:
* int和Integer的区别
* int是基本数据类型,Integer是包装类,包装类中提供很多的对整数的操作的方法,int和Integer之间可以自动装箱和拆箱
* int的默认值是0,Integer的默认值是null
*/
public static void main(String[] args) {
Person p = new Person();
System.out.println("int的默认值:"+p.getId());
System.out.println("Integer的默认值:"+p.getAge());
//对象在使用前要判断是否是空
//Integer i = p.getAge() + 10;
String str = null;
Integer i1 = new Integer(str);
System.out.println(i1);
}
}
public class Person {
private int id;
private Integer age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
三、日期处理
1、Date类
①Date类的构造器
②Date类的方法
2、日期的格式化
②掌握的方法
③String转换日期
④例子
public class SystemDemo {
public static void main(String[] args) {
int[] arr1 = {5,6,7,0,6,6,1,2,3};
int[] arr2 = {12,14,67};
/**
* 第一个参数:要拷贝数组
* 第二个参数:要拷贝的数组的起始索引
* 第三个参数:目标数组
* 第四个参数:目标数组的索引起始位置
* 第五个参数:要拷贝的长度
*/
System.arraycopy(arr2, 1, arr1, 3, 2);
for(int i = 0; i < arr1.length; i++){
System.out.print(arr1[i]+"\t");
}
System.out.println();
//获得当前时间的毫秒数,从1970年到现在的毫秒数
long tm = System.currentTimeMillis();
System.out.println(tm);
long startTime = System.currentTimeMillis();
//运行的程序
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
System.out.println("程序的运行时间是:"+(endTime - startTime)+"毫秒");
//退出jvm
//System.exit(0);
System.out.println("------------");
//调用垃圾回收期
System.gc();
String path = System.getProperty("os.name");
System.out.println(path);
}
}
3、Calendar日历类
①例子
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class CalendarDemo1 {
public static void main(String[] args) {
Calendar ca = Calendar.getInstance();
System.out.println(ca);
//获得年
int year = ca.get(Calendar.YEAR);
//获得月
int month = ca.get(Calendar.MONTH)+1;
//获得日
int day = ca.get(Calendar.DAY_OF_MONTH);
//获得小时
int hour = ca.get(Calendar.HOUR_OF_DAY);
//获得分钟
int minute = ca.get(Calendar.MINUTE);
//获得秒
int second = ca.get(Calendar.SECOND);
//获得毫秒
int ms = ca.get(Calendar.MILLISECOND);
System.out.println(year+"年"+month+"月"+day+"日"+ " "+hour+"时"+minute+"分"+second+"秒"+ms+"毫秒");
//设置日历
ca.set(Calendar.YEAR, 1985);
ca.set(Calendar.MONTH, 03);
ca.set(Calendar.DAY_OF_MONTH, 22);
ca.set(Calendar.HOUR_OF_DAY, 6);
ca.set(Calendar.MINUTE, 23);
ca.set(Calendar.SECOND, 34);
ca.set(Calendar.MILLISECOND, 344);
System.out.println(ca);
//获得日历相关的毫秒数,日历和日期直接的转换
long tm = ca.getTimeInMillis();
Date date = new Date(tm);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String str = sdf.format(date);
System.out.println(str);
System.out.println(date);
}
}
四、小结
1、System.currentTimeMillis()方法时获得当前时间的毫秒数,从1970.1.1到现在的毫秒数。可用于计算函数运行的时间(差值);
2、直接退出java虚拟机JVM的方法是System.exit();
3、在字符串转换成Integer类型时,一定要保证字符串是数值类型,而且要保证不是空字符串;
4、字符串类型于任何类型相加都是字符串类型;
5、基本数据类型转换成包装类的过程叫装箱,包装类转换成基本数据类型的过程叫做拆箱。JDK1.5以后的特性,自动完成;
五、int和Integer的区别
①int是基本数据类型,Integer是包装类,包装类中提供很多堆整数的操作方法,int和Integer之间可以自动装箱和拆箱;
②int的默认值是0,Integer的默认值是null;
③Integer对象在使用前要判断是否为空(对象中不允许为空)。