【java学习之路】(java SE篇)007.常用类

本文深入讲解Java中的基本数据类型包装类、字符串操作、日期处理、数学运算及枚举类的使用技巧,帮助读者掌握核心概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

常用类

基本数据类型包装类

/*
    包装类和基本数据类型
        包装类是将基本数据类型封装成一个类,包含属性和方法
        使用:
            在使用过程中会涉及到自动装箱和自动拆箱
            装箱:将基本数据类型转换成包装类  intValue()
            拆箱:将包装类转换成基本数据类型  Integer.valueOf()
 */
public class IntegerDemo {
    public static void main(String[] args) {
        int a = 10;
        Integer j = new Integer(10);

        //拆箱与装箱的过程
        Integer j1 = Integer.valueOf(a);//装箱
        int j2 = j.intValue();//拆箱

        Integer i1 = 100;
        Integer i2 = 100;
        Integer i3 = 200;
        Integer i4 = 200;
        System.out.println(i1==i2);//true
        System.out.println(i3==i4);//false
       //Integer源码:
//        public static Integer valueOf(int i) {
//            if (i >= Integer.IntegerCache.low && i <= Integer.IntegerCache.high)
//        m和n均从缓存中取得同一个127(范围:-128~+127)
//                return Integer.IntegerCache.cache[i + (-Integer.IntegerCache.low)];
        //超出范围,自动创建一个新的对象,地址不再一样
//            return new Integer(i);
//        }


        Double d1 = 1.0;
        Double d2 = 1.0;
        Double d3 = 2.0;
        Double d4 = 2.0;
        System.out.println(d1==d2);//false
        System.out.println(d3==d4);//false
        //Double源码:
//        public static Double valueOf(double d) {
//            return new Double(d);
//        }


    }
}

String类

/*
    注意:常量池在jdk1.7之后放置在了堆空间之中

    字符串的使用:
        1.创建
            String str1 = "abc";
            String str2 = new String("abc");
            两种方法都可以
        2.字符串的本质
            1.字符串的本质是字符数组或字符序列
            2.String类使用final定义,不可以被继承
            3.使用equals方法比较的是字符数组的每一个位置的值
            4.String是一个不可变对象
 */
public class StringDemo {

    public static void main(String[] args) {
        String str1 = "abc";
        String str2 = new String("abc");


        System.out.println(str1.equals(str2));//比较两个字符串是否相等  true
        System.out.println(str1.charAt(1));//取出字符串中某个特定元素  b
        System.out.println(str1.concat("cde"));//字符数组的复制+拼接  abccde
        System.out.println(str1.compareTo(str2));//比较字符串 -1 前面小 0 相等 1 后面小
        String str3 = "ABC";
        System.out.println(str1.compareToIgnoreCase(str3));//将字符串全部转为小写并比较
        System.out.println(str1.indexOf("b"));//从字符串中获取指定元素的下标  1
        String str4 = "abcdefghigklmn";
        System.out.println(str4.substring(6));//指定开始,直到字符串结尾,截取字符串  ghigklmn
        System.out.println(str4.substring(2, 6));//指定开始,指定结束,截取字符串  cdef
        System.out.println(str4.length());//返回字符数组(字符串)长度

//        String a = "abcd";
//        String b = new String("abcd");
        //是当前的字符对象(通过new出来的对象)可以使用intern方法从常量池中获取,
        // 如果常量池中不存在该字符串,那么就新建一个这样的字符串放到常量池中。
//        b = b.intern();
//        System.out.println(a==b);//true

        String a = "abc";
        String b = "def";
        String c = "abcdef";
        String d = a+b;
        String e = "abc"+"def";
        System.out.println(c==d);//false
        System.out.println(c==e);//true

        String f = "a"+"b"+"c";
        String a1 = "a";
        String a2 = "b";
        String a3 = "c";
        String a4 = a1+a2+a3;
        //一共生成几个对象:4个   “abc”、“a”、“b”、“c”
    }
}

StringBuffer类和StringBuilder类

public class StringBufferDemo {
/*
    可变字符串
        StringBuffer:线程安全,效率低
        StringBuilder:线程不安全,效率高

 */
    public static void main(String[] args) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(1).append(1.234).append("abc").append(true);
        System.out.println(stringBuffer);//拼接字符串

        System.out.println(stringBuffer.length());//剩余容量
        System.out.println(stringBuffer.capacity());//总容量

        System.out.println(stringBuffer.delete(1,2 ));//删除某位置的值
        System.out.println(stringBuffer.insert(1, 1));//往指定位置插入元素


        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("123").append(1).append(false);
    }
}

日期处理相关类

在这里插入图片描述

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateDemo {

    public static void main(String[] args) throws ParseException {
        Date date = new Date();
        System.out.println(date);
        System.out.println(date.getTime());//毫秒值

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str = dateFormat.format(date); //dateFormat.format() 将Date类按照规定的日期格式返回一个字符串
        System.out.println(str);
        //将字符串转换成对应的日期类
        Date d1 = dateFormat.parse("2010-10-10 20:20:20");
        System.out.println(d1);

        //一种全新创建类的方式
        Calendar calendar = Calendar.getInstance();
        //获取当前系统的时间
        System.out.println(calendar);//格林威治标准时间(GMT)
        //获取年月日,时分秒
        calendar.setTime(d1);
        System.out.println(calendar);
        System.out.println(calendar.get(Calendar.YEAR));
        System.out.println(calendar.get(Calendar.MONTH));//9  从零开始,表示10月份
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
        System.out.println(calendar.get(Calendar.HOUR_OF_DAY));
        System.out.println(calendar.get(Calendar.MINUTE));
        System.out.println(calendar.get(Calendar.SECOND));
    }
}

Math类

public class MathDemo {

    public static void main(String[] args) {
        System.out.println(Math.abs(-1));//取绝对值
        System.out.println(Math.sqrt(4));//开方
        System.out.println(Math.ceil(3.14));//向上取整
        System.out.println(Math.floor(3.14));//向下取整
        System.out.println(Math.pow(2, 3));//a的b次方
    }
}

枚举类

最初级的枚举类用法

public enum Gender {,}
public class Test {

    Gender gender1 = Gender.;
    Gender gender2 = Gender.;
}

进阶

/*
    枚举类型:
        1.只能够取特定值中的一个
        2.使用enum关键字
        3.所有的枚举类型隐性地继承java.lang.Enum
        (枚举实质上还是类,而每个被枚举的成员实质就是一个枚举类型的实例,默认为public static final,可以直接通过枚举类型名使用)
        4.推荐当要定义一组常量的时候,使用枚举类
 */
public enum EventEnum {

    LAUNCH("launch"),PAGEVIEW("pagevien"),EVENT("event");

    EventEnum(String name){
        this.name = name;
    }

    private String name;

    public void show(){
        System.out.println(this.name);
        EventEnum[] ee = values();
        for(int i=0;i<ee.length;i++){
            System.out.println(ee[i]);
        }
    }
}
public class Test {

    public static void main(String[] args) {
        EventEnum ee = EventEnum.LAUNCH;//launch
        ee.show();
        //LAUNCH
		//PAGEVIEW
		//EVENT

        String name = EventEnum.PAGEVIEW.name();
        System.out.println(name);
        //PAGEVIEW
    }
}
 static void main(String[] args) {
        EventEnum ee = EventEnum.LAUNCH;//launch
        ee.show();
        //LAUNCH
		//PAGEVIEW
		//EVENT

        String name = EventEnum.PAGEVIEW.name();
        System.out.println(name);
        //PAGEVIEW
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值