『JavaSE基础入门』九、常用类

九 - 常用类

java.long包下的类不用导包

8 - 1 字符串相关类

不会修改原字符串

String str = "huierb";----此创建方式创建的数据都存放在字符串常量池

  • String类
方法名返回值类型用途返回
charAt(int index)char查看某个索引处的值char指定索引处的值。
indexOf(int ch)int查看某个值处的索引指定字符第一次出现的字符串内的索引。
isEmpty()boolean判断字符串是否为空返回 true,当且仅当 length()为0(即空字符串)
replace(char oldChar, char newChar)String已有字符串的替换返回替换后的字符串
split(String regex)String[]分割字符串分割后的字符串会存入String[]
startsWith(String prefix)boolean判断字符串是否以指定的字符串开头
trim()String去掉字符串前后的空格去掉空格后的字符串
substring(int beginIndex, int endIndex) —[beginIndex,endIndex)String截取字符串(下标左闭右开)返回一个字符串,该字符串是此字符串的子字符串。
toUpperCase()String转换为大写返回转换后的字符串
toLowerCase()String转换为小写返回转换后的字符串
valueOf(Object obj)String将参数转换为String类型
  • ==equals()区别

    new的对象存放在堆中

    =创建的对象放在常量池中

    • equals():
      • 判断的时两个数据的值是否相同
    • == :
      • 比较基本数据类型时,判断的时两个数据的值是否相同
      • 比较引用数据类型时,判断的时两个数据的地址是否相同
  • StringBuilder

    不是线程安全的类

    是在原来字符串的基础上进行的操作,改变了原字符串,字符串拼接时,StringBuilder用的较多

    方法名返回值类型作用返回
    append(Object obj)StringBuilder专门用来在原字符串上拼接的拼接后的字符串
    delete(int start, int end)StringBuilder删除一段字符串[start,end]删除后的字符串
    replace(int start, int end, String str)StringBuilder用str替换[start,end]上的字符替换后的字符串
    insert(int offset, char c)StringBuilder指定位置插入
    reverse()StringBuilder逆序输出
  • StringBufferStringBuilder中的方法基本一致

    StringBuffer是线程安全的类

8 - 2 数字相关类

8 - 2 -1 引用数据类型与基本数据类型

集合中的泛型必须用引用数据类型

引用数据类型的默认初始值均为null

引用数据类型基本数据类型
Bytebyte
Shortshort
Integerint
Longlong
Doubledouble
Folatfloat
Booleanboolean
Characterchar
  • 运行时常量池

Integer a = 12;----引用数据类型创建的数据放在运行时常量池

