包装类
Java是面向对象的语言,但不是纯面向对象,我们常用到的基本数据类型就不是对象,实际应用中需要把基本数据转化成对象。为了解决这个不足,Java在设计类时为每个基本数据类型设计了一个对应的类进行代表,这样八个和基本数据类型对应的类统称为包装类。
1.八大包装类只有char和int的包装类名跟基本数据类型不同
2.八个类中,除了Character和Boolean以外,其他的都是“数字型”,数字型”都是java.lang.Number的子类。Number类是抽象类,因此它的抽象方法,所有子类都需要提供实现。Number类提供了抽象方法:intValue()、longValue()、floatValue()、doubleValue(),意味着所有的“数字型”包装类都可以互相转型。
测试Integer的用法
public class Test00 {
void testInteger() {
// 基本类型转化成Integer对象
Integer int1=new Integer(10);
Integer int2=Integer.valueOf(20);// 官方推荐这种写法
// Integer对象转化成int
int a =int1.intValue();
// 字符串转化成Integer对象
Integer int3=Integer.parseInt("334");
Integer int4=new Integer("999");
// Integer对象转化成字符串
String str1=int3.toString();
// 一些常见int类型相关的常量
System.out.println("int能表示的最大整数:"+Integer.MAX_VALUE);
}
public static void main(String[] args) {
Test00 test=new Test00();
test.testInteger();
}
}
其中 Integer.valueOf()和Integer.parseInt(),一个返回Integer类型,一个返回int 类型。
自动装箱,拆箱
自动装箱:可直接Integer i = 5 就能实现基本数据类型转换成包装类,因为JVM自动执行了Integer i = Integer.valueOf(5),这就是Java的自动装箱。自动装箱调用的是valueOf()方法,而不是new Integer()方法。
ps:包装类在自动装箱时为了提高效率,对于-128~127之间的值会进行缓存处理。超过范围后,对象之间不能再使用==进行数值的比较,而是使用equals方法。
自动拆箱:需要一个值时,对象会自动转成基本数据类型,如:Integer i = 5;int j = i;,系统直接编译成int j = i.intValue();,所以当Integer i = null;int j = i;会抛出空指针异常,自动拆箱调用的xxxValue()方法,i是null,并没有指向任何对象的实体,所以也就不可能操作intValue()方法。
String类
-----String 类对象代表不可变的Unicode字符序列,因此我们可以将String对象称为“不可变对象”
字符串常量拼接时的优化
String str8 = “ping” +“tou”+“ge”;会被编译器优化成一个String str8 = “pingtouge”;对象,所以:
public class Test01 {
public static void main(String[] args) {
String str="hello"+"java";//自动编译成hellojava
String str1="hellojava";
System.out.println(str==str1);//true
String st="hello"+"java";
String st1="hello java";//多加了一个空格,false
System.out.println(st==st1);
String s1="hello";
String s2="java";
//编译的时候不知道变量中存储的是什么,所以没办法在编译的时候优化
String s5=s1+s2;
System.out.println(str1==s5);
}
}
-----StringBuffer和StringBuilder均代表可变的字符序列,当对字符串进行修改的时候,需要使用 StringBuffer 和 StringBuilder 类。和 String 类不同的是,StringBuffer 和 StringBuilder 类的对象能够被多次的修改,并且不产生新的未使用对象。区别:
1StringBuffer,线程安全,做线程同步检查, 效率较低。
2StringBuilder,线程不安全,不做线程同步检查,因此效率较高。
综上,如果只是在单线程中使用字符串缓冲区,则StringBuilder的效率会高些,但是当多线程访问时,最好使用StringBuffer。所以多数情况下建议使用 StringBuilder 类。然而在应用程序要求线程安全的情况下,则必须使用 StringBuffer 类。
public class Test02 {
public static void main(String[] args) {
StringBuilder str=new StringBuilder();
for(int i=0;i<7;i++) {
str.append((char)('a'+i));//加字符
}
//用str.toString()会转换成String类型,用str直接是StringBuilder类型,输出内容一样
System.out.println(str.toString());
System.out.println(str);
str.append(",i love you");//加字符串
System.out.println(str.toString());
System.out.println(str);
StringBuffer str1=new StringBuffer("中华人民共和国");
str1.insert(0, "爱").insert(0, "我");//插入字符串
System.out.println(str1);
str1.delete(0, 2);//删除字符串
System.out.println(str1);
str1.deleteCharAt(0).deleteCharAt(0);//删除索引0的字符
System.out.println(str1);
System.out.println(str1.charAt(0));//输出索引0的字符
System.out.println(str1.reverse());//输出字符串逆序
}
}
-----可变与不可变字符序列使用陷阱
String一经初始化后,就不会再改变其内容了。对String字符串的操作实际上是对其副本(原始拷贝)的操作,原来的字符串一点都没有改变。如果多次执行这些改变串内容的操作,会导致大量副本字符串对象存留在内存中,降低效率。如果这样的操作放到循环中,会极大影响程序的时间和空间性能,甚至会造成服务器的崩溃。
相反,StringBuilder和StringBuffer类是对原字符串本身操作的,可以对字符串进行修改而不产生副本拷贝或者产生少量的副本。因此可以在循环中使用。
DateFormat类和SimpleDateFormat类
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test03 {
public static void main(String[] args) throws ParseException {
SimpleDateFormat s1=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
SimpleDateFormat s2=new SimpleDateFormat("yyyy-MM-dd");
// 将时间对象转换成字符串
String daytime=s1.format(new Date());
System.out.println(daytime);
System.out.println(s2.format(new Date()));
System.out.println(new SimpleDateFormat("hh:mm:ss").format(new Date()));
// 将符合指定格式的字符串转成成时间对象.字符串格式需要和指定格式一致。
String time="2020-01-08";
Date date=s2.parse(time);
System.out.println("date1:"+date);
time="2020-01-08 08:59:23";
date=s1.parse(time);
System.out.println("date2:"+date);
}
}
代码中的格式化字符的具体含义
Calendar日历类
Calendar 类是一个抽象类,为我们提供了关于日期计算的相关功能,比如:年、月、日、时、分、秒的展示和计算。GregorianCalendar 是 Calendar 的一个具体子类,注意月份的表示,一月是0,二月是1,以此类推,12月是11。周日是1,周一是2……周六是7。 因为大多数人习惯于使用单词而不是使用数字来表示月份,这样程序也许更易读,父类Calendar使用常量来表示月份:JANUARY、FEBRUARY等等。
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class Test04 {
public static void main(String[] args) {
GregorianCalendar c=new GregorianCalendar(2049,10,9,00,00,00);
int year=c.get(Calendar.YEAR);
int month=c.get(Calendar.MONTH);
int day=c.get(Calendar.DAY_OF_MONTH);
int day2=c.get(Calendar.DATE);
//Calendar.DATE和Calendar.DAY_OF_MONTH同义
int date=c.get(Calendar.DAY_OF_WEEK);
//1-7.周日是1,周一是2......周六是7
System.out.println(year+"年"+(month+1)+"月"+day+"日"+day2+"日"+"\t星期"+(date-1));
GregorianCalendar c2=new GregorianCalendar();
c2.set(Calendar.YEAR, 2039);
c2.set(Calendar.MONTH, Calendar.FEBRUARY);
c2.set(Calendar.DATE, 3);
c2.set(Calendar.HOUR_OF_DAY, 10);
c2.set(Calendar.MINUTE, 20);
c2.set(Calendar.SECOND, 23);
printCalendar(c2);
GregorianCalendar c3=new GregorianCalendar(2049,10,9,00,00,00);
c3.add(Calendar.MONTH, -7);
c3.add(Calendar.DATE, 7);
printCalendar(c3);
Date d=c3.getTime();
GregorianCalendar c4=new GregorianCalendar();
c4.setTime(new Date());
long g=System.currentTimeMillis();
}
static void printCalendar(Calendar c) {
int year=c.get(Calendar.YEAR);
int month=c.get(Calendar.MONTH)+1;//月份数:0-11
int day=c.get(Calendar.DATE);
//1-7.周日是1,周一是2,。。。周六是7
int date=c.get(Calendar.DAY_OF_WEEK)-1;
String week=""+((date==0)?"日":date);
int hour=c.get(Calendar.HOUR);
int minute=c.get(Calendar.MINUTE);
int second=c.get(Calendar.SECOND);
System.out.printf("%d年%d月%d日,星期%s %d:%d:%d\n",year,month,day,week,hour,
minute,second);
}
}
File类
import java.io.File;
import java.util.Date;
public class Test06 {
public static void main(String[] args) throws Exception {
File f3 = new File("e:/b.txt");//文件绝对路径
File f = new File("a.txt");//文件相对路径,就在该项目文件里
f3.createNewFile();//创建文件
// f3.delete();//删除文件
System.out.println("File是否存在:"+f3.exists());
System.out.println("File是否是目录:"+f3.isDirectory());
System.out.println("File是否是文件:"+f3.isFile());
System.out.println("File最后修改时间:"+new Date(f3.lastModified()));
System.out.println("File的大小:"+f3.length());
System.out.println("File的文件名:"+f3.getName());
System.out.println("File的目录路径:"+f3.getPath());
}
}
通过File对象创建空文件或目录
-----使用mkdir创建目录(有一个目录不存在创建失败)
import java.io.File;
public class Test07 {
public static void main(String[] args) throws Exception{
File f=new File("e:/c.txt");
f.createNewFile();
f.delete();
File f2=new File("e:/电影/华语/大陆");//目录结构中有一个不存在,则不会创建整个目录树
boolean flag=f2.mkdir();
System.out.println(flag);//结果为false
}
}
-----使用mkdirs创建目录(目录不存在则创建整个目录)
import java.io.File;
public class Test07 {
public static void main(String[] args) throws Exception{
File f=new File("e:/c.txt");
f.createNewFile();
f.delete();
File f2=new File("e:/电影/华语/大陆");//目录结构中有一个不存在也没关系;创建整个目录树
//f2.delete();加了这句语句会删除大陆这个文件夹,不会整个目录树都删除掉
boolean flag=f2.mkdirs();
System.out.println(flag);//结果为true
}
}
File综合应用
import java.io.File;
import java.io.IOException;
public class Test07{
public static void main(String[] args) {
//指定一个文件
File file=new File("e:/sxt/b.txt");
//判断该文件是否存在
boolean flag=file.exists();
if(flag) {
//删除
boolean flagd=file.delete();
if(flagd) {
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
}else {
//创建
boolean flagn=true;
try {
//如果目录不存在,先创建目录
File dir=file.getParentFile();
//因为java会把目录创建当成目录或者是文件路径,加了getParentFile(),不会把文件名创建成一个目录。
dir.mkdirs();
//创建文件
flagn=file.createNewFile();
System.out.println("创建成功");
}catch(IOException e) {
System.out.println("创建失败");
e.printStackTrace();
}
}
}
}
来源:CSDN
作者:Atobe.keigo
链接:https://blog.csdn.net/qq_29259051/article/details/103884948