包装类
基本数据类型这个数进行了一个封装,产生了一个类,----包装类
对应关系
基本数据 | 对应的包装类 | 继承关系 |
byte | Byte | Number----->Object |
short | Short | |
int | Integer | |
long | Long | |
float | Float | |
double | Double | |
char | Character | Object |
boolean | Boolean | Object |
为什么使用包装类
- 面向对象的思维,最擅长的就是操纵各种类,假如把基本数据类型编程包装类,以后可以操纵类。
- 数组,装东西的,装基本数据类型和引用数据类型都可以。但是以后我们要学习 集合,装东西的,但是只能装引用数据类型。
Integer
- 使用Integer包装类的时候,不用直接导包
- 层次关系
继承关系:Integer—>Number---->Object
实现接口:直接实现Comparable:当中一定实现了一个方法:compareTo方法–》内部比较器
间接实现自Serializable: - 常用属性
public class Test {
public static void main(String[] args) {
System.out.println("Integer的最大值:"+Integer.MAX_VALUE);
System.out.println("Integer的最小值:"+Integer.MIN_VALUE);
System.out.println("最大值加1:"+(Integer.MAX_VALUE+1));
System.out.println("最小值减1:"+(Integer.MIN_VALUE-1));
}
}
- 构造器
public class Test {
public static void main(String[] args) {
Integer i1=new Integer(123);
Integer i2=new Integer("123");
System.out.println("i1="+i1); //说明Integer类重写了toString()方法,不再显示内地址,而是显示的是属性值
System.out.println("i2="+i2);
System.out.println("i1==i2:"+(i1==i2));//比较内存地址 false
System.out.println("i1.equals(i2):"+i1.equals(i2)); //true,说明Integer类重写Object的equals方法,用于比较两个对象的属性值
Integer i3=new Integer("helloworld");// java.lang.NumberFormatException:
System.out.println("i3="+i3);
}
}
- Integer类的成员方法
a)基本数据类型<–>包装类
基本数据类型------>包装类类型 :构造 方法 Integer (int value)
Integer类的静态方法 valueOf(int value)
包装类类型------>基本数据类型 : 成员方法 intValue();
b)包装类<–>String
包装类---->String :Object类中的toString方法
String类的静态方法valueOf(Object obj)
String -->包装类 :构造方法 Integer(String str)
:Integer类的静态方法 valueOf(String str)throws NumberFormatException
c)基本数据类型<–>String
基本–>String :字符串连接
:String类的静态方法valueOf(int value)
String–>基本: Integer类的静态方法 parseInt(String s)
public class Test2 {
public static void main(String[] args) {
//(1)基本-->包装
Integer i1=new Integer(123); //构造方法
Integer i2= Integer.valueOf(123);//静态方法
//(2)包装-->基本
int i3=i1.intValue(); //包装-->基本
//(3)包装-->String
String s1=i1.toString();
//String类中的valueOf方法,将各种引用数据类型转成String类型
String s2= String.valueOf(i1);
//(4)String -->包装
Integer i4=new Integer("123");
Integer i5=Integer.valueOf("123");
//(5)基本-->String
String s3=i3+""; //字符串连接
//String类的静态方法valueOf(int value);
String s4= String.valueOf(123);
//(6)String --》基本
int i7= Integer.parseInt("123");
}
}
自动拆箱,装箱
JDK1.5的特性
自动装箱 :基本类型直接赋值 给包装类型
自动拆箱 :包装类型直接赋值给基本数据类型
String
-
类的概述
字符串不变; 它们的值在创建后不能被更改。 字符串缓冲区支持可变字符串。 因为String对象是不可变的,它们可以被共享。 -
类的继承关系
-
类的定义
a)String类不允许有子类
b)直接继承Object的,toString(),equals(),hashCode() 有可能被String重写
c)实现了三个接口,每个接口表示一种能力
aa)Serializable表示一种序列化的能力
bb)Comparable表示一种比较大小的能力
cc)CharSequence 字符序列 ,String 类是CharSequence这个接口的实现类
4. 类的构造方法
字符串实际上就是一个char类型的数据,说白了,String 实际上就是对char类型的数组的封装
对字符串的所有操作,实际上都是在操作char类型的数组
public class Client {
public static void main(String[] args) {
String str="helloworld"; //str称为String类型的对象 ,没有使用new,直接在方法区中的常量池
String str2=new String(); //创建一个String类型 的对象,在堆里开了空间的
byte [] buf={97,98,99,100,101,102};
String str3=new String(buf); //abcdef
String str4=new String(buf,3,2); //de
System.out.println(str4);
char [] chBuf={'a','b','c','d'};
String str5=new String(chBuf);
String str6=new String(chBuf,0,3);
System.out.println(str6);
}
}
- 常用方法
public class TestString {
public static void main(String[] args) {
String s="helloworld";
//(1)charAt(int index)和(2) length()字符串中字符的个数
for(int i=0;i<s.length();i++){
System.out.print(s.charAt(i)+"\t");
}
//(3)compareTo(...)比较两个字符串的大小
System.out.println("\n");
System.out.println("apple".compareTo("banana"));//-1 97-98
System.out.println("apple".compareTo("app"));//2 5-3
System.out.println("cat".compareTo("app"));//2 99-97
//(4)concat(String st) 作用相当于+
s= s.concat("java");
System.out.println(s);
//(5)contains(CharSequence cd)多态,接口作方法的形参,可以传入接口的任何实现类
System.out.println(s+"这个字符串中是否包含"+s.contains("java"));
}
}
public class TestString2 {
public static void main(String[] args) {
String str="abc";
String str2=new String("abc");
System.out.println("str==str2:"+(str==str2));
//(6)equals
System.out.println("str.equals(str2):"+str.equals(str2));//比较对应位置上的字符
//(7)getBytes()用于将字符串转成byte类型的数组
String s="helloworld";
byte [] buf=s.getBytes();
for(byte b:buf){
System.out.print(b+"\t");
}
System.out.println("\n"+new String(buf));
//(8)查找字符/字符串在当前字符串中第一次出现的位置
System.out.println("o在"+s+"中第一次出现的位置:"+s.indexOf('o'));
System.out.println("o在"+s+"中第一次出现的位置,从索引为5 的位置开始查:"+s.indexOf('o',5));
//(9)最后一次出现的位置
System.out.println("o在"+s+"中最后出现的位置:"+s.lastIndexOf("o"));
//(10)字符串中字符的个数为0,是空字符串
String ss=""; //有地址,一个字符都没有
System.out.println(ss.length());
String sss=null;//没有地址
// System.out.println(sss.length());//没有对象而调用对象的方法,空指针异常
//(11)替换
s= s.replace("o","中");
System.out.println(s);
//(12)字符串截取
s=s.substring(5,7);//含头不含尾
System.out.println(s);
/** 68554698@qq.com*/
//(13)将字符串转成char类型的数组
char [] ch=s.toCharArray();
for(char c:ch){
System.out.print(c+"\t");
}
//(14)大小写转换
s="helloworld";
s=s.toUpperCase();
System.out.println("\n"+s);
System.out.println(s.toLowerCase());
System.out.println(s);
//(15)trim去掉字符串前后空格
s=" hello world ";
System.out.println("s的长度:"+s.length());
System.out.println(s.trim());
System.out.println("因为s.trim()之后,没有重新赋指向,所以s还指向原来的字符串"+s.length());
//(16)valueOf(...)将各种数据类型转成String类
String s9=String.valueOf(100)+String.valueOf(100);
s9=String.valueOf(new Integer(123))+String.valueOf(new Integer(123));
System.out.println(s9);
}
}
- String类型的对象在什么情况下会在堆中开空间
(1)等号右侧使用了new关键字
(2)等号右侧有变量,而且变量参与运算
Stringbuilder 和StringBuffer
StringBuffer与StringBuilder:类的定义相同,功能相同,
StringBuffer是JDK1.0版 :线程同步,安全性高,效率低
StringBuilder是JDK1.5版 :线程不同步,安全性低,效率高
StringBuffer与StringBuilder:称为“容器” 将“数据”
- StringBuffer的常用方法
增 :append(…) ,insert(int index, CharSequence ch)
删 :delete(int start,int end),deleteCharAt(int index)
改 :replace(int start,int end,String s),setCharAt(int index, char ch)
查 :capcity() ,length(),charAt(int index),reverse()
public class TestStringBuffer {
public static void main(String[] args) {
StringBuffer sb=new StringBuffer();//底层创建了一个长度为16的char类型的数组
//添加方法为append
System.out.println("最大容量:"+sb.capacity()+",目前为止已经存多少个字符:"+sb.length());
sb.append(true);
System.out.println("最大容量:"+sb.capacity()+",目前为止已经存多少个字符:"+sb.length());
sb.append("hello");
sb.append("world");
sb.append("98");
System.out.println("最大容量:"+sb.capacity()+",目前为止已经存多少个字符:"+sb.length());
//?如果再继续append是否会出现数组下标超出边界的异常呢?
sb.append("java");//扩容
System.out.println("最大容量:"+sb.capacity()+",目前为止已经存多少个字符:"+sb.length());
System.out.println(sb.charAt(2)); //根据索引获取指定位置上的字符
sb.delete(4,9); //含头不含尾
System.out.println(sb);
sb.deleteCharAt(9); //索引为9的位置上的字符
System.out.println(sb);
sb.insert(9,"java");
System.out.println(sb);
sb.replace(4,9,"中国梦主题歌");
System.out.println(sb);
sb.reverse();//反转
System.out.println(sb);
sb.setCharAt(4,'9');
System.out.println(sb);
String str= sb.substring(4,9); //如果为一个新的String
System.out.println(str);
//用于将StringBuffer转成String类型 toString()
String s=sb.toString();
System.out.println(s);
//如何将String--》StringBuffer
StringBuffer sb2=new StringBuffer("hello");
System.out.println(sb2.capacity()); //21
StringBuffer sb3=new StringBuffer("hello");
System.out.println("sb2==sb3:"+(sb2==sb3)); //false
System.out.println("sb2.equals(sb3):"+sb2.equals(sb3));//false 调用的是Object的equals()方法,比较的是地址
}
}
Date
public class TestDate {
public static void main(String[] args) {
Date d=new Date();
System.out.println(d);
System.out.println(System.currentTimeMillis());//long,从1970-1-1 00:00:00到当前的毫秒值
Date d2=new Date(System.currentTimeMillis());
System.out.println(d2);
//带参的构造方法,只要给一个毫秒值,就可以得到一个日期
Date d3=new Date(1000);
System.out.println(d3);//东八区
System.out.println(d3.toGMTString());//该方法是已弃用的方法,不建议使用,但可以使用
System.out.println(System.currentTimeMillis()); //得到是1970-1-1 00:00:00到当前的毫秒值
System.out.println(d.getTime());//从1970-1-1 00:00:00到创建d这个对象的毫秒值
}
}
用到了两个构造方法,一个带参,一个无参,使用到了个成员方法getTime()
三个子类java.sql包中,用于java程序与数据库之间在“Date”类型之间转换用
public class TestDate3 {
public static void main(String[] args) {
Date d=new Date();
java.sql.Date date=new java.sql.Date(d.getTime());//将util.Date转成sql.Date
java.sql.Time time=new java.sql.Time(d.getTime());
java.sql.Timestamp timestamp=new java.sql.Timestamp(d.getTime());
System.out.println(date);
System.out.println(time);
System.out.println(timestamp);
Date d2=new java.sql.Date(d.getTime());//多态,父类引用指向子类对象
}
}
格式化日期
DateFormat (Abstract):用于String与Date之间进行转换使用的
–>String format(Date d)
–>Date parse(String s)
public class TestDate2 {
public static void main(String[] args) {
Date d=new Date();
System.out.println(d);
DateFormat df= DateFormat.getInstance();
String strDate=df.format(d); //默认的格式化方式19-7-15 下午2:15
System.out.println(strDate);
//使用DateFormat的子类,使用多态的形式创建对象
DateFormat df2=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss.SSS");
String strDate2=df2.format(d);
System.out.println(strDate2);
df2=new SimpleDateFormat("yyyy-MM-dd");
System.out.println(df2.format(d));
//将String-->Date类型
Scanner input=new Scanner(System.in);
System.out.println("请输入您的出生日期:(yyyy-MM-dd):");
String s=input.next();
try {
Date date=df2.parse(s);
System.out.println(date);
} catch (ParseException e) {//第一个检查时异常ParseException
e.printStackTrace();
}
}
}
Calendar:日历类
public class Test {
public static void main(String[] args) {
Calendar calendar=Calendar.getInstance(); //第一种得到Calendar类的对象
// System.out.println(calendar);
//第二种方法得到Calendar类的对象
Calendar calendar1=new GregorianCalendar();
System.out.println("年:"+calendar1.get(1)+"\t"+calendar1.get(Calendar.YEAR));
System.out.println("月:"+(calendar1.get(Calendar.MONTH)+1));
System.out.println("日:"+calendar1.get(Calendar.DAY_OF_MONTH));
System.out.println("时:"+calendar1.get(Calendar.HOUR_OF_DAY));
System.out.println("分:"+calendar1.get(Calendar.MINUTE));
System.out.println("秒:"+calendar1.get(Calendar.SECOND));
//还有一个非常重要的方法setTime(Date d) 用于将Date类型转成对应的日历类型
Date d=new Date(4234398234L);
calendar1.setTime(d);
System.out.println(calendar1);
System.out.println(d);
}
}
Math
Math位于java.lang包
JDK1.5有一个特性,“静态导入”,如果一个类中的方法和属性均为static,就可以使用静态导入,静态导入之前,调用方法时,就可以省略类名不写
public class TestMath {
public static void main(String[] args) {
System.out.println("绝对值:"+abs(10)+"\t"+abs(-10)+"\t"+abs(0));
System.out.println("向上取整转double:"+Math.ceil(9.00001)+"\t"+Math.ceil(-9.999999));
System.out.println("向下取整转double:"+Math.floor(9.999999)+"\t"+Math.floor(-9.000001));
System.out.println("四舍五入:"+Math.round(98.4)+"\t"+Math.round(98.5));
System.out.println("随机数:"+random());//[0,1)
//如何产生一个[1000,9999]的随机数
int number=(int)(Math.random()*9000)+1000;
System.out.println(number);
System.out.println("两个数的最大值:"+Math.max(10,20));
System.out.println("三个数的最小值:"+Math.min(10,Math.min(20,5)));
System.out.println("某个数的N次方:"+Math.pow(3,2));//3的2次方
System.out.println("开方运算:"+Math.sqrt(4));
}
/*public static double random(){
return 1;
}*/
}
枚举
枚举是JDK1.5时加入
枚举使用enum关键字声明
public enum Gender {
男,女;
}
男,女,称为枚举类型的“值”默认为static ,final
枚举不是类,但是具备类的特征,可以有构造方法和成员方法
public enum Gender {
男,女;
Gender(){
}
public void show(){
System.out.println("嘿嘿");
}
}
枚举类型的使用
public class Test2 {
public static void main(String[] args) {
Gender g= Gender.男; //得到了个枚举类型的对象
g.show();//就可以调用成员方法
System.out.println(g); //说明默认调用了toString()方法
//枚举类型如何转成String类型
String str=String.valueOf(g);
//String类型如何转成枚举类型
// 所有的枚举类型“隐式”继承Enum
Gender g2=Enum.valueOf(Gender.class,"男");
System.out.println(g2);
Gender g3=Enum.valueOf(Gender.class,"人妖");// java.lang.IllegalArgumentException 非法参数
}
}
枚举在实体类中使用
public class Person {
private String name;
private int age;
private Gender gender;//枚举类型的性别
}
switch结构的(表达式) byte ,short,int ,char,JDK1.7String ,JDK1.5枚举
public class Test3 {
public static void main(String[] args) {
Gender g=Gender.女;
switch (g){
case 男:
break;
case 女:
break;
}
}
}
File类:位于java.io
- 常用方法
public class TestFile {
public static void main(String[] args) throws IOException {
File file=new File("D:\\java.txt");
// File file=new File("java.txt");
//创建文件
// System.out.println(file.createNewFile());
//删除文件
// System.out.println(file.delete()); //不经过回收站,直接从磁盘上删除
System.out.println("java.txt这个文件在磁盘上是否存在:"+file.exists());
System.out.println("文件内容的大小 :"+file.length());
System.out.println("相对路径:"+file.getPath()); //相对点,java程序的根目录
System.out.println("绝对路径:"+file.getAbsolutePath());
System.out.println("直接输出对象"+file.toString());//重写了toString()方法,调用的是getPath
System.out.println("获取文件的名称:"+file.getName());
}
}
- File操作目录
public class TestFile2 {
public static void main(String[] args) {
//File file=new File("D:\\a");
// file.mkdir();
File file2=new File("D:\\aa\\bb\\cc\\dd");
System.out.println(file2.mkdir());//没有创建成功,只创建单层目录
System.out.println(file2.mkdirs()); //创建多层目录
System.out.println("绝对路径:"+file2.getAbsolutePath());
System.out.println("相对路径:"+file2.getPath());
System.out.println("目录名称:"+file2.getName());
System.out.println("上层目录:"+file2.getParent());
// System.out.println(file2.length());
System.out.println(file2.delete()); //不能删除非空目录
}
}
public class Test {
public static void main(String[] args) {
File file=new File("D:\\190702-505");
String [] str=file.list(); //文件名/目录名
for(String s:str){
System.out.println(s);
}
System.out.println("---------------------------------------");
File [] files=file.listFiles(); //完整路径
for(File f:files){
System.out.println(f);
}
}
}