java中的常用类-1
Java API
API(應用程序編程接口)
java語言中提供了很多的類和接口
但是一般所說的api是指api文檔,是對java預定定義的類類或接口功能和函數功能的說明文檔,目的是提供給開發人員進行使用幫助說明。
基本數據類型包裝類
為什么要有基本數據類型包裝類?
基本數據類型是通過八個關鍵字聲明,結構簡單。但是基本數據類型卻是不面向對象的,這在實際使用中存在許多的不便。為解決這個問題,在設計類時為每個基本數據類型設計了一個對應的類進行代表,這些基本數據類型對應的類統稱為基本數據包裝類。
包裝類:這些類封裝了一個對應的基本數據類型數值,并為其提供了一系列操作方法。
基本數據類型 對應的基本數據類型包裝類
byte Byte
short Short
char Character
int Integer
long Long
float Float
double Double
boolean Boolean
包裝類的用途主要有兩種:
1.作為和基本數據類型相對應的類類型存在
2.包含每種基本數據類型的相關屬性如最大值,最小值等,以及相關的操作方法。
基本數據類型分裝類中的常用方法
java.lang.Interger
public class use1 {public static void main(String[] args) { ?Integer a =new Integer(10);Integer b =new Integer("100"); ?System.out.println(Integer.MAX_VALUE);//打印int類型數值的最大值System.out.println(Integer.MIN_VALUE);//打印int類型數值的最小值 ?System.out.println(Integer.SIZE); //打印System.out.println(Integer.BYTES); //打印 ?System.out.println(Integer.max(50,20));//比較兩個int類型數值的大小,并打印兩者中較大的數的數值System.out.println(Integer.min(50,20));//比較兩個int類型數值的大小,并打印兩者中較小的數的數值 ?System.out.println(Integer.toHexString(1000));//將傳入的int類型的數值以16進制打印System.out.println(Integer.toOctalString(100));//將傳入的int類型的數值以8進制打印System.out.println(Integer.toBinaryString(10));//將傳入的int類型的數值以2進制打印System.out.println(a.compareTo(b));//比較兩個對象中包含的基本數據類型值的大小,返回 -1,0,1。/*-1 后面的大0 二者一樣大1 前面的大*/?} }裝箱和拆箱
裝箱:
自動將基本數據類型轉換為包裝器類型
裝箱的時候自動調用的是Integer的valueOf(int)方法
拆箱:
自動將包裝器類型轉換為基本數據類型
拆箱的時候自動調用的是Integer的intValue方法
public class use1 {public static void main(String[] args) { ?Integer a =new Integer(10);Integer b =new Integer("100");//將String類型包裝為intInteger d =Integer.valueOf(40);int aa =a.intValue(); ?int c =Integer.parseInt("20");/*自動裝箱和自動拆箱*/Integer x =new Integer(10);int y =x;//自動拆箱,把包裝類轉換為類型x.intValue()System.out.println(x);Integer z =x;//自動裝箱,把基本類型轉為包裝類型Integer.valueOf(y)/*裝箱和拆箱*/int ?a1 =10;Integer b1 =Integer.valueOf(a);//裝箱int c =b1.intValue();//拆箱} } ?==與equal
public class use2 {public static void main(String[] args) {// == 比較基本類型時,比較 值 是否相等int x =128;int y =128;System.out.println(x==y);//true ? ?/*自動裝箱Integer a =Integer.valueOf(128);Integer b =Integer.valueOf(128);*//* Integer a =127;Integer b =127;//== 比較引用類型時,比較的是兩個引用所指向的對象地址是否相同System.out.println(a==b);//true ?*/Integer a =128;//Integer.valueOf(127), 大于107,小于-128 就回new新的對象Integer b =128;System.out.println(a==b);//true ? ?Integer i =new Integer(10);Integer ii =new Integer(10);System.out.println(i==ii);//false ?System.out.println(a.equals(b));//比較的是對象中的值是否相等 ? ? ?} }Object類
Object類 是所有類的父類,是多態實現的基礎,
其中的方法 toString();將xx轉為字符串形式 Arrays.toString(a)
public class use3 extends Object {public static void main(String[] args) { ?Car c = new Car("寶馬", 300000);/*要對對象進行輸出,默認會調用類中的toString()返回對象的字符串表示形式。Car類中沒有toString(),會去調用父類中toString()public String toString() { ? ? ? ? 將對象內存地址(哈希值),轉為16進制return getClass().getName() + "@" + Integer.toHexString(hashCode());}*/System.out.println(c);//com.ffyc.javaapi.obj.Car@1b6d3586 ? ?} } ?所以,需要使用Object類中的方法時,如果忘記了重寫方法,那會調用原來類中的方法。
@Overridepublic boolean equals(Object obj) {if(obj instanceof Car){Car otherCar = (Car)obj;return this.name.equals(otherCar.name) && this.price==otherCar.price;}return false;} ? ? ? ?Car c = new Car("寶馬", 30000);Car c1 = new Car("寶馬", 30000);/*public boolean equals(Object obj) {return (this == obj);}*/System.out.println(c.equals(c1));//true //如果為重寫之前就使用 false 原因是Car類中沒有重寫equals(),調用Object類中的方法 ?Arrays類
java.util.Arrays類
用來操作數組的類,里面定義了常見操作數組的方法。
public class use3 {public static void main(String[] args) {int []a =new int[]{1,2,3,4,5}; ?int []b =new int[]{1,2,3,4,5};System.out.println(Arrays.equals(a,b));//equals(a,b),傳入兩個數組,比較兩個數組內容是否相等System.out.println(Arrays.toString(a));System.out.println(Arrays.toString(b));//toString(a),將傳入的數組的內容以字符串的形式輸出 ?int []c =new int[]{3,2,4,1,5};Arrays.sort(c,2,4);//開始位置(包含),結尾位置(不包含)Arrays.sort(c); ? ? ? ? //引用傳遞System.out.println(Arrays.toString(c)); ?user1 s1 =new user1("alice",1);user1 s2 =new user1("bob",2);user1 s3 =new user1("charli",3);user1 s4 =new user1("dog",4);user1 s5 =new user1("easy",5); ?System.out.println(s1.toString());System.out.println(s2.toString());//需要在子類中重寫父類Object中的toString方法 ?user1 []a1 =new user1[5];a1[0]=s3;a1[1]=s4;a1[2]=s1;a1[3]=s2;a1[4]=s5;//想要輸出user1類型的數組的內容,如果不重寫toString方法的話,不能達到效果Arrays.sort(a1);//對于引用類,需要特殊處理。Error!,.ClassCastException,數據類型轉換錯誤,需要重寫方法/*在引用類中實現Comparable<user1>,重寫compareTo方法,*/ ?System.out.println(Arrays.toString(a1));} } public class user1 implements Comparable<user1>{String name;int no; ?public user1(String name, int no) {this.name = name;this.no = no;}@Overridepublic String toString() {return "user1{" +"name='" + name + '\'' +", no=" + no +'}';}@Overridepublic int compareTo(user1 o) {return ?this.no-o.no;} } ?二分查找
public class use4 {public static void main(String[] args) {int []a =new int[]{5,3,4,2,1,7,6};Arrays.sort(a);int index = Arrays.binarySearch(a,1);System.out.println(index);//index如果不是負數,說明找到了。/*二分查詢:要有一個有序的列表,以升序數列為例,比較一個元素與數列中的中間位置的元素的大小,如果比中間位置的元素大,則繼續在后半部分的數列中進行二分查找;如果比中間位置的元素小,則在數列的前半部分進行比較;如果相等,則找到了元素的位置。每次比較的數列長度都會是之前數列的一半,直到找到相等元素的位置或者最終沒有找到要找的元素。*///如果key在數組中,則返回搜索值的索引;否則返回-1或者”-“(插入點)} }數組擴容
public class use5 {public static void main(String[] args) {int []a =new int[]{1,2,3,4,5};int []b = Arrays.copyOf(a,7);//數組擴容/*數組:數組的大小一旦給定,那就無法修改。如果想要數組擴容,需要重新定義一個數組。然后將之前的數組內容賦給新的數組。*/} }String類
public class use6 {public static void main(String[] args) {/*java中所有的字符串"",都可以視為String類的實例,String對象的值一旦定義無法更改,因為底層是使用char數組存儲的*/String s ="abc";s += "de"; //第一個字符串對象System.out.println(s); //第二個字符串對象/*創建字符串有兩種方式第一種:String s ="abcd",隱式的創建了一個字符串對象第二種:通過new創建*/String s1 ="acd";String a ="acd";System.out.println(a==s1);//trueSystem.out.println(s1.equals(a));//true/*隱式創建:字符串對象直接在字符串常量池中存儲,如果值相同,就直接返回字符串常量池中的對象地址 */String s2 =new String();String b =new String("abcd");String s3 =new String("abcd");System.out.println(b==s3);//false} } String s =new String();//""String s1 =new String("abcd");// public class use7 {public static void main(String[] args) {String s =new String();String s1 =new String("abcd");String s2 =new String("abcd");System.out.println(s1.equals(s2));//比較兩個字符串內容的值是否相等String s3 =new String("Abcd");System.out.println(s1.equalsIgnoreCase(s3));//比較兩個字符串內容,并且對于大小寫字母的區別進行忽略System.out.println(s3.contains("Ab"));//是否包含指定的子串/*String s =""; 與 null是有區別的*/System.out.println(s.isEmpty());//判斷字符串是否為空System.out.println(s1.startsWith("ab"));//判斷字符串是否以ab開頭System.out.println(s1.endsWith("cd"));//判斷字符串是否以cd結尾} } public class use8 {public static void main(String[] args) {String s ="abcdea";System.out.println(s.length());//返回字符串長度System.out.println(s.charAt(1));//返回指定位置上的字符串System.out.println(s.indexOf("c"));//獲取指定字符串首次出現的位置 從后向前System.out.println(s.indexOf("c",3));//獲取指定字符串首次出現的位置 從指定位置開始找System.out.println(s.lastIndexOf("c"));//從指定的位置開始截取一個新的子串到結尾System.out.println(s.substring(2));//從指定的位置開始截取一個到指定位置結尾的子串} }總結
以上是生活随笔為你收集整理的java中的常用类-1的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java医院管理系统(his)源码免费分
- 下一篇: 计算机网络安全-----Internet