常用类

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        char            float       double      boolean

所对应的包装类 Byte     Short      Integer    Long       character    Float      Double     Boolean

构造方法:每个包装类都支持本身数据类型和String类型作为参数构造实例Character类只支持char类型构造实例

注:1.数值型的包装类父类是Number,其他包装类直接继承Object

2.除int与char的包装类分别为Integer和Character外,其余都是首字母大写

3.Byte b1 = new Byte((byte) 120); //调用构造方法,相当于形参传入了,int类型传入byte需要强转,与直接赋值不同

4.Boolean除非传入true(不区分大小写),其他都是false

(了解)包装类的作用:

1.提供了一系列使用的方法

2.集合不允许存放基本数据类型数据,存放数字时,要用包装类型

常用方法:

1.toString() 以字符串形式返回包装对象表示的基本类型数据

2.parse***() 将字符串转换为基本数据类型 character类除外

         int parseInt = Integer.parseInt("123");

3.xxxValue() 拆箱 包装类型转换为基本 知道即可

4.valueOf() 装箱 基本转换为包装类型 知道即可

注: 除了Character类只有一种之外 其他的包装类都提供了valueOf()两种方式(字符串)

自动装箱和拆箱 要求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(不含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等……

nextXXX()

两个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类

equalsIgnoreCase() 忽略大小写比较字符串内容

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() 根据指定的内容分割字符串,返回值是一个字符串数组

contains() 查看源字符串是否包含某一个字符串,返回值为布尔型

endsWith(String 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() 将字符串转为大写

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。String  s1 = "abc";  String s2 = "abc";

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);
    }
}

7.StringBuffer&StringBuilder

String类是不可变的对象,所以当我们需要频繁的修改字符串的内容,推荐使用StringBuffer或者StringBuilder

面试题:

StringBuffer和StringBuilder的区别?

前者是线程安全的,效率低,先有的,since JDK1.0

后者是非线程安全的,效率高,后有的,since JDK1.5

package com.qfedu.test1;
/**
 * 	StringBuffer和StringBuilder区别
 * 	前者是线程安全的 效率低  先有的  since JDK1.0
 * 	后者是线程不安全的 效率高 后有的  JDK1.5
 * @author asus
 */
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类

本类提供了一些程序运行过程中的信息方法,本类不能直接实例化

Runtime.getRuntime() Calendar类、Runtime类、System类均不能直接实例化

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类

本类可以将日期格式化:format(date1)

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));
	}
}

每日问题

1.基本数据类型以及对应包装类

2.什么是自动拆箱和装箱,jdk版本有什么要求?代码举例并标明

3.NumberFormatException是什么异常?代码说明会导致这种异常的情况

4.如果关注事物的本质,使用抽象类,如果关注功能,使用接口。为什么?

5.Math类获取随机数的方式,向上取整,向下,四舍五入

6.Random类获取随机数的方式

7.String类字符串长度的方法,去除空格的方法

8.查找字符串中某个字符第一次出现的位置方法

9.查找字符串中某个字符最后一次出现的位置方法

10.截取字符串的方法 截取的规则

11.拆分字符串的方法 拆分以后返回值是什么

12.判断是否已某个字符串开头

13.判断是否已某个字符串结尾

解答:

1.基本数据类型以及对应包装类

     Byte Short Integer Long Float Double Boolean Character

2.什么是自动拆箱和装箱,jdk版本有什么要求?代码举例并标明

      =号右边赋值给=号左边 Integer a = 5; int b = a;

3.NumberFormatException是什么异常?代码说明会导致这种异常的情况

      数字转换异常,比如将一个不能转换为数字的字符串作为参数构造数值包装类实例 Integer i = new Integer("abc");

4.如果关注事物的本质,使用抽象类,如果关注功能,使用接口。为什么?

      抽象类是可以写普通属性,所以可以描述事物,但是接口不行,接口不能写普通属性

5.Math类获取随机数的方式,向上取整,向下,四舍五入

      Math.random()

      Math.ciel()

      Math.floor()

      Math.round()

6.Random类获取随机数的方式

       Random rand = new Random();

       Random rand = new Random(long seed);

       rand.nextInt();获取整数 rand.nextInt(int bound); rand.nextDouble(); rand.nextFloat(); rand.nextBoolean();

7.String类字符串长度的方法,去除空格的方法

       length(); trim();

8.查找字符串中某个字符第一次出现的位置方法      

       indexOf();

9.查找字符串中某个字符最后一次出现的位置方法

      lastIndexOf(); 返回下标

10.截取字符串的方法 截取的规则

      subString(int beginIndex); 表示从传入的位置开始截取到末尾

      subString(int beginIndex,int endIndex); 比如 3 , 5 包前(包含3下标) 不包后 (不包含5下标)

11.拆分字符串的方法 拆分以后返回值是什么

       split(String codition); 字符串数组

12.判断是否已某个字符串开头 startsWith();

13.判断是否已某个字符串结尾 endsWith();

posted on 2020-08-13 21:36  zitian246  阅读(62)  评论(0编辑  收藏  举报