为什么需要包装类(Wrapper Class)
java并不是纯面向对象的语言,java语言是一个面向对象的语言,但是java中的基本数据类型却不是面向对象的,但是我们在实际使用中经常将基本数据类型转换成对象,便于操作,比如,集合的操作中,这时,我们就需要将基本类型数据转化成对象!
包装类和基本数据类型的关系
基本数据类型 包装类
byte Byte
boolean Boolean
short Short
char Character
int Integer
long Long
float Float
double Double
包装类的基本操作
1 public class TestInteger { 2 3 public static void main(String[] args) { 4 // TODO Auto-generated method stub 5 Integer i1=new Integer(123); 6 Integer i2 = new Integer("123"); 7 System.out.println("i1=i2:"+(i1==i2));//false 8 System.out.println("i1.equals(i2):"+i1.equals(i2)); 9 System.out.println(i2); 10 System.out.println(i2.toString());//说明重写了toString方法 11 Integer i3=new Integer(128); 12 System.out.println(i1.compareTo(i3));//-1 13 System.out.println(i1.compareTo(i2));//0 14 System.out.println(i3.compareTo(i2));//1 15 //(1)Integer-->int 包装对象.intValue() 16 int i=i1.intValue(); 17 System.out.println(Integer.max(10, 20));//返回最大值 18 //(2)String -->int 包装类类名.parseInt(String s) 19 int ii=Integer.parseInt("234"); 20 //(3)int -->Integer 21 Integer i4=Integer.valueOf(123); 22 //(4)int-->String 23 String str=ii+""; 24 String s=String .valueOf(ii); 25 //(5)String-->Integer; 26 Integer i5=new Integer("345"); 27 //(6)Integer-->String 28 String ss=i5.toString(); 29 System.out.println(ss); 30 } 31 32 }
4.StringBuilder_StringBuffer 用法_JDK 底
层源码分析---->请见源码查看
4.1 可变的字符串
StringBuilder:效率高,安全性低
StringBuffer:效率低,安全性高
4.2StringBuilder 的常用方法的使用
- append()
- delete()
- insert()
- StringBuilder()构造函数
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package com.bjsxt.stringbuilder;
public class TestStringBuffer {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
// 字符串的追加
sb.append(“hello”);
sb.append(true);
sb.append(‘你’);
sb.append(100);
System.out.println(sb.toString());
sb.delete(3, 5);// 含头不含尾 System.out.println(sb);
sb.deleteCharAt(1);// 删除指定位置上的字符
System.out.println(sb);
sb.insert(2, ‘好’);
System.out.println(sb);
System.out.println(sb.indexOf(“t”) + “\t” + sb.indexOf(“k”));
}
}
package com.bjsxt.stringbuilder; public class TestStringBuilder2 { public static void main(String[] args) { StringBuilder sb=new StringBuilder();//实际上创建了一个长度为16的char类型的数组 StringBuilder sb2=new StringBuilder("hello");//5+16 sb2.append("world"); sb2.append("world"); sb2.append("world"); sb2.append("world"); sb2.append("world"); sb2.append("world"); sb2.append("world"); sb2.append("world"); System.out.println(sb2); System.out.println("容量"+sb2.capacity()+"\tsb2的长度"+sb2.length()); } }
4.3StringBuilder 的 JDK 源码分析
5.不可变字符序列和可变字符序列的深入
javap 是 JDK 自带的一个工具,可以反编译,也可以查看 Java
编译器生成的字节码,是分析代的一个好工具。
-c 对代码进行反汇编
public class Test {
public static void main(String[] args) {
String str1=“abc”+“dbc”;
String str4=“abcdbc”;
//在运行时会在堆中创建对象
/
* StringBuilder sb=new StringBuilder();
* sb.append(“abcdbc”);
* sb.append(“cde”);
* String str2=sb.toString();
*
* */
String str2=str1+“cde”;
String str3=new String(“hello”);
System.out.println(str1==str4);
/*在等号的右侧有变量参与运行或new关键字,将在堆内存开辟空间/
String str6="";
for(int i=0;i<10;i++){
StringBuilder sb2=new StringBuilder();
sb2.append(str6);
sb2.append(i);
str6=sb2.toString();
//str6=str6+i;
}
System.out.println(str6);
/*只创建了一个对象,sb对象/
StringBuilder sb=new StringBuilder();
for(int i=0;i<10;i++){
sb.append(i);
}
System.out.println(sb);
}
}
6.java.util.Date 类
6.1java.util.Date 类
long getTime()
- 返回自 1970 年 1 月 1 日以来,由此 Date 对象表示的00:00:00 GMT 的毫秒数
6.2java.sql.Date 类
6.3java.sql.Date 类
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package com.bjsxt.testdate;
import java.util.Date;
public class TestDate {
public static void main(String[] args) {
Date d=new Date();
System.out.println(System.currentTimeMillis());//1970-1-1 00:00:00
Date d2=new Date(1000L);
System.out.println(d2.toGMTString());
System.out.println(d.getTime());
System.out.println(d.before(d2)); //false
System.out.println(d.after(d2)); //true
System.out.println(d.getTime()<d2.getTime());
System.out.println(d.getTime()>d2.getTime()); //
System.out.println(d2.toString());
/*java.util.Date类的子类/
java.sql.Date sqlDate=new java.sql.Date(1000L);
System.out.println(sqlDate);
java.sql.Time sqlTime=new java.sql.Time(1000L);
System.out.println(sqlTime.toGMTString());
java.sql.Timestamp timestamp=new java.sql.Timestamp(1001L);
System.out.println(timestamp);
}
}
7.DateFormat 和 SimpleDateFormat 类
完成字符串和时间对象的转换
package String; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; public class TestDateFormat { public static void main(String[] args) throws ParseException { // TODO Auto-generated method stub DateFormat d=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); /** * Date-->String format(Date d) * String-->Date parse(String s) */ Date date=new Date(3456465434131346L); System.out.println(date); String strDate=d.format(date); System.out.println(strDate); Scanner input=new Scanner(System.in); System.out.println("请输入一个时间:yyyy-MM-dd hh:mm:ss.SSS"); String str=input.nextLine(); Date d2=d.parse(str); System.out.println(d2); } } package String; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; public class TestDateFormat { public static void main(String[] args) throws ParseException { // TODO Auto-generated method stub DateFormat d=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); /** * Date-->String format(Date d) * String-->Date parse(String s) */ Date date=new Date(3456465434131346L); System.out.println(date); String strDate=d.format(date); System.out.println(strDate); Scanner input=new Scanner(System.in); System.out.println("请输入一个时间:yyyy-MM-dd hh:mm:ss.SSS"); String str=input.nextLine(); Date d2=d.parse(str); System.out.println(d2); } }
8.Calendar 类
8.1Calendar 的类
人们对于时间的认识是:某年某月某日,这样的日期概念。计算机是 long 类型的数字。通过 Calendar 在二者之间搭起桥梁!
GregorianCalendar 是 Calendar 的一个具体子类,提供了世界上大多数国家/地区使用的标准日历系统。
注意:
月份:一月是 0,二月是 1,以此类推,是 12 月是 11
星期:周日是 1,周一是 2,。。。周六是 7
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package com.bjsxt.calendar;
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;
public class Test {
public static void main(String[] args) throws ParseException {
Scanner input=new Scanner(System.in);
System.out.println(“请输入一个日期:yyyy-MM-dd”);
String str=input.next();
//创建DateFormat对象,用于将String转Date
DateFormat df=new SimpleDateFormat(“yyyy-MM-dd”);
Date d=df.parse(str);
//Date对象所表示的时间设置到Calendar中
Calendar cal=new GregorianCalendar();
cal.setTime(d);
//获取输入的日期中的date部分
int nowDate=cal.get(Calendar.DAY_OF_MONTH); //14
//将日期设置为1号,设置成2030-2-1
cal.set(Calendar.DAY_OF_MONTH, 1);
//获取1号是星期几?
int dayOfWeek=cal.get(Calendar.DAY_OF_WEEK);
System.out.println(“日\t一\t二\t三\t四\t五\t六”);
for(int i=1;i<dayOfWeek;i++){
System.out.print("\t");
}
//获取输入的月份的最后一天
int maxDay=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
//System.out.println(maxDay);
for(int i=1;i<=maxDay;i++){
System.out.print(i);
if(i==nowDate){
System.out.print("*");
}
System.out.print("\t");
if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY){
System.out.println();
}
cal.add(Calendar.DAY_OF_MONTH, 1);
}
}
}
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class TestCalendar {
public static void main(String[] args) {
//父类引用指向子类对象
Calendar cal=new GregorianCalendar();
cal.set(2030, 1, 14);
System.out.println(“年”+cal.get(1));
System.out.println(“年:”+cal.get(Calendar.YEAR));
//getTime(),将Calendar转换成Date对象
Date d=cal.getTime();
System.out.println(d);
//setTime(Date) 将Date对象所表示的时间设置到日历中
Date date=new Date(1000L);
cal.setTime(date);
System.out.println(“年:”+cal.get(Calendar.YEAR));
System.out.println(“月:”+cal.get(Calendar.MONTH));
System.out.println(“日:”+cal.get(Calendar.DAY_OF_MONTH));
cal.add(Calendar.DAY_OF_MONTH, 10);
System.out.println(“日:”+cal.get(Calendar.DAY_OF_MONTH));
cal.add(Calendar.DAY_OF_MONTH, -100);
System.out.println(“年:”+cal.get(Calendar.YEAR));
System.out.println(“月:”+cal.get(Calendar.MONTH));
System.out.println(“日:”+cal.get(Calendar.DAY_OF_MONTH));
}
}
9.Math 类
9.1Math 类的常用方法
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package String;
public class TestMath {
public static void main(String[] args) {
System.out.println(“绝对值:”+Math.abs(23)+"\t"+Math.abs(-23)+"\t"+Math.abs(0));
System.out.println(“向上取整,再转double:”+Math.ceil(23.0001)+"\t"+Math.ceil(-9.9999));
System.out.println(“向下取整再转double”+Math.floor(23.9999)+"\t"+Math.floor(-23.0001));
System.out.println(“最值:”+Math.max(20, 10)+"\t"+Math.min(2, 40));
System.out.println(“5的2次方:”+Math.pow(5, 2)+“5的立方”+Math.pow(5, 3));
System.out.println(“随机数[0,1):”+Math.random());
int ran=(int)(Math.random()*9000)+1000;
System.out.println(“1000-9999之间的随机数:”+ran);
System.out.println(“四舍五入:”+Math.round(34.566)+"\t"+Math.round(34.345));
System.out.println(“开方:”+Math.sqrt(55));
}
}
9.2 静态导入
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package String; import static java.lang.Math.*;//静态导入 public class TestStaticImport { public static void main(String[] args) { System.out.println(abs(-20)); System.out.println(Math.abs(-2)); System.out.println(random()); } public static int abs(int number){ return number; } }
JDK1.5
10.File 类
文件和目录路径名的抽象表示形式。一个 File 对象可以代表一个文件或目录
- 可以实现获取文件和目录属性等功能
- 可以实现对文件和目录的创建、删除等功能
常用方法有:
- createNewFile() //有则不创建 返回false,没有则创建 返回true
- delete() //删除成功 返回true ,失败返回false
- getAbsoluteFile() //返回绝对路径
- getPath() //返回相对路径
- getName() //返回文件名
- isFile() //是否为文件 boolean
- length //长度
- mkdir() //创建单层目录
- mkdirs() //创建多级目录
- delete() //只能删除空目录
- list() //返回一个字符串数组,命名由此抽象路径名表示的目录中的文件和目录。
- listFiles() //返回一个抽象路径名数组,表示由该抽象路径名表示的目录中的文件和目录。
File 不能访问文件内容
10.1File 类操作文件
10.2File 类操作目录
package dead;
import java.io.File;
import java.io.IOException;
public class TestFile {
//使用file类去操纵文件
public static void main(String[] args) throws IOException {
//创建File类的文件
File f1=new File(“D:/a1.txt”);//绝对路径
File f2=new File(“D:\a2.txt”);
File f3=new File(“a.txt”);//相对路径
File f5=new File(“D:\test”);//目录
File f4=new File(f5,“a4.txt”);
File f6=new File(“D:”+File.separator+“a5.txt”);
/**
* File操作文件的相关方法
*/
//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); //相对路径
System.out.println(“是否是文件”+f3.isFile());
System.out.println(“是否是文件”+f5.isFile());
System.out.println(“文件中内容的长度”+f3.length());
}
}
package com.bjsxt.file;
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 s:strFile){
System.out.println(s);
}
System.out.println("\n--------------------------------\n");
File [] flist=f3.listFiles();
for(File file:flist){
System.out.println(file);
}
}
}
View Code
11.使用递归算法遍历目录结构和树结构
编写一个程序:以树状结构展现特定的文件夹及其子文件
(夹)(使用递归来做)
package Testfile;
import java.io.File;
public class TestFile {
public static void main(String[] args) {
File f=new File(“E:\eclipse”);
printFile(f,0);
}
public static void printFile(File file,int level){
//打印树状结构的层次关系
for(int i=0;i<level;i++){
System.out.print("-");
}
//输出目录或者文件的名称
System.out.println(file.getName());
if(file.isDirectory()){//判断file对象是否是目录
File[] listFiles = file.listFiles();
for(File temp:listFiles){
//自己调用自己
printFile(temp,level+1);
}
}
}
}
12.枚举
12.1 枚举的定义
只能够取特定值中的一个
使用 enum 关键字
12.2 枚举的使用
而每个被枚举的成员实质就是一个枚举类型的实例,他们默认都是 public static final 的。可以直接通过枚举类型名直接使用它们。)
强烈建议当你需要定义一组常量时,使用枚举类型尽量不要使用枚举的高级特性,事实上高级特性都可以使用普通类来实现,没有必要引入复杂性!
12.3 枚举与类的关系
所有的枚举类型隐性地继承自 java.lang.Enum。(枚举实质上还是类!)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package com.bjsxt.enumpro;
public enum Gender {
男,女; //男,女 public static final
private String name;
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package com.bjsxt.enumpro; /**枚举在自定义类中的使用*/ public class Person { private String name;//姓名 private int age;//年龄 private Gender gender;//性别 public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Gender getGender() { return gender; } public void setGender(Gender gender) { this.gender = gender; } public Person(String name, int age, Gender gender) { super(); this.name = name; this.age = age; this.gender = gender; } public Person() {
package com.bjsxt.enumpro;
public class TestGender {
public static void main(String[] args) {
Gender sex=Gender.女;
//
//Gender g=new Gender();//错误的,因为枚举不是类,不能有构造方法
sex.setName(“张三”);
//System.out.println(sex.getName());
/枚举与String类型之间的转换*/
String str=sex.toString();
System.out.println(str);
/枚举–>String toString()方法
* String -->枚举/
Gender g=Enum.valueOf(Gender.class, “男”);
System.out.println(g);
/**枚举在switch中的使用/
switch(g){
case 男:
System.out.println(“男----------”);
break;
case 女:
System.out.println(“女---------------”);
break;
}
/**枚举在自定义类中的使用*/
Person p=new Person(“marry”, 20, Gender.女);
}
}