Java初級筆記_常用API

  ------<a href="http://www.itheima.com" target="blank">Java培訓、Android培訓、iOS培訓、.Net培訓</a>、期待與您交流! -------

一、Object類
      (1)是定義在java.lang包下的,是所有類的超類。所有類都直接或者間接的繼承自Object類。
     父類:超類,根類,基類。
     子類:派生類。

(2)要掌握的方法:
      public String toString():返回對象的字符串表示形式。
 默認情況下的組合:類的全路徑名稱+@+對象的哈希值的十六進制表示。
         這種做法對我們來說,一般沒有意義,所以,建議重寫。
  重寫做法:一般是把所有的成員變量組合成一個字符串返回。
         實在不願意自己寫,自動生成。

      public boolean equals(Object obj):比較對象的地址值是否相同。
默認情況下,比較的是對象的地址值是否相同。
 如果有自己的特殊需求,請重寫Object類的該方法。
 怎麼重寫呢?

public boolean equals(Object obj) {
if(this == obj) {
return true;
}
if(!(obj instraceof Student)) {
return false;
}
Student s = (Student) obj;
return this.name.equals(s.name) && this.age == s.age;
}

如果有多個成員變量,把多個比較用&&連接即可。
引用類型用equals()方法。
基本類型用==號即可。
         實在不願意自己寫,自動生成。


(3)面試題:
      ==和equals()的區別?
==:
可以比較基本類型,也可以比較引用類型。
比較基本類型,比較的是值是否相同。
比較引用類型,比較的是地址值是否相同。
equals:
只能比較引用類型。
默認情況下,比較的是地址值是否相同。
如果想比較內容,請自己重寫Object類的equals()方法。
二、Scanner的概述
      (1)Scanner是JDK5以後出現的方便我們從鍵盤接受數據的類。
(2)Scanner的構造格式:
       Scanner sc = new Scanner(System.in);
System.in 是System類下面有一個靜態的成員變量in。它的類型是InputStream。
  代表的是標準鍵盤輸入流。也就是鍵盤錄入數據。
Scanner是對其進行了封裝,提供了各種轉換功能。方便我們獲取到想要的數據類型的數據。

(3)要掌握的兩個功能:
       A:返回int類型
   public int nextInt()
       B:返回String類型
   public String nextLine()
   public String next()
注意事項
   先next,再nextLine會有問題。
   解決方案:
     重新建立Scanner對象。//一般不會這樣做。因爲消耗資源
     根據需求,選擇合適的方法。
     統一一種方法。
三、String類的概述和使用
      (1)由多個字符組成的一串數據。
(2)構造方法:
    A:String s = new String();
    B:String s = new String(byte[] bys);
    C:String s = new String(byte[] bys,int startIndex,int count);
    D:String s = new String(char[] chs);
    E:String s = new String(char[] chs,int startIndex,int count);
    F:String s = new String(String s2);
    G:String s = "hello";
    常使用:
BCDEG
(3)面試題:
    A:字符串一旦被賦值就不能被改動。
注意:這裏的改動指的是字符串的內容,而不是字符串對象的引用。
    B:String s = new String("hello");和String s = "hello";有區別嗎?是什麼呢?
有。
  前者創建了兩個對象。
  後者創建了一個對象。

    C:看程序,寫結果
String s1 = new String("hello");
  String s2 = new String("hello");
  System.out.println(s1==s2);
  System.out.println(s1.equals(s2));

  String s3 = new String("hello");
  String s4 = "hello";
  System.out.println(s3==s4);
  System.out.println(s3.equals(s4));

  String s5 = "hello";
  String s6 = "hello";
  System.out.println(s5==s6);
  System.out.println(s5.equals(s6));

    D:看程序,寫結果
String s7 = "hello";
  String s8 = "world";
  String s9 = "helloworld";
  System.out.println(s9==s7+s8);
  System.out.println(s9=="hello"+"world");

  變量就直接造,常量先找,如果有就使用,否則就造。

(4)字符串的常見功能:(補齊中文)
    A:判斷功能
boolean equals(Object obj)
  boolean equalsIgnoreCase(String str)
  boolean contains(String str)
  boolean startsWith(String str)
  boolean endsWith(String str)
  boolean isEmpty()


    B:獲取功能
int length()
  char charAt(int index)
  int indexOf(int ch) 
  int indexOf(String str);
  int indexOf(int ch,int fromIndex)
  int indexOf(String str,int fromIndex)
  String substring(int start)
  String substring(int start,int end)


    C:轉換功能
byte[] getBytes()  //獲取字符串的字節數組
  char[] toCharArray()  //獲取字符串的字符數組
  static String copyValueOf(char[] chs)
  static String valueOf(char[] chs)
  static String valueOf(int i)
  String toLowerCase()
  String toUpperCase()
  String concat(String str)


    D:其他功能
String replace(char old,char new)
  String replace(String old,String new)

  String[] split(String regex)

  String trim()

  int compareTo(String str)
  int compareToIgnoreCase(String str) 

(5)案例:
    A:遍歷字符串
String s = "hello";

  for(int x=0; x<s.length(); x++) {
System.out.println(s.charAt(x));
  }

    B:統計字符串中大寫字母,小寫字母以及數字字符出現的次數
    C:把一個字符串的首字母變成大寫,其他的全部小寫
    D:統計大串中小串出現的次數

  ------<a href="http://www.itheima.com" target="blank">Java培訓、Android培訓、iOS培訓、.Net培訓</a>、期待與您交流! -------
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章