运行时常量池数据范围整数的常量池范围均是一个字节(-128----127

故在此范围内创建的数据地址相同,如果不再这个范围内,则创建的数据的地址不同

public class Test {
    public static void main(String[] args) {

        Integer a = 127;
        Integer b = 127;
        
        System.out.println("a==b : "+ (a==b));  // true
        
        System.out.println("a.equals(b)) : " + a.equals(b));
        System.out.println("a.intValue()==b : "+ (a.intValue()==b)); // equals()的底层实现
        
        
        Integer a1 = 128;
        Integer b1 = 128;
        
        System.out.println("a1==b1 : "+ (a1==b1)); // false
        
        System.out.println("a1.equals(b1)) : "+a1.equals(b1));
    }

}
  • 自动拆箱和自动装箱

    基本数据类型-------------------(自动装箱)-------------------->引用数据类型—包装类(具有了一定的功能)

    引用数据类型-------------------(自动拆箱)-------------------->基本数据类型

8 - 2 - 2 Random

生成随机数

  • random.nextInt(10)其中的参数为随机数的范围(取不到10)
/**
 * 随机产生4位验证码(包含数字、大小写字母)
 */
public class Test {
    public static void main(String[] args) {

        String str = "1234567890qwertyuiopasdfghjklzxcvbnmWQERTYUIOPASDFGHJKLZXCVBNM";

        Random random = new Random();
        char[] arr = new char[4];
        for (int i = 0; i < arr.length ; i++){
            arr[i] = str.charAt(random.nextInt(str.length()));
        }
        
        System.out.println(new String(arr));
    }

}
8 - 2 - 3 Scanner()

控制台输入数据

  • next()nextLine()区别

    两者同时使用会出错

    • next()

      接收一行数据,识别到回车就认为是一行,并且会清空回车

    • nextLine()

      接收字符串的数据,回车只是用来接收数据,控制台上会留有回车

8 - 3 Math类

方法作用
abs(int a)求绝对值
addExact(int x, int y)求和
max(int a, int b)求最大值
min(int a, int b)求最小值
random()返回0.0–1.0之间的Double类型的随机数
floor(double a)向下取整
ceil(double a)向上取整
Math.PI
pow(double a, double b)a的b次方
sqrt(double a)a的平方根

8 - 4 精确数字匹配(精确小数运算)

float、double都是不精确的小数

public class Test {
    public static void main(String[] args) {

        System.out.println(100.0/3);    // 33.333333333333336
        System.out.println(0.01+0.05);  // 0.060000000000000005
        System.out.println(1.0-0.9);    // 0.09999999999999998
    }

}
  • 涉及到精确小数运算,不能用float、double,得BigDecimal

    使用BigDecimal时,参数必须使用的构造方法,参数必须为String

    public class InputTest {
        public static void main(String[] args) {
    
            System.out.println(100.0/3);    // 33.333333333333336
            
            BigDecimal bigDecimal1 = new BigDecimal("0.01");	// 参数必须为String
            BigDecimal bigDecimal2 = new BigDecimal("0.05");
            
            System.out.println(0.01+0.05);  // 0.060000000000000005
            System.out.println(bigDecimal1.add(bigDecimal2));	// 0.6
    
            System.out.println(1.0-0.9);    // 0.09999999999999998
            System.out.println(new BigDecimal("1.0").subtract(new BigDecimal("0.9"))); 	// 0.1
    
        }
    
    }
    
  • 需要将BigDecimal数据格式化输出时,使用DecimalFormat

    public class InputTest {
        public static void main(String[] args) {
    
            DecimalFormat decimal = new DecimalFormat("#,###.00");  // 定义输出的格式
            String df1 = decimal.format(1000000000);   // 格式化后的数据为字符串
            System.out.println(df1);
    
    
            BigDecimal bigDecimal1 = new BigDecimal("1.0");
            BigDecimal bigDecimal2 = new BigDecimal("3");
            //                 参与运算的数据1     参与运算的数据2        运算的舍入模式
            BigDecimal divide = bigDecimal1.divide(bigDecimal2, BigDecimal.ROUND_HALF_DOWN);
    
            DecimalFormat decimalFormat = new DecimalFormat("0.00");  // 定义输出的格式
            String df = decimalFormat.format(divide);   // 格式化后的数据为字符串
            System.out.println(df);
    
        }
    
    }
    

8 - 5 时间类

8 - 5 - 1 Data(日期类)

Date

yyyy-MM-dd HH:mm:ss

y:年

M:月

d:一月中的第几天

D:一年中的第几天

H:24小时

h:12小时

m:分钟

s:秒

S:毫秒

  • 日期格式化输出

    SimpleDateFormat

    public class Test {
        public static void main(String[] args) {
    
            Date date = new Date();
            System.out.println(date);   // Mon Jun 07 20:05:10 CST 2021
    
            // 格式化打印时间(可以单独的获取年、月、日等)
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = sdf.format(date);
            System.out.println(format); // 2021-06-07 20:05:10
    
            // 将字符串格式转化为时间格式
            String str = "2020-5-21 13:14:21";
            try {
                Date myDate = sdf.parse(str);
                System.out.println(date.after(myDate)); //true
                System.out.println(date.before(myDate)); //false
            } catch (ParseException e) {
                e.printStackTrace();
            }
    
        }
    
    }
    
8 - 5 - 1 Calendar(日历类)

涉及到日期类的计算,使用Calendar

public class InputTest {
    public static void main(String[] args) {

        Calendar calendar = Calendar.getInstance(); // 得到当前时间(日历表示)
        System.out.println(calendar);
        System.out.println(calendar.get(Calendar.YEAR)); // 哪一年
        System.out.println(calendar.get(Calendar.MONTH+1)); // 哪一月
        System.out.println(calendar.get(Calendar.DAY_OF_YEAR)); // 一年中的第几天

        calendar.add(Calendar.DAY_OF_YEAR,100); // 计算100天以后的时间
        Date date = calendar.getTime();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String f = sdf.format(date); // 将100天以后的日期格式化输出
        System.out.println(f);

    }

}
  • 自己设置时间计算时间段
public class Test {
    public static void main(String[] args) {

        // 手动输入一个时间
        String str = "2020-5-21 13:14:21";
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        
        Calendar calendar = sdf1.getCalendar();
        // 计算3年后的第100天是哪一天
        calendar.add(Calendar.YEAR, 3);
        calendar.add(Calendar.DAY_OF_YEAR, 100);
        //将日历转化为日期
        Date time = calendar.getTime();
        //将日期转化为指定格式的字符串
        String format = sdf1.format(time);
        System.out.println(format);
        
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值