概述
包装类
public class TestWrappdeClass {
public static void main(String[] args) {
Integer a = new Integer(3);
}
}
常用方法
//基本数据类型转成包装类对象
Integer a = new Integer(3);
Integer b = Integer.valueOf(30);
//把包装类对象转成基本数据类型
int c = b.intValue();
double d = b.doubleValue();
//把字符串转成包装类对象
Integer e = new Integer("9999");
Integer f = Integer.parseInt("999888");
//把包装类对象转成字符串
String str = f.toString(); //""+f
//常见的常量
System.out.println("int类型最大的整数:"+Integer.MAX_VALUE);
自动装箱拆箱
Integer a = 234; //自动装箱。Integer a = Integer.valueOf(234);
int b = a;//自动拆箱。编译器会修改成:int b = a.intValue();
缓存
//缓存[-128,127]之间的数字。实际就是系统初始的时候,创建了[-128,127]之间的一个缓存数组。
//当我们调用valueOf()的时候,首先检查是否在[-128,127]之间,如果在这个范围则直接从缓存数组中拿出已经建好的对象
//如果不在这个范围,则创建新的Integer对象。
Integer in1 = Integer.valueOf(-128);
Integer in2 = -128;
System.out.println(in1 == in2);//true 因为123在缓存范围内
System.out.println(in1.equals(in2));//true
System.out.println("################");
Integer in3 = 1234;
Integer in4 = 1234;
System.out.println(in3 == in4);//false 因为1234不在缓存范围内
System.out.println(in3.equals(in4));//true
不可变字符序列String类
不可变
String生成后不可变
String str = "aaabbbb";
String str2 = str.substring(2, 5);
System.out.println(str);
System.out.println(str2);
做比较时使用equals,而不用==
//编译器做了优化,直接在编译的时候将字符串进行拼接
String str1 = "hello" + " java";//相当于str1 = "hello java";
String str2 = "hello java";
String str3 = new String("hello java");
System.out.println(str1 == str2);//true,都在常量池里面,所以地址相同,是同一个对象。
System.out.println(str1 == str3);//flase,new String新建了一个对象
String str3 = "hello";
String str4 = " java";
//编译的时候不知道变量中存储的是什么,所以没办法在编译的时候优化
String str5 = str3 + str4;
System.out.println(str2 == str5);//false
System.out.println(str2.equals(str5)); //做字符串比较的时候,使用equals不要使用==
常用方法
String s1 = "core Java";
String s2 = "Core Java";
System.out.println(s1.charAt(3));//提取下标为3的字符
System.out.println(s2.length());//字符串的长度
System.out.println(s1.equals(s2));//比较两个字符串是否相等
System.out.println(s1.equalsIgnoreCase(s2));//比较两个字符串(忽略大小写)
System.out.println(s1.indexOf("Java"));//字符串s1中是否包含Java
System.out.println(s1.indexOf("apple"));//字符串s1中是否包含apple
String s = s1.replace(' ', '&');//将s1中的空格替换成&
System.out.println("result is :" + s);
System.out.println(s1.startsWith("How"));//是否以How开头
System.out.println(s1.endsWith("you"));//是否以you结尾
s = s1.substring(4);//提取子字符串:从下标为4的开始到字符串结尾为止
System.out.println(s);
s = s1.substring(4, 7);//提取子字符串:下标[4, 7) 不包括7
System.out.println(s);
s = s1.toLowerCase();//转小写
System.out.println(s);
s = s1.toUpperCase();//转大写
System.out.println(s);
String s2 = " How old are you!! ";
s = s2.trim();//去除字符串首尾的空格。注意:中间的空格不能去除
System.out.println(s);
System.out.println(s2);//因为String是不可变字符串,所以s2不变
可变字符序列StringBuilder和StringBuffer
String str;
//StringBuilder线程不安全,效率高(一般使用它);StringBuffer线程安全,效率低。
StringBuilder sb = new StringBuilder("abcdefg");
System.out.println(Integer.toHexString(sb.hashCode()));
System.out.println(sb);
sb.setCharAt(2, 'M');//修改下标为2的字符
System.out.println(Integer.toHexString(sb.hashCode()));
System.out.println(sb);
/*
15db9742
abcdefg
15db9742 地址没变
abMdefg 结果变了
*/
用法
StringBuilder sb = new StringBuilder();
for(int i=0;i<26;i++){
char temp = (char)('a'+i);
sb.append(temp);//在数组后列追加字符
}
System.out.println(sb);
sb.reverse(); //倒序
System.out.println(sb);
sb.setCharAt(3, '高');//替换
System.out.println(sb);
sb.insert(0, '我').insert(6, '爱').insert(10, '你'); //链式调用。核心就是:该方法调用了return this,把自己返回了。
System.out.println(sb);
sb.delete(20, 23);// 删除某个区间的字符
System.out.println(sb);
陷阱
/** 使用String进行字符串的拼接 */
String str8 = "";
// 本质上使用StringBuilder拼接, 但是每次循环都会生成一个StringBuilder对象
long num1 = Runtime.getRuntime().freeMemory();// 获取系统剩余内存空间
long time1 = System.currentTimeMillis();// 获取系统的当前时间
for (int i = 0; i < 5000; i++) {
str8 = str8 + i;// 相当于产生了10000个对象(i和str8)
}
long num2 = Runtime.getRuntime().freeMemory();
long time2 = System.currentTimeMillis();
System.out.println("String占用内存 : " + (num1 - num2));
System.out.println("String占用时间 : " + (time2 - time1));
/** 使用StringBuilder进行字符串的拼接 */
StringBuilder sb1 = new StringBuilder("");
long num3 = Runtime.getRuntime().freeMemory();
long time3 = System.currentTimeMillis();
for (int i = 0; i < 5000; i++) {
sb1.append(i);
}
long num4 = Runtime.getRuntime().freeMemory();
long time4 = System.currentTimeMillis();
System.out.println("StringBuilder占用内存 : " + (num3 - num4));
System.out.println("StringBuilder占用时间 : " + (time4 - time3));
/*
String占用内存 : 49776496
String占用时间 : 43
StringBuilder占用内存 : 0
StringBuilder占用时间 : 0
方法一十分占用内存与时间,所以不要使用。
*/
时间处理相关类
计算机世界里,1970年1月1日 00:00:00定为基准时间。每个度量单位是毫秒。
Date类
import java.util.Date;
Date d = new Date();
System.out.println(d); //当前电脑时刻
Date d1 = new Date(2000);
System.out.println(d2);//基准时间+2000ms
System.out.println(d.getTime()); //获得日期毫秒数
Date d2 = new Date();
System.out.println(d2.getTime());
System.out.println(d2.after(d)); //测试日期是否在d之后
System.out.println(d2.before(d)); //测试日期是否在d之前
//以后遇到日期处理:使用Canlendar日期类
Date d3 = new Date(2020-1900,3,10); //2020年4月10日
System.out.println(d3);
DateFormat类(抽象类)
一般使用他的实现类SimpleDateFormat。
把时间对象转化成指定格式的字符串,反之也行。
public static void main(String[] args) throws ParseException {
//把时间对象按照“格式字符串指定的格式”转成相应的字符串
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//抽象类实现
String str = df.format(new Date(4000000));
System.out.println(str);
//把字符串按照“格式字符串指定的格式”转成相应的时间对象
DateFormat df2 = new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒");
Date date = df2.parse("1983年5月10日 10时45分59秒");
System.out.println(date);
//测试其他的格式字符。比如:利用D,获得本时间对象是所处年份的第几天。
DateFormat df3 = new SimpleDateFormat("D");
String str3 = df3.format(new Date());
System.out.println(str3);
}
Calendar类
//获得日期的相关元素
Calendar calendar = new GregorianCalendar(2999,10,9,22,10,50);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DATE); //也可以使用:DAY_OF_MONTH。
int weekday = calendar.get(Calendar.DAY_OF_WEEK); //星期几。 1-7. 1:星期日,2星期一,。。。7是星期六。
System.out.println(year) ;
System.out.println(month) ; //0-11表示对应的月份。0是1月,1月是2月.....11是12月。
System.out.println(weekday);
System.out.println(day);
//设置日期的相关元素
Calendar c2 = new GregorianCalendar();
c2.set(Calendar.YEAR, 8012);//不写此句的话,则输出当天的日期
System.out.println(c2);
//日期的计算
Calendar c3 = new GregorianCalendar();
c3.add(Calendar.YEAR, -100); //往前100年
System.out.println(c3) ;
//日期对象和时间对象的转化
Date d4 = c3.getTime();
Calendar c4 = new GregorianCalendar();
c4.setTime(new Date());
printCalendar(c4);
public static void printCalendar(Calendar c){
//打印:1918年10月10日 11:23:45 周三
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH)+1; //0-11
int date = c.get(Calendar.DAY_OF_MONTH);
int dayweek = c.get(Calendar.DAY_OF_WEEK)-1; //1-7.1周日,2周1,3周2....
String dayweek2 = dayweek==0?"日":dayweek+"";
int hour = c.get(Calendar.HOUR);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
System.out.println(year+"年"+month+"月"+date+"日 "+hour+"时"+minute+"分"+second+"秒"
+" 周"+dayweek2);
}
日历可视化
难点:
如何判断输入的日期符合格式?
如何识别每个月的天数,以及每天对应的星期数?
Math类
//取整相关操作
System.out.println(Math.ceil(3.2)); //入
System.out.println(Math.floor(3.2)); //舍
System.out.println(Math.round(3.2)); //四舍五入
System.out.println(Math.round(3.8));
//绝对值、开方、a的b次幂等操作
System.out.println(Math.abs(-45)); //绝对值
System.out.println(Math.sqrt(64)); //开方
System.out.println(Math.pow(5, 2)); //a的b次幂
System.out.println(Math.pow(2, 5));
//Math类中常用的常量
System.out.println(Math.PI); //Π
System.out.println(Math.E); //e
//随机数
System.out.println(Math.random());// [0,1)
Random类
Random rand = new Random();
//随机生成[0,1)之间的double类型的数据
System.out.println(rand.nextDouble());
//随机生成int类型允许范围之内的整型数据
System.out.println(rand.nextInt());
//随机生成[0,1)之间的float类型的数据
System.out.println(rand.nextFloat());
//随机生成false或者true
System.out.println(rand.nextBoolean());
//随机生成[0,10)之间的int类型的数据
System.out.println(rand.nextInt(10));
//随机生成[20,30)之间的int类型的数据
System.out.println(20 + rand.nextInt(10));
//随机生成[20,30)之间的int类型的数据(此种方法计算较为复杂)
System.out.print(20 + (int) (rand.nextDouble() * 10));
File类
java.io.File类:代表文件和目录。在开发中,读取、生产、删除、修改文件会使用到本类。
// File f = new File("d:/a.txt"); 两种写法均可
File f = new File("d:\\a.txt");
System.out.println(f); //输出文件路径
f.renameTo(new File("d:/bb.txt")); //修改文件名称
System.out.println(System.getProperty("user.dir")); //当前程序所在目录
File f2 = new File("gg.txt");
f2.createNewFile(); //创建新文件,默认在当前程序所在目录
f2.delete(); //删除文件
System.out.println("File是否存在:"+f2.exists());
System.out.println("File是否是目录:"+f2.isDirectory());
System.out.println("File是否是文件:"+f2.isFile());
System.out.println("File最后修改时间:"+new Date(f2.lastModified()));
System.out.println("File的大小:"+f2.length());
System.out.println("File的文件名:"+f2.getName());
System.out.println("File的目录路径:"+f2.getAbsolutePath());
File f3 = new File("d:/电影/华语/大陆");
//boolean flag = f3.mkdir(); //目录结构中有一个不存在,则不会创建整个目录树
boolean flag = f3.mkdirs();//目录结构中有一个不存在也没关系;创建整个目录树
System.out.println(flag);//创建成功
递归遍历目录结构
枚举
Tips:
- 当你需要定义一组常量时,可以使用枚举类型。
- 尽量不要使用枚举的高级特性,高级特性都可以使用普通类来实现。
/*
enum 枚举名{
枚举体(常量列表)
}
*/
enum Season{
SPRING, SUMMER,AUTUMN,WINTER
}
enum Week {
星期一,星期二,星期三,星期四,星期五,星期六,星期日
}
// 用法
System.out.println(Season.SPRING); //输出SPRING
Season a = Season.AUTUMN;
switch(a){ //输出为秋天
case SPRING:
System.out.println("春天来了,播种的季节");
break;
case SUMMER:
System.out.println("夏天来了,游泳的季节");
break;
case AUTUMN:
System.out.println("秋天来了,收获的季节");
break;
case WINTER:
System.out.println("冬天来了,冬眠的季节");
break;
}

806

被折叠的 条评论
为什么被折叠?



