001-Java数据类型

        // -------------------- Number对象方法 ------------------
        Number a = 5;
        // Number对象转化为相对应类型的值
        int a1 = a.intValue();
        float a2 = a.floatValue();
        double a3 = a.doubleValue();
        System.out.println("a1=" + a1);
        System.out.println("a2=" + a2);
        System.out.println("a3=" + a3);
        // 将number对象与参数比较,0相等 -1小于 1大于
        int a4 = ((Integer) a).compareTo(2);
        System.out.println("a4=" + a4);
        // 判断number对象是否与参数相等
        boolean a5 = a.equals(a);
        System.out.println("a5=" + a5);
        // 以字符串形式返回值
        String a6 = a.toString();
        System.out.println("a6=" + a6);

        // -------------------- Math类方法 ------------------
        // 返回参数的绝对值
        int e1 = Math.abs(-9);
        System.out.println("e1=" + e1);
        // 返回两个数中最小值
        int e2 = Math.min(-4, 6);
        System.out.println("e2=" + e2);
        // 返回两个数中最大值
        int e3 = Math.max(54, 60);
        System.out.println("e3=" + e3);
        // 返回自然数底数e的参数次方
        double e4 = Math.exp(0);
        System.out.println("e4=" + e4);
        // 返回参数的自然数底数的对数值
        double e5 = Math.log(1);
        System.out.println("e5=" + e5);
        // 返回第一个参数的第二个参数次方
        double e6 = Math.pow(3, 2);
        System.out.println("e6=" + e6);
        // 求参数的算数平方根
        double e7 = Math.sqrt(9);
        System.out.println("e7=" + e7);
        // 将参数转化为角度
        double e8 = Math.toDegrees(Math.PI);
        System.out.println("e8=" + e8);
        // 将角度转化为弧度
        double e9 = Math.toRadians(180);
        System.out.println("e9=" + e9);
        // 产生随机数
        double e10 = Math.random();
        System.out.println("e10=" + e10);


        // -------------------- Character类方法 ------------------
        char ch = 'a';
        char[] chArr = {'h', 'e', 'l', 'l', 'o'};
        // Character类用来操作char
        Character chObj = new Character(ch);
        // 判断是否是一个字母
        boolean isLetter = Character.isLetter(ch);
        System.out.println("isLetter=" + isLetter);
        // 判断是否是一个数字字符
        boolean isDigit = Character.isDigit(ch);
        System.out.println("isDigit=" + isDigit);
        // 判断是否是一个空格
        boolean isWhitespace = Character.isWhitespace(ch);
        System.out.println("isWhitespace=" + isWhitespace);
        // 判断是否是大写字母
        boolean isUpperCase = Character.isUpperCase(ch);
        System.out.println("isUpperCase=" + isUpperCase);
        // 判断是否是小写字母
        boolean isLowerCase = Character.isLowerCase(ch);
        System.out.println("isLowerCase=" + isLowerCase);
        // 小写转化为大写
        char aCh1 = Character.toUpperCase(ch);
        System.out.println("aCh1=" + aCh1);
        // 大写转化为小写
        char aCh2 = Character.toLowerCase(aCh1);
        System.out.println("aCh2=" + aCh2);
        // 字符转字符串
        String aStr = Character.toString(ch);
        System.out.println("aStr=" + aStr);


        // -------------------- String ------------------
        // 创建字符串
        // 方法一:赋初始值
        String str1 = "hello world!";
        // 方法二:利用字符数组创建
        char[] chArray = {'h', 'e', 'l', 'l', 'o', '.'};
        String str2 = new String(chArray);
        // 方法三:利用格式符创建
        int aInt = 4;
        double aDouble = 5.8;
        char aChar = 'g';
        String str3 = String.format("整数值为%d, 小树值为%f, 字符数值为%c", aInt, aDouble, aChar);
        System.out.println("str3=" + str3);

        // 属性
        // 字符串长度
        String s = "hello world!";
        int sLen = s.length();
        System.out.println("sLen=" + sLen);

        // 方法
        String str = "hello world!";
        // 1.返回字符串的长度
        int len = str.length();
        System.out.println("len=" + len);
        // 2.返回指定索引处的char值
        char indexChar = str.charAt(1);
        System.out.println("indexChar=" + indexChar);
        // 3.查找字符
        // 返回指定字符在此字符串中第一次出现的索引
        int firstIndex1 = str.indexOf('l');
        System.out.println("firstIndex1=" + firstIndex1);
        // 返回指定字符在此字符串中第一次出现的索引(从指定的索引开始搜索)
        int firstIndex2 = str.indexOf('l', 4);
        System.out.println("firstIndex2=" + firstIndex2);
        // 返回指定子字符串在此字符串中第一次出现的索引
        int firstIndex3 = str.indexOf("hell");
        System.out.println("firstIndex3=" + firstIndex3);
        // 返回指定子字符串在此字符串中第一次出现的索引(从指定的索引开始搜索)
        int firstIndex4 = str.indexOf("ell", 1);
        System.out.println("firstIndex4=" + firstIndex4);
        // 返回指定字符在此字符串中最后一次出现的索引
        int lastIndex1 = str.lastIndexOf('l');
        System.out.println("lastIndex1=" + lastIndex1);
        // 返回指定字符在此字符串中最后一次出现处的索引(从指定的索引开始搜索)
        int lastIndex2 = str.lastIndexOf('l', 3);
        System.out.println("lastIndex2=" + lastIndex2);
        // 返回指定子字符串在此字符串中最右边出现处的索引
        int lastIndex3 = str.lastIndexOf("ll");
        System.out.println("lastIndex3=" + lastIndex3);
        // 返回指定子字符串在此字符串中最右边出现处的索引(从指定的索引开始搜索)
        int lastIndex4 = str.lastIndexOf("ll", 3);
        System.out.println("lastIndex4=" + lastIndex4);
        // 4.拼接字符串
        String concat = str.concat("Frank Li");
        System.out.println("concat=" + concat);
        // 5.替换字符
        String replace = str.replace('o', 'q');
        System.out.println("replace=" + replace);
        // 6.截取字符串
        // 从指定的索引开始截取到字符串最后一个字符
        String subStr1 = str.substring(2);
        System.out.println("subStr1=" + subStr1);
        // 指定起始索引和结束索引
        String subStr2 = str.substring(2, 5);
        System.out.println("subStr2=" + subStr2);
        // 7.字符串的比较
        // 把这个字符串和另一个对象/字符串比较
        int compare = str.compareTo("hello world");
        System.out.println("compare=" + compare);
        // 把这个字符串和另一个字符串比较(不考虑大小写)
        int compareToIgnore = str.compareToIgnoreCase("Hello world!");
        System.out.println("compareToIgnore=" + compareToIgnore);
        // 比较两个字符串
        boolean isContentEquals = str.contentEquals("hello wrold!");
        System.out.println("isContentEquals=" + isContentEquals);
        // 比较此字符串与指定对象
        boolean isEquals = str.equals("hello");
        System.out.println("isEquals=" + isEquals);
        // 比较两个字符串,不考虑大小写
        boolean isEqualsIgnore = str.equalsIgnoreCase("HeLLo world!");
        System.out.println("isEqualsIgnore=" + isEqualsIgnore);
        // 8.字符串类型转化为byte类型
        byte[] strByte = str.getBytes();
        // 9.字符串类型转化为字符数组
        char[] strCharArr = str.toCharArray();
        // 10.字符串大小写转换
        // 小写转大写
        String strUpperCase = str.toUpperCase();
        System.out.println("strUpperCase=" + strUpperCase);
        // 大写转小写
        String strLowerCase = str.toLowerCase();
        System.out.println("strLowerCase=" + strLowerCase);
        // 11.字符串前后缀检测
        // 检测后缀
        boolean isEnd = str.endsWith("!");
        System.out.println("isEnd=" + isEnd);
        // 检测前缀
        boolean isStart = str.startsWith("hello");
        System.out.println("isStart=" + isStart);


        // -------------------- StringBuffer ------------------
        // 创建StringBuffer
        StringBuffer sBuffer = new StringBuffer("test");

        // 方法
        // 返回序列的长度
        int sBufferLen = sBuffer.length();
        System.out.println("sBufferLen=" + sBufferLen);
        // 追加字符串
        sBuffer.append(" String Buffer");
        System.out.println("sBuffer=" + sBuffer);
        // 将此字符串用反转形式取代
        sBuffer.reverse();
        System.out.println("sBuffer=" + sBuffer);
        // 移除字符(指定起始索引和结束索引)
        sBuffer.delete(2, 4);
        System.out.println("sBuffer=" + sBuffer);
        // 插入字符(指定插入索引)
        sBuffer.insert(3, 'G');
        System.out.println("sBuffer=" + sBuffer);
        // 替换字符(指定起始索引和结束索引)
        sBuffer.replace(2, 4, "haha");
        System.out.println("sBuffer=" + sBuffer);
        // 返回当前序列中指定索引处的字符值
        char bufferChar = sBuffer.charAt(2);
        System.out.println("bufferChar=" + bufferChar);
        // 设置序列中给定索引的值
        sBuffer.setCharAt(3, 'H');
        System.out.println("sBuffer=" + sBuffer);
        // 截取字符串(从指定的索引开始截取到字符串最后一个字符)
        String sBufferSubStr1 = sBuffer.substring(2);
        System.out.println("sBufferSubStr1=" + sBufferSubStr1);
        // 截取字符串(指定起始索引和结束索引)
        String sBufferSubStr2 = sBuffer.substring(2, 5);
        System.out.println("sBufferSubStr2=" + sBufferSubStr2);
        // 返回指定子字符串在此序列中第一次出现的索引
        int firstIndex01 = str.indexOf("hell");
        System.out.println("firstIndex01=" + firstIndex01);
        // 返回指定子字符串在此序列中第一次出现的索引(从指定的索引开始搜索)
        int firstIndex02 = str.indexOf("ell", 1);
        System.out.println("firstIndex02=" + firstIndex02);
        // 返回指定子字符串在此序列中最右边出现处的索引
        int lastIndex01 = str.lastIndexOf("ll");
        System.out.println("lastIndex01=" + lastIndex01);
        // 返回指定子字符串在此序列中最右边出现处的索引(从指定的索引开始搜索)
        int lastIndex02 = str.lastIndexOf("ll", 3);
        System.out.println("lastIndex02=" + lastIndex02);


        // -------------------- 数组 ------------------
        // 声明数组
        // dataType[] myList;
        // 创建数组
        double[] myArr1 = new double[6];
        double[] myArr2 = {4, 5, 6, 3.4};

        double[] myList = {4, 5, 6, 3, 4.5};
        // 遍历数组
        // 第一种:普通的for循环
        for (int i = 0; i < myList.length; i++) {
            System.out.println(myList[i]);
        }
        // 第二种:foreach循环
        for (double element: myList) {
            System.out.println(element);
        }


        // -------------------- 日期 ------------------
        // 创建Date对象
        Date date = new Date();

        // 方法
        Date date1 = new Date();
        Date date2 = new Date();
        // 日期比较
        // true:date1在date2之后 false:date1在date2之前或相等
        boolean isAfter = date1.after(date2);
        // true:date1在date2之前 false:date1在date2之后或相等
        boolean isBefore = date1.before(date2);
        // 0:相等 负数:date1在date2之前 正数:date1在date2之后
        int compareValue = date1.compareTo(date2);
        // true:相等 false:不相等
        boolean isEqual = date1.equals(date2);
        System.out.println("isAfter=" + isAfter + ";" + "isBefore=" + isBefore + ";" + "compareValue=" + compareValue + ";" + "isEqual=" + isEqual);
        // 格式化日期
        Date date3 = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
        String dateStr = dateFormat.format(date3);
        System.out.println("dateStr=" + dateStr);


        // -------------------- Calendar(公历日历) ------------------
        // 创建一个Calendar对象(默认是当前日期)
        Calendar c1 = Calendar.getInstance();
        // 创建一个指定日期的Calendar对象
        Calendar c2 = Calendar.getInstance();
        c2.set(2018, 11, 2);
        // 设置c2的某一个字段值
        c2.set(Calendar.YEAR, 2020);         // 设置年份为2020
        // 把c2的某一个字段值加上一个指定数字
        c2.add(Calendar.DATE, -10);  // 把c2的日期加上-10
        // 获得c2的某一个字段值
        c2.get(Calendar.YEAR);               // 获取c2的年份值
        // GregorianCalendar是Calendar类的一个具体表现    

 

posted @ 2018-11-02 17:17  Frank9098  阅读(203)  评论(0)    收藏  举报