关于java:java基础之十四常用类

29次阅读

共计 4091 个字符,预计需要花费 11 分钟才能阅读完成。

一、根本数据类型的包装类



包装类是将根本数据类型封装成一个类,蕴含属性和办法
应用:
在应用过程中,会波及到主动装箱和主动拆箱
装箱:将根本数据类型转换成包装类型
拆箱:将包住类型转换成根本数据类型

示例

package com.msbline.basic.common;
public class IntegerDemo {public static void main(String[] args) {
        int a = 10;
        Integer i = new Integer(10);
        // 通过办法进行类型的转换
 Integer i2 = Integer.valueOf(a);
        int i3 = i2.intValue();
        System.out.println(a == i);
        Integer b1 = 100;
        Integer b2 = 100;
        Integer b3 = 200;
        Integer b4 = 200;
        // 这两个后果的不统一,是因为在主动拆箱的时候,
 // 如果 value 不在规定范畴内(-128 到 127)的时候,会 new Interger, // 导致比拟的时候两个地址不统一
 //Interger 中主动拆箱的办法://public static Integer valueOf(int i) {//        if (i >= IntegerCache.low && i <= IntegerCache.high) //            return IntegerCache.cache[i + (-IntegerCache.low)]; //        return new Integer(i); //    } System.out.println(b1 == b2);//true
 System.out.println(b3 == b4);//false
 }
}

二、字符串相干类

1、不可变字符序列:String






留神:
1、常量池在 jdk1.7 之后放在了对空间中
2、字符串的实质
– 字符串的实质是字符数组或者叫字符序列
–String 类应用 final 润饰,不能够被继承
– 应用 equals 办法比拟的是字符数组的每个地位的值

示例
package com.msbline.basic.common;
public class StringDemo {public static void main(String[] args) {
        String str = "abc";
        String str1 = new String("abc");
        System.out.println(str == str1);
        System.out.println(str.equals(str1));
        str1 = str1.intern();
        System.out.println(str == str1);
        System.out.println(str1.charAt(0));
        System.out.println(str1.concat("def"));
        System.out.println("-------------");
        String a = "abc";
        String b = "def";
        String c = "abcdef";
        String d = a+b;
//        String d = (a+b).intern();
        String e = "abc"+"def";
        System.out.println(c == d);//false
        System.out.println(c == e);//true
        System.out.println("-------------");
        String f = "a"+"b"+"c";// 一个对象
        // 如下则是 4 个对象
        String a1 = "a";
        String a2 = "b";
        String a3 = "c";
        String f1 = a1+a2+a3;
    }
}

2、可变字符序列:StringBuffer、StringBuilder

StringBuffer 线程平安 效率低
StringBuilder 线程不平安 效率高
示例
package com.msbline.basic.common;
public class StringBufferDemo {public static void main(String[] args) {StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(1)
                    .append(1.234)
                    .append("abcaaaaaaaaaaaaa")
                    .append(true);
        System.out.println(stringBuffer);
        System.out.println(stringBuffer.charAt(1));
        System.out.println(stringBuffer.length());
        System.out.println(stringBuffer.capacity());
        System.out.println("-----------------------------");
        //StringBuilder 与 StringBuffer 应用大抵一样
 StringBuilder stringBuilder = new StringBuilder();}
}

小结

三、工夫解决相干类

1、Date、DateFormat、SimpleDateFormat、Calender

示例

package com.msbline.basic.common;
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.getYear());
        // 返回以后日期的毫秒值
        System.out.println(date.getTime());
        //yyyy-MM-dd HH:mm:ss
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 将 date 类依照标准转换为字符串格局
        String str = dateFormat.format(date);
        System.out.println(str);
        // 将字符串转换成对应的日期类
        Date date1 = dateFormat.parse("2010-10-10 20:20:20");
        System.out.println(date1);
        System.out.println("-----------------------------------------");
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar);
        // 设置指定工夫的日历类
        calendar.setTime(date1);
        System.out.println(calendar);
        System.out.println(calendar.get(Calendar.YEAR));
        System.out.println(calendar.get(Calendar.MONTH));
        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 类

示例

package com.msbline.basic.common;
public class MathDemo {public static void main(String[] args) {System.out.println(Math.abs(-1));
        System.out.println(Math.sqrt(2));
        // 向上取整
        System.out.println(Math.ceil(3.14));
        // 向下取整
        System.out.println(Math.floor(3.14));
        // 2 的 3 次方
        System.out.println(Math.pow(2,3));
    }
}

五、枚举类 jdk1.5


示例

package com.msbline.basic.common;
public enum Event {LAUNCH("launch"),PAGEVIEW("pageview"),EVENT("event");
    private String name;
    Event(String name){this.name = name;}
    public void show(){System.out.println(this.name);
        Event[] ee = values();
        for(int i = 0; i < ee.length; i++){System.out.println(ee[i].name);
        }
    }
}
package com.msbline.basic.common;
public enum Gender {男, 女}
package com.msbline.basic.common;
public class Test {public static void main(String[] args) {
        Gender gender = Gender. 女;
        Gender gender1 = Gender. 男;
        Event event = Event.LAUNCH;
        System.out.println(event.name());
        event.show();}
}

正文完
 0