栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【java学习之路】007.常用类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【java学习之路】007.常用类

常用类 基本数据类型包装类
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类
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 {

    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.男;
}

进阶

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 
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
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/715888.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号