基本用法
为什么需要包装类
Java是面向对象的语言,但并不是“纯面向对象”的,因为我们经常用到的基本数据类型就不是对象。但是我们在实际应用中经常需要将基本数据转化成对象,以便于操作。比如:将基本数据类型存储到Object[]数组或集合中的操作等等。
为了解决这个不足,Java在设计类时为每个基本数据类型设计了一个对应的类进行代表,这样八个和基本数据类型对应的类统称为包装类(Wrapper Class)。
包装类和基本数据类型的对应关系
基本数据类型 | 包装类 |
---|---|
byte | Byte |
short | Short |
int |
Integer |
long | Long |
float | Float |
double | Double |
boolean | Boolean |
char | Character |
在这八个类名中,除了Integer和Character类以外,其它六个类的类名和基本数据类型一致,只是类名的第一个字母大写而已。
包装类中的继承关系
基本转换
Int——Integer new Integer(primitive)
Integer——Int Integer对象.xxxValue()
Integer——String Integer对象.toString()
String——Integer new Integer(Sting str)
int——String String.valueOf(primitive)
String——int Integer.prseXxx()
package cn.xjion.pro08;
/**
* 测试数据之间的转换
* @author xjion
*/
public class TestInteger {
public static void main(String[] args) {
// 创建两个包装类对象
Integer i1 = new Integer(123);
Integer i2 = new Integer("123");
// 打印
System.out.println("i1==i2吗:"+(i1==i2));//false
System.out.println("i1.equals(i2)等吗:"+i1.equals(i2));//true
// 重写toString方法
System.out.println(i2.toString());
// 创建包装类对象i3
Integer i3 = new Integer(128);
// 测试包装类的转换比较
System.out.println(i3.compareTo(i1));//大于是1
System.out.println(i3.compareTo(131));//小于是-1
System.out.println(i3.compareTo(i3));//等于是0
// Integer-->int 包装类对象.intValue()
int i = i1.intValue();
System.out.println(i);//123
System.out.println(Integer.max(10, 20));//20
// int-->Integer
Integer i4 = Integer.valueOf(123);
// String-->int 包装类名.parseInt(String s)
int i5 = Integer.parseInt("234");
// int-->String
String str = i5 +"";
String s = String.valueOf(i5);
// String-->Integer
Integer i6 = new Integer("345");
// String-->Integer
Integer i7 = new Integer("345");
// Integer-->String
String s1 = i7.toString();
System.out.println(s1);//345
}
}
自动拆箱和装箱
自动拆箱 aoto - boxing
基本类型的数据处于需要对象的环境中时,会自动转为“对象”。我们以Integer为例:在JDK1.5以前,这样的代码 Integer i = 5 是错误的,必须要通过Integer i = new Integer(5) 这样的语句来实现基本数据类型转换成包装类的过程;而在JDK1.5以后,Java提供了自动装箱的功能,因此只需Integer i = 5这样的语句就能实现基本数据类型转换成包装类,这是因为JVM为我们执行了Integer i = Integer.valueOf(5)这样的操作,这就是Java的自动装箱。
package cn.xjion.pro08;
public class TestAutoBoxing {
public static void main(String[] args) {
Integer a = 100;//Integer a = Integer.valueOf(100);
Integer b = 100;
System.out.println("a==b:"+(a==b));//true,调用的是同一个对象
Integer aa = 1000;
Integer bb = 1000;
System.out.println("aa==bb"+(aa==bb));//false
}
}
自动拆箱unboxing
1.每当需要一个值时,对象会自动转成基本数据类型,没必要再去显式调用intValue()、doubleValue()等转型方法。
如 Integer i = 5;int j = i; 这样的过程就是自动拆箱。
int a = new Integer(100);
编译器为我们添加了
int a = new Integer(100).intValue();
2. 自动装箱调用的是valueOf()方法,而不是new Integer()方法。
3. 自动拆箱调用的xxxValue()方法。
4. 包装类在自动装箱时为了提高效率,对于-128~127之间的值会进行缓存处理。超过范围后,对象之间不能再使用==进行数值的比较,而是使用equals方法
public class TestUnBoxing {
public static void main(String[] args) {
int a = new Integer(100);
//int a = new Integer(100).intValue();
Integer b = null;
int c = b;//自动拆箱,空指针异常
//int c = b.intValue();//对象为null却调用了对象中的方法
System.out.println(c);
}
}
String类的底层源码分析
1、底层数据结构
String类的底层源码实际上是一个char数组实现的。
String的方法实际上就是对一个数组进行操作。
可变字符串
1. StringBuffer JDK1.0版本提供的类,线程安全,做线程同步检查, 效率较低。
2. StringBuilder JDK1.5版本提供的类,线程不安全,不做线程同步检查,因此效率较高。 建议采用该类。
StringBuilder的使用
package cn.xjion.pro08;
/**
* 测试StringBuilder的使用
* @author xjion
*/
public class TestStringBuilder {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
// 字符串的追加
sb.append("hello");
sb.append(true);
sb.append(100);
sb.append('男');
System.out.println(sb);//hellotrue100男
// 删除下标3到5的字符串,喊头不含尾
sb.delete(3, 5);
System.out.println(sb);//heltrue100男
// 删除指定下标的字符
sb.deleteCharAt(1);
System.out.println(sb);//hltrue100男
// 从下标2开始插入oo
sb.insert(2, "oo");
System.out.println(sb);//hlootrue100男
// 返回t和k的下标,找到就返回下标,没找到就返回-1
System.out.println(sb.indexOf("t")+"\t"+sb.indexOf("k"));
}
}
StringBuilder源码分析
package cn.xjion.pro08;
public class TestStringBuilder2 {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();//实际上创建了一个长度为16的char类型的数组
StringBuilder sb2 = new StringBuilder("hello");//hello是5位加上16位
// 添加字符
sb2.append("world");
sb2.append("world");
sb2.append("world");
sb2.append("world");
sb2.append("world");
System.out.println(sb2);
// 求容量及长度
// 容量不够时,c=c*2+2,容量扩为44,长度30
System.out.println("容量"+sb2.capacity()+"\tsb2的长度"+sb2.length());
}
}
StringBuffer的用法和StringBuilder的用法一样
package cn.xjion.pro08;
public class TestStringBuffer {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();//实际上创建了一个长度为16的char类型的数组
StringBuffer sb2 = new StringBuffer("hello");//hello是5位加上16位
// 添加字符
sb2.append("world");
sb2.append("world");
sb2.append("world");
sb2.append("world");
sb2.append("world");
System.out.println(sb2);
// 求容量及长度
// 容量不够时,c=c*2+2,容量扩为44,长度30
System.out.println("容量"+sb2.capacity()+"\tsb2的长度"+sb2.length());
}
}
不可变字符序列和可变字符序列的深入
javap是JDK自带的一个工具,可以反编译,也可以查看Java编译器生成的字节码,是分析代码的一个好工具
package cn.xjion.pro08;
/**
* 对代码进行反编译
* @author xjion
*/
public class TestString {
public static void main(String[] args) {
// 创建两个字符对象
String str1 = "abc"+"dbc";
String str2 = "abcdbc";
System.out.println(str1==str2);//为同一对象创建,显然为true
// 在运行时会在堆中创建对象
String str = str2 + "cde";
/*
StringBuilder sb = new StringBuilder();
sb.append("abcdbc");
sb.append("cde");
String str = sb.toString();
*/
String str3 = new String("hello");
// 在符号的右侧的变量和运算会在堆中开辟空间
String str4 = "";
for (int i = 0; i < 10; i++) {
str4 = str4 + i;
/*StringBuilder sb2 = new StringBuilder();
sb2.append(str4);
sb2.append(i);
String str11 = sb2.toString();
*/
}
System.out.println(str4);//0123456789
// 只创建了一个对象,sb对象,而不会创建大量无用的对象
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10; i++) {
sb.append(i);
}
System.out.println(sb);
}
}
时间类(Util.Date)
long getTime()
返回从1970年1月 1日到现在的毫秒数。
时间处理的相关类
package cn.xjion.pro08;
import java.util.Date;
/**
* 测试Date的相关类
* @author xjion
*/
public class TestDate {
public static void main(String[] args) {
Date d = new Date();
// 返回当前时间
System.out.println(d);//Tue May 21 11:28:56 CST 2019
System.out.println(d.toString());
// 返回毫秒数
System.out.println(System.currentTimeMillis());//1558409417100
// 传入一个开始时间
Date d2 = new Date(1000L);
System.out.println(d2);//从北京时间算起,Thu Jan 01 08:00:01 CST 1970
System.out.println(d2.toGMTString());//从开始计时算起,1 Jan 1970 00:00:01 GMT
// 返回距1970年1月1日到现在的毫秒数
System.out.println(d.getTime());//1558409665264
// 是否在之前或者之后
System.out.println(d.before(d2));//false
System.out.println(d.after(d2));//true
// 比较
System.out.println(d.getTime()<d2.getTime());//false
System.out.println(d.getTime()>d2.getTime());//true
// java.Util.Date类的子类
// 传入1000ms
java.sql.Date sqlDate = new java.sql.Date(1000L);
// 打印年月日
System.out.println(sqlDate);//1970-01-01
// 返回时分秒
java.sql.Time sqlTime = new java.sql.Time(1000L);
System.out.println(sqlTime);//08:00:01
// 返回GMT时间
System.out.println(sqlTime.toGMTString());//1 Jan 1970 00:00:01 GMT
// 返回毫秒数所在整体时间
java.sql.Timestamp timestamp = new java.sql.Timestamp(1000L);
System.out.println(timestamp);//1970-01-01 08:00:01.0
}
}
DateFormat和SimpleDateFormat类
package cn.xjion.pro08;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class TestDateFommat {
public static void main(String[] args) throws ParseException {
/**Date-->String format(Date d)
* String-->Date parse(String s)
* */
DateFormat d = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS");
Date d2 = new Date(10000L);
System.out.println(d2);
String str = d.format(d2);
System.out.println(str);
Scanner input = new Scanner(System.in);
System.out.println("请输入一个时间:yyyy-MM-dd hh:mm:ss.SSS");
String str2 = input.nextLine();
Date d3 = d.parse(str2);
System.out.println(d3);
}
}
Calendar日历类
人们对于时间的认识是:某年某月某日,这样的日期概念。计算机是long类型的数字。
通过Calendar在二者之间搭起桥梁!
GregorianCalendar 是 Calendar 的一个具体子类,提供了世界上大多数国家/地区使用的标
准日历系统。
• 注意:
• 月份:一月是0,二月是1,以此类推,是12月是11
• 星期:周日是1,周一是2,。。。周六是7
Calendar的常用方法
package cn.xjion.pro08;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* 测试Calendar类的一些常用的方法
* @author xjion
*/
public class TestCalendar {
public static void main(String[] args) {
// 父类引用指向子类对象,提供标准日历系统
Calendar ca = new GregorianCalendar();
// 设置
ca.set(2020, 8,20);
// 打印年
System.out.println(ca.get(Calendar.YEAR));
// getTime(),将calendar转换成Date对象
Date d = ca.getTime();
System.out.println(d);
// setTime(Date) 将Date对象所表示的时间设置到日历中
Date d2 = new Date(10000L);
ca.setTime(d2);
System.out.println("---------------------");
// 获取Calendar的年月日
System.out.println("年:"+ca.get(Calendar.YEAR));
// 月份默认是0,0就是表示的1月,11就是12月
System.out.println("月:"+ca.get(Calendar.MONTH));
System.out.println("日:"+ca.get(Calendar.DAY_OF_MONTH));
// 加10天
ca.add(Calendar.DAY_OF_MONTH, 10);
System.out.println("日:"+ca.get(Calendar.DAY_OF_MONTH));
// 减11天所在的年月日
ca.add(Calendar.DAY_OF_MONTH, -11);
System.out.println("年:"+ca.get(Calendar.YEAR));
System.out.println("月:"+ca.get(Calendar.MONTH));
System.out.println("日:"+ca.get(Calendar.DAY_OF_MONTH));
}
}
使用Calendar类编写万年历
package cn.xjion.pro08;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;
/**
* 制作一个万年历
* @author xjion
*
*/
public class TestPerpeCalendar {
public static void main(String[] args) throws ParseException {
// 获取控制台,输入年月日,获取字符串
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个日期:yyyy-mm-dd");
String str = sc.nextLine();
// 创建DateFormat对象,用于将String转为Date
DateFormat df =new SimpleDateFormat("yyyy-mm-dd");
Date d = df.parse(str);
// Date对象所表示的时间设置到Calendar中
Calendar ca = new GregorianCalendar();
ca.setTime(d);
// 获取输入的日其中的date部分
int nowDate = ca.get(Calendar.DAY_OF_MONTH);
// 将日期设置为1号
ca.set(Calendar.DAY_OF_MONTH, 1);
// 获取1号是周几
int dayOfWeek = ca.get(Calendar.DAY_OF_WEEK);
System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");
// 获取的当天是周几的前面全部是制表符
for (int i = 0; i < dayOfWeek; i++) {
System.out.print("\t");
}
// 获取输入的月份的最后一天
int lastDay = ca.getActualMaximum(Calendar.DAY_OF_MONTH);
// 打印这个月所有的天数
for (int i = 1; i < lastDay; i++) {
System.out.print(i);
// 是当天就打印*
if (i == nowDate) {
System.out.print("*");
}
// 每打印一个就打印一个制表符
System.out.print("\t");
// 如果这一天是周六就换行,+1是因为日历的周天是现实的周一
if (ca.get(Calendar.DAY_OF_WEEK)+1 == Calendar.SATURDAY) {
System.out.println();
}
// 每次循环加1天
ca.add(Calendar.DAY_OF_MONTH, 1);
}
}
}
Math类
package cn.xjion.pro08;
/**
* 测试Math的常用方法
* @author xjion
*/
public class TestMath {
public static void main(String[] args) {
System.out.println("绝对值"+Math.abs(-23));
System.out.println("向上取整再转double:"+Math.ceil(10.01)+"\t"+Math.ceil(-9.9999));
System.out.println("向下取整再转double:"+Math.floor(10.01)+"\t"+Math.floor(-9.9999));
System.out.println("最大值:"+Math.max(10, 20)+"最小值:"+Math.min(15, 25));
System.out.println("5的2次方"+Math.pow(5, 2)+"\t"+"5的立方:"+Math.pow(5, 3));
System.out.println("随机数[0,1):"+Math.random());
// 随机数的范围是int rand = (int)(Math.random()*(max-min+1))+min;
System.out.println("1000-9999之间的随机数:"+((int)(Math.random()*9000)+1000));
System.out.println("四舍五入:"+Math.round(3.45)+"\t"+Math.round(3.51));
System.out.println("开方"+Math.sqrt(4));
}
}
静态导入
package cn.xjion.pro08;
import static java.lang.Math.*;
/**
* 静态导入的作用是:导入后可以直接用方法,不用Math.方法名
* @author xjion
*/
public class TestStaticImport {
public static void main(String[] args) {
System.out.println(abs(-20));
}
}
File类
java.io.File类:代表文件和目录。 在开发中,读取文件、生成文件、删除文件、修改文件的属性时经常会用到本类。
File不能访问文件的内容,可以获取文件和目录的属性。
文件:
package cn.xjion.pro08;
import java.io.File;
import java.io.IOException;
/**
* 测试File类的操作
* @author xjion
*/
public class TestFile {
public static void main(String[] args) throws IOException {
// 创建File类的对象,\需要转义,所以是\\
// 绝对路径
File f1 = new File("D:\\a.txt");
File f2 = new File("D:/a.txt");
// 相对路径
File f3 = new File("a.txt");
// 目录
File f4 = new File("D:\\电影");
// f4下的b.txt
File f5 = new File(f4,"b.txt");
// File.separator就是文件分隔符,会根据需求转义
File f6 = new File("D:"+File.separator+"a.txt");
// File操作文件的相关方法
// 文件是否已创建
System.out.println(f3.createNewFile());
System.out.println(f1.createNewFile());
// 删除文件
System.out.println(f1.delete());
// 文件是否存在
System.out.println(f1.exists());
System.out.println("获取绝对路径:"+f3.getAbsolutePath());
System.out.println("获取相对路径:"+f3.getPath());
System.out.println("获取文件名:"+f3.getName());
System.out.println("是否是文件:"+f3.isFile());
System.out.println("是否是文件:"+f4.isFile());
System.out.println("文件中的字节数:"+f5.length());
}
}
目录:
package cn.xjion.pro08;
import java.io.File;
public class TestDirectory {
public static void main(String[] args) {
//创建File类的对象
File f = new File("D:"+File.separator+"test");
f.mkdir(); //用于创建目录,单层目录
System.out.println("目录是否存在"+f.exists());
System.out.println("是不是目录:"+f.isDirectory());
System.out.println("是不是文件:"+f.isFile());
File f2 = new File("D:\\aa\\bb\\cc");
f2.mkdirs();//用于创建目录,多层目录
f.delete();
File parent = f2.getParentFile(); //获取cc目录的父级目录
System.out.println(parent);
parent.delete();//delete删除目录时,只允许删除空目录
f2.delete();//删除cc
parent.delete();//删除bb
// 遍历文件夹中所有文件
File f3 = new File("D:\\");
String[] strFile = f3.list();
System.out.println(strFile.length);//数组中元素的个数
for(String str:strFile) {
System.out.println(str);
}
System.out.println("------------------------------------");
// 遍历查看文件夹中的所有文件,带有父级目录
File[] flist = f3.listFiles();
for(File file:flist) {
System.out.println(file);
}
}
}
使用递归算法遍历文件的目录结构和树结构
package cn.xjion.pro08;
import java.io.File;
/**
* 使用递归算法遍历文件中所有的文件,包括子文件
* @author xjion
*/
public class TestFile {
public static void main(String[] args) {
// 目录
File f =new File("D:\\");
// 传入参数,调用方法
printFile(f,0);
}
public static void printFile(File file,int level) {
//打印树状结构的层次关系,每一级多一个-分隔
for(int i=0;i<level;i++) {
System.out.print("-");
}
//输出目录或文件的名称
String name = file.getName();
System.out.println(name);
if(file.isDirectory()) {//判断File对象是否是目录
File[] fileName = file.listFiles();
// File是目录就会继续一直往下找
for(File file2:fileName) {
//自己调用自己
printFile(file2,level+1);
}
}
}
}
枚举
只能够取一个特定的值
使用enum关键字
public enum Gender {
男,女;//默认都是public static final
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
枚举的使用
enum Season {
SPRING, SUMMER, AUTUMN, WINDER
}
所有的枚举类型隐性地继承自 java.lang.Enum。枚举实质上还是类!而每个被枚举的成员实质就是一个枚举类型的实例,他们默认都是public static final修饰的。可以直接通过枚举类型名使用它们。
当你需要定义一组常量时,可以使用枚举类型。
尽量不要使用枚举的高级特性,事实上高级特性都可以使用普通类来实现,没有必要引入枚举,增加程序的复杂性!
枚举与类的关系
所有的枚举类型隐形地继承自java.lang.Enum。(枚举实质上还是类!)
package cn.xjion.pro08;
import cn.xjion.pro03.User;;
public class TestGender {
public static void main(String[] args) {
Gender sex = Gender.女;
// Gereder g = new Gender();错误,枚举不是类,不能有构造方法
sex.setName("张三");
System.out.println(sex.getName());
// 枚举-->String
String str = sex.toString();
System.out.println(str);
// String-->枚举
// String str1 = "妖";
// Gender g = Enum.valueOf(Gender.class, str1);抛异常,因为Gender枚举中没有这个属性
Gender g = Enum.valueOf(Gender.class, str);
System.out.println(g);
// 枚举在switch中的使用
switch(g){
case 男:
System.out.println("男");
break;
case 女:
System.out.println("女");
break;
}
// 枚举在自定义类中的使用,需要在自定义类中加上这个属性
Person p = new Person("xjion",Gender.男,20);
}
}