天天熱點!【學懂Java】(六)常用類

2023-01-25 20:32:41 來源:51CTO博客

一.包裝類

1.概念

包裝類是將基本類型封裝到一個類中,包含屬性和方法,方便對象操作包裝類位于java.lang包中

2.轉換

包裝類與基本數據類型

包裝類是將基本數據類型封裝成一個類,包含屬性和方法


(資料圖片)

使用

在使用過程中,會涉及到自動裝箱和自動拆箱

裝箱:將基本數據類型轉換成包裝類基本類型就自動地封裝到與它相同類型的包裝中,如:Integer i = 100;本質上,編譯器編譯時為我們添加了:Integer i = Integer.valueOf(100);拆箱:將包裝類轉換成基本數據類型包裝類對象自動轉換成基本類型數據。如:int a = new Integer(100);本質上,編譯器編譯時為我們添加了:– int a = new Integer(100).intValue();
public class IntegerDemo {    public static void main(String[] args) {//        int a = 10;//        Integer i = new Integer(10);//被拆箱了//------------------------------------------//        //通過方法進行類型的轉換//        Integer i2 = Integer.valueOf(a);//裝箱//        int i3 = i.intValue();//拆箱//------------------------------------------//        System.out.println(a == i);//        Float f1 = new Float(3.14);//        Double d1 = new Double(3.14);        Integer i = 10;        int a = i;        System.out.println(a==i);    }}

案例

//        int i =100;//        Integer i1 = 100;//        Integer i2 = 100;//        Integer i3 = 200;//        Integer i4 = 200;//        System.out.println(i1==i2);//        System.out.println(i3==i4);//        結果//true//false

看valueOf的實現

值在-128---127之間返回相同Integer

超過這個范圍后就返回??new Integer(i);???

//        Double d1 = 1.0;//        Double d2 = 1.0;//        Double d3 = 2.0;//        Double d4 = 2.0;//        System.out.println(d1==d2);//        System.out.println(d3==d4);//        結果//false//false

都返回??new??

二.String

注意:常量池在1.7之后放置在了堆空間之中

字符串的使用:

1.創建

String str = "abc";String str2 = new String("abc");兩種方式都可以用,只不過第一種使用比較多

2.字符串的本質

字符串的本質是字符數組或者叫做字符序列String類使用final修飾,不可以被繼承使用equals方法比較的是字符數組的每一個位置的值String是一個不可變對象

3.常用方法

char charAt(int index)返回字符串中第index個字符。boolean equals(String other)如果字符串與other相等,返回trueboolean equalsIgnoreCase(String other) 如果字符串與other相等(忽略大小寫),則返回true int indexOf(String str) lastIndexOf(String str,int idx)int length()返回字符串的長度。String replace(char oldChar,char newChar)返回一個新串,它是通過用 newChar 替換此字符串中出現的所有oldChar而生成的boolean startsWith(String prefix)如果字符串以prefix開始,則返回trueboolean endsWith(String prefix) 如果字符串以prefix結尾,則返回trueString substring(int beginIndex)String substring(int beginIndex,int endIndex)返回一個新字符串,該串包含從原始字符串beginIndex到串尾戒endIndex-1的所有字符String toLowerCase()返回一個新字符串,該串將原始字符串中的所有大寫字母改成小寫字母String toUpperCase()返回一個新字符串,該串將原始字符串中的所有小寫字母改成大寫字母String trim()返回一個新字符串,該串刪除了原始字符串頭部和尾部的空格
public class StringDemo {    public static void main(String[] args) {        String str = "abc";        String str2 = new String("abc");//        str2 = str2.intern();        System.out.println(str==str2);        System.out.println(str.equals(str2));        System.out.println(str.charAt(0));                //數組的復制過程        System.out.println(str.concat("cde"));                //返回指定下標的元素        System.out.println(str.indexOf("a"));                String s = "abcdefghijklmn";        System.out.println(s.substring(3));        //在截取字符串的時候,需要注意是左閉右開區間        System.out.println(s.substring(3,5));        System.out.println(s.length());//----------------------------------------------------------//      String a = "abc";//      String b = new String("abc");//      b = b.intern();//      System.out.println(a==b);        String a = "abc";        String b = "def";        String c = "abcdef";        // String d1 = a+b;        // System.out.println(c==d1);//false        String d = (a+b).intern();        String e = "abc"+"def";        System.out.println(c==d);//true        System.out.println(c==e);//true,c,e都是常量,"abcdef"在常量池里只會存在一次,因此地址相同                String f = "a" + "b" +"c";        String a1 = "a";        String a2 = "b";        String a3 = "c";        String f1 = a1+a2+a3;    }}

三.StringBuffer類與StringBuilder類

可變字符串

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);//11.234abctrue        System.out.println(stringBuffer.length());//13        System.out.println(stringBuffer.capacity());//16        StringBuilder stringBuilder = new StringBuilder();        stringBuilder.append("123").append(1).append(false);        System.out.println(stringBuilder);    }}

四.時間處理相關類

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");        //將Date類按照規范轉換為字符串格式        String str = dateFormat.format(date);        System.out.println(str);        //將字符串轉換成對應的日期類        Date d1 = dateFormat.parse("2019-10-9 20:29:56");        System.out.println(d1);        //獲取的是當前系統的時間        Calendar calendar = Calendar.getInstance();        System.out.println(calendar);        //設置指定時間的日歷類        calendar.setTime(d1);        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類

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));//向下取整        System.out.println(Math.pow(2,3));//次方    }}

六.枚舉類

枚舉指由一組固定的常量組成的類型
public enum Gender {    男,女}

1. 只能夠取特定值中的一個

2. 使用enum關鍵字

3. 所有的枚舉類型隱性地繼承自 java.lang.Enum。(枚舉實質上還是類!而每個被枚舉的成員實質就是一個枚舉類型的實例,他們默認都是public static final的。可以直接通過枚舉類型名直接使用它們。)

public enum EventEnum {    LAUNCH("launch"),PAGEVIEW("pageview"),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;        ee.show();        String name = EventEnum.PAGEVIEW.name();        System.out.println(name);    }}

標簽: 基本數據類型 基本類型 枚舉類型

上一篇:觀速訊丨精華推薦 | 【深入淺出 RocketMQ原理及實戰】「底層源碼挖掘系列」透徹剖析貫穿RocketMQ的消費者端的運行核心的流程(上篇)
下一篇:世界新資訊:繞過 Windows Defender (最新版本)