1.枚举
使用enum修饰类,用于规定一些特殊的值,枚举中的值都是静态常量
枚举也可以用于switch中
package com.qfedu.test1;
public enum Week {
SUN,MON,TUE,WED,THUR,FRI,SAT
}
package com.qfedu.test1;
public class TestEnum {
// enum枚举也可以用于 switch中
// 根据枚举的值 输出一周的天数
public static void main(String[] args) {
Week day = Week.FRI;
switch(day) {
case MON:
System.out.println("周一");
break;
case TUE:
System.out.println("周二");
break;
case WED:
System.out.println("周三");
break;
case THUR:
System.out.println("周四");
break;
case FRI:
System.out.println("周五");
break;
case SAT:
System.out.println("周六");
break;
case SUN:
System.out.println("周日");
break;
}
printDay(Week.WED);
}
public static void printDay(Week day) {
switch(day) {
case MON:
System.out.println("周一");
break;
case TUE:
System.out.println("周二");
break;
case WED:
System.out.println("周三");
break;
case THUR:
System.out.println("周四");
break;
case FRI:
System.out.println("周五");
break;
case SAT:
System.out.println("周六");
break;
case SUN:
System.out.println("周日");
break;
default :
System.out.println("地球的一周只有七天");
break;
}
}
}
2.包装类
每个基本数据类型都有与之对应的包装类
byte short int long float double boolean char
Byte Short Integer Long Float Double Boolean Character
数值型的包装类父类是Number,其他包装类直接继承Object
构造方法:
每个包装类都支持本身数据类型和String类型作为参数构造实例,Character类只支持char类型构造实例
1.**Value()方法
将包装数据类型转换为基本数据类型
2.valueOf()方法
将基本数据类型转换为包装数据类型
3.parse**()方法
每个包装类都提供,将字符串转换为对应的基本数据类型,Character类除外
4.自动装箱和拆箱,要求JDK1.5以后
装箱:将基本数据类型自动转换为包装数据类型 例如:Integer i = 20;
拆箱:将包装数据类型自动转换为基本数据类型 例如 : int a = i;
每个包装类提供的有一些静态常量用于获取本包装类的一些属性信息
比如 Integer.MAX_VALUE获取int最大值
Integer.MIN_VALUE获取int最小值
3.Math类
Math类是数学工具类,提供了两个静态常量E(自然对数的基数),PI(圆周率),和一些常用数学计算公式的方法,本类中的方法全部是静态方法
Math类获取随机数
Math.random();返回值是0~1之间的double类型的小数,所以我们通过乘以10或者乘以100获取到指定范围的随机数
面试题:
Math.ceil()向上取整
Math.floor()向下取整
Math.round()四舍五入
package com.qfedu.test4;
public class Test7 {
public static void main(String[] args) {
// Math
System.out.println(Math.E);
System.out.println(Math.PI);
System.out.println("绝对值" + Math.abs(-30)); // 绝对值
System.out.println("最大值" + Math.max(23, 35)); // 最大的数
System.out.println("最小值" + Math.min(20, 30)); // 最小的数
// 以下三个方法面试题经常出现
System.out.println("向上取整" + Math.ceil(3.3));
System.out.println("向下取整" + Math.floor(3.5));
System.out.println("四舍五入" + Math.round(3.5));
// 随机数
System.out.println("随机数" + (int)(Math.random() * 100));
}
}
4.Random类
Random类也提供了一些用于获取随机数的方法,比Math类获取随机数更为强大,可以获取随机的double,boolean等……
两个Random对象使用相同的种子将得到 相同的随机数
package com.qfedu.test4;
import java.util.Random;
public class Test9 {
public static void main(String[] args) {
Random ran1 = new Random();
System.out.println("无参的nextInt方法" + ran1.nextInt());
System.out.println("有参的nextInt方法" + ran1.nextInt(20));
System.out.println(ran1.nextDouble());
System.out.println(ran1.nextFloat());
System.out.println(ran1.nextBoolean());
System.out.println(ran1.nextLong());
Random ran2 = new Random(20);
Random ran3 = new Random(20);
System.out.println(ran2.nextInt());
System.out.println(ran3.nextInt());
}
}
5.String类
String是开发过程使用较多一个类,提供很多常用的方法
length()获取字符串长度
equals()比较字符串内容,重写与Object类
indexOf(String str)查找某个字符串第一次出现的位置
indexOf(int num)根据ascii码表或者unicode码表查找对应数值所对应的字符串在源字符串中的位置
lastIndexOf(String str)跟indexOf一样,但是是查找的最后一个字符串
lastIndexOf(int num)跟indexOf一样,但是是查找的最后一个字符串
subString(int beginIndex)表示从指定位置截取到字符串末尾
subString(int beginIndex , endIndex)表示从开始截取到指定结束位置,包前不包后
trim()去除字符串首位的空格
split(String str)根据指定的内容分割字符串,返回值是一个字符串数组
contains(String str)查看源字符串是否包含某一个字符串,是返回true,否返回false
endsWith(Strinig str)判断字符串是否已str结尾
startsWith(String str)判断字符串是否已str开头
replace(String oldStr,String newStr)将oldStr替换为newStr
replaceAll(String regx,String newStr)根据正则表达式将匹配到的内容替换为newStr
concat(String str)将源字符串与str拼接,与 + 号拼接字符串效果一样
toLowerCase()将字符串转为小写
toUpperCase()将字符串转为大写
equalsIgnoreCase() 忽略大小写比较字符串内容
toCharArray()将字符串转换为char数据
charAt(int index)返回index下标对应的字符,char
getBytes()将字符串转换为byte数组
package com.qfedu.test5;
public class Test1 {
public static void main(String[] args) {
String str1 = "abcd";
System.out.println("字符串长度" + str1.length());
String str2 = "ABCD";
System.out.println(str1.equals(str2));
System.out.println("忽略大小写比较" + str1.equalsIgnoreCase(str2));
String lowerStr = str2.toLowerCase();
System.out.println("转换为小写的str2" + lowerStr);
String upperStr = str1.toUpperCase();
System.out.println("转换为大写" + upperStr);
String str3 = "a";
String str4 = "b";
System.out.println("使用方法连接字符串" + str3.concat(str4));
System.out.println("使用+号连接字符串" + str3 + str4);
// 查找字符串在源字符串中第一次存在的位置,如果不存在返回-1
String str5 = "abacdefg中国哦a";
System.out.println("a在源字符串中第一次存在的位置,如果不存在返回-1-------" + str5.indexOf("a"));
System.out.println(str5.indexOf(97));
System.out.println(str5.indexOf(20013));
// 查找字符串在源字符串中最后一次存在的位置,如果不存在返回-1
String str6 = "中abcd世界你好ancd中";
System.out.println(str6.lastIndexOf("a"));
System.out.println(str6.lastIndexOf(20013));
String str7 = "abcdefg";
System.out.println(str7);
System.out.println(str7.substring(2)); // 从下标为2的位置开始截取到最后
System.out.println(str7.substring(2, 5));// 包前不包后 包含2下标 不包含5下标
String str8 = " a b c d e f ";
String str9 = str8.trim();
System.out.println(str9);
}
}
package com.qfedu.test5;
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
String str11 = "a1b1c1d1e1fg";
System.out.println(str11.replaceAll("\\d", "数字"));
String str = "ancd";
// 判断字符串是否以某一个字符串开头
System.out.println(str.startsWith("a"));
// 判断文件名是否已 .java结尾
// 邮箱名是否正确
Scanner input = new Scanner(System.in);
System.out.println("请输入文件名");
String fileName = input.next();
System.out.println("请输入邮箱");
String email = input.next();
// int emailIndex = email.lastIndexOf("@");
//
// int index = fileName.lastIndexOf(".");
// if(index != -1 && fileName.substring(index).equals(".java")) {
// System.out.println("文件名正确");
// }else {
// System.out.println("不正确");
// }
//
// if(emailIndex != -1 && email.substring(emailIndex).equals("@163.com")) {
// System.out.println("邮箱正确");
// }else {
// System.out.println("不正确");
// }
// 判断字符串是否以某一个字符串结尾
if(fileName.endsWith(".java")) {
System.out.println("文件名正确");
}else {
System.out.println("文件名错误");
}
if(email.endsWith("@163.com")) {
System.out.println("邮箱正确");
}else {
System.out.println("邮箱错误");
}
}
}
package com.qfedu.test5;
import java.util.Scanner;
public class Test4 {
public static void main(String[] args) {
// 输入一个字符串 提示用户输入一个字 查找这个字在字符串中出现的次数
String str = "我爱你中国 我爱你故乡";
String strs[] = str.split("");
Scanner input = new Scanner(System.in);
System.out.println("请输入一个字符");
String find = input.next();
int count = 0;
for (int i = 0; i < strs.length; i++) {
System.out.println(strs[i]);
if(find.equals(strs[i])) {
count++;
}
}
System.out.println(count);
}
}
6.常量池
整型的包装类和Character包装类取值在byte内将不会产生新的对象,所以我们使用比较为true,也是比较地址,String类型首次直接赋值创建对象,会将字符串内容存入常量池中,第二次创建相同内容的字符串将不会产生新的对象,所以使用比较两个直接赋值并且内容一样的字符串,也为true
package com.qfedu.test1;
public class Test2 {
public static void main(String[] args) {
// == 和equals什么区别呢?
// ==比较基本数据类型比较的是值
// ==比较引用数据类型 比较的是地址
// equals本身也比较地址
// 包装数据类型是基本还是引用?
Integer i1 = 20;
Integer i2 = 20;
System.out.println(i1 == i2);
Integer i3 = 127;
Integer i4 = 127;
System.out.println(i3 == i4);
Integer i5 = -129;
Integer i6 = -129;
System.out.println(i5 == i6);
// 经过我们验证 我们知道当Integer取值为byte范围内 两个对象==比较将输出为true
// 所有的整型包装类型 加上 Character 在byte取值范围内 都输入为true 超过则为false
Short s1 = 127;
Short s2 = 127;
System.out.println(s1 == s2);
Short s3 = 128;
Short s4 = 128;
System.out.println(s3 == s4);
Long l1 = 128L;
Long l2 = 128L;
System.out.println(l1 == l2);
Long l3 = -128L;
Long l4 = -128L;
System.out.println(l4 == l3);
Character ch1 = 127;
Character ch2 = 127;
System.out.println(ch1 == ch2);
Character ch3 = 128;
Character ch4 = 128;
System.out.println(ch3 == ch4);
String str1 = "abc";
String str2 = "abc";
System.out.println(str1 == str2);
String str3 = "ab";
String a = "a";
String b = "b";
String str4 = a + b;
System.out.println(str3 == str4);
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9MyHcecx-1602589696979)(.\img\常量池.png)]
7.StringBuffer&StringBuilder
String类是不可变的对象,所以当我们需要频繁的修改字符串的内容,推荐使用StringBuffer或者StringBuilder
面试题:
StringBuffer和StringBuilder区别?
前者是线程安全的 效率低 先有的 since JDK1.0
后者是线程不安全的 效率高 后有的 JDK1.5
package com.qfedu.test1;
/**
* StringBuffer和StringBuilder区别
* 前者是线程安全的 效率低 先有的 since JDK1.0
* 后者是线程不安全的 效率高 后有的 JDK1.5
* @author WHD
*
*/
public class Test3 {
public static void main(String[] args) {
String str1 = "abc";
String str2 = "abc";
String str3 = new String("abc");
System.out.println(str1 == str3);
System.out.println(str2 == str3);
// 我们发现字符串不能改变内容 改变内容将会产生新的对象
// 所以当我们有这样一个场景 需要频繁修改字符串的内容 那么使用String类就很不合理
// 比如 我们去电商网站搜索一些信息
// 面试题 StringBuffer和StringBuilder区别?
// StringBuffer线程安全的 StringBuilder非线程安全
String str = new String("abcdefg");
str += "a";
System.out.println(str);
StringBuilder sb2 = new StringBuilder("abcdefg");
System.out.println(sb2);
// 包前不包后
System.out.println("删除0~3下标" + sb2.delete(0, 3));
System.out.println(sb2.insert(2, "新添加的内容"));
StringBuffer sb1 = new StringBuffer();
System.out.println("逻辑容量" + sb1.capacity());
System.out.println("实际长度" + sb1.length());
sb1.append(false);
sb1.append("abcd");
sb1.append(20);
sb1.append(20.0);
System.out.println("改变内容以后的sb1" + sb1);
System.out.println(sb1.delete(3, 7));
System.out.println(sb1.insert(4, "世界你好"));
System.out.println(sb1.reverse());
StringBuffer condition = new StringBuffer("手机");
condition.append("华为");
condition.append("1亿像素");
condition.append("12GB内存");
condition.append("2K屏幕");
condition.append("2000以下");
}
}
8.System类
System类提供了一些获取系统信息的方法,退出jvm虚拟机,复制数组,获取系统时间毫秒数等,本类不能直接实例化
package com.qfedu.test2;
import java.util.Properties;
public class TestSystem {
public static void main(String[] args) {
// System 系统
System.out.println("普通信息打印");
// error 错误
System.err.println("错误信息打印");
System.currentTimeMillis();//获取系统时间毫秒数
System.gc();
System.exit(0);//0表示正常退出,非0表示非正常退出
Properties properties = System.getProperties();
System.out.println(properties.getProperty("user.dir"));
System.out.println(properties.getProperty("java.home"));
System.out.println(properties.getProperty("java.version"));
System.out.println(properties.getProperty("os.name"));
System.out.println("=============================");
System.getProperties().list(System.out);
}
}
9.Runtime类
本类提供了一些程序运行过程中的信息方法,本类不能直接实例化
package com.qfedu.test2;
import java.io.IOException;
public class TestRuntime {
public static void main(String[] args) {
// Runtime 运行时
// 此类提供了一些关于程序运行过程中信息获取的方法
// 比如 内存大小等
System.out.println("JVM最大内存" + Runtime.getRuntime().maxMemory() / 1024 / 1024 );
System.out.println("JVM空闲内存" + Runtime.getRuntime().freeMemory() / 1024 / 1024);
System.out.println("JVM总内存" + Runtime.getRuntime().totalMemory() / 1024 / 1024);
try {
Runtime.getRuntime().exec("D:\\apps\\炸弹人.exe");
} catch (IOException e) {
e.printStackTrace();
}
String str = "abc";
str = null;
Runtime.getRuntime().gc(); // 调用垃圾回收机制 但是不是立即回收
System.out.println(str);
}
}
10. java.util.Date类
Date类提供一些用于获取时间相关信息的方法,大多数已经弃用,不推荐使用,可以用
package com.qfedu.test2;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestDate {
public static void main(String[] args) {
// java.util.Date
Date date1 = new Date();
System.out.println(date1);
// 已弃用
// Date date2 = new Date(2021, 11, 22);
// System.out.println(date2);
// 毫秒数 从1970年元月1日0点0分0秒 到现在的毫秒数
// 毫秒 1秒等于1000毫秒
// 纳秒 1秒等于10亿纳秒
System.out.println(System.currentTimeMillis());
Date date3 = new Date(System.currentTimeMillis());
System.out.println(date3);
System.out.println("一个月中的第几天" + date3.getDate());
System.out.println("一周中的第几天" + date3.getDay());
System.out.println("月份,要加1" + date3.getMonth());
System.out.println("小时" + date3.getHours());
System.out.println("分钟" + date3.getMinutes());
System.out.println("秒钟" + date3.getSeconds());
}
}
11.SimpleDateFormat类
本类可以将日期格式化
public class TestDate {
public static void main(String[] args) {
Date date1 = new Date();
// 默认日期显示方式不符合我们的阅读习惯
// 所以我们可以将日期格式化
SimpleDateFormat sdf= new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
System.out.println(sdf.format(date1));
}
}
12.Calendar类
本类也可以获取时间的相关信息,本类不能直接实例化
package com.qfedu.test2;
import java.util.Calendar;
public class TestCalendar {
public static void main(String[] args) {
// Calendar 日历
Calendar cal = Calendar.getInstance();
System.out.println("一个月中的第几天" + cal.get(Calendar.DATE));
System.out.println("一周中的第几天" + cal.get(Calendar.DAY_OF_WEEK));
System.out.println("第几个周几" + cal.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("月份" + cal.get(Calendar.MONTH));
System.out.println("小时" + cal.get(Calendar.HOUR));
System.out.println("分钟" + cal.get(Calendar.MINUTE));
System.out.println("秒钟" + cal.get(Calendar.SECOND));
}
}