java總結1028/1029

A:  Object類:
Object類是所有類的父類,是類層次結構的根類,所有對象都實現這個類裏的方法(包括數組)
Object類中的方法:
1.public int hashCode()     返回該對象的哈希碼值
通過哈希算法(哈希表hashable)---->地址值(並不是真正的地址值)
Student s1 = new Student() ;
System.out.println(s1.hashCode());//1460821906
2.public final Class getClass()    返回此 Object 的運行時類  (Java反射機制中講!)

Class類(不是class 區分大小寫)中的一個方法
public String getName()以 String 的形式返回此 Class 對象所表示的實體(類、接口、數組類、基本類型或 void)名稱。
返回全路徑名稱:
Class c1 = s1.getClass() ;
String name = c1.getName() ;
System.out.println("name:"+name);//org.westos.object_01.Student :全路徑名稱
//org.westos.object_01.Student
3.public String toString();返回該對象的字符串表示(建議所有子類都重寫此方法)
如果直接輸出對象名稱,想要顯示當前對象的成員變量的值,那麼必須重寫Object類中的toString()方法

4.public boolean equals(Object obj)指示其他某個對象是否與此對象“相等”。
==和equal()方法的區別
==:比較的是兩個對象的地址值是否相同,
equals()方法默認比較的是兩個對象的地址值是否相同,如果重寫了Object類中的equals()方法,那麼默認比較就是兩個對象的內容是否相同

5.protected void finalize()throws Throwable:當垃圾回收器確定不存在對該對象的更多引用時,由對象的垃圾回收器調用此方法,
但是,什麼時候調用垃圾回收器不確定;

System類中的一個方法:
public void gc():運行垃圾回收器,這個垃圾回收器最終調用的就是finalize()方法

6.protected Object clone()創建並返回此對象的一個副本
                throws CloneNotSupportedException
                
注意事項:
Object 類的 clone 方法執行特定的複製操作。首先,如果此對象的類不能實現接口 Cloneable,則會拋出 CloneNotSupportedException。

//重寫Object類中clone()
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
//沒有講clone方法之前:
Student s3 = s1; //把s1地址值賦值s3對象,s3指向s1的堆內存中的地址值


B:  Scanner類:
Scanner:用來創建一個文本掃描器(鍵盤錄入)
java.util.Scanner
Scanner sc = new Sacnner(System.in);
ava高級特性:IO流(後面講)
BufferReder:字符緩衝流來鍵盤錄入

System類中的靜態字段:
public static final InputStream in:  標準輸入流
InputStream :字節流 InputStream is = System.in ;  實質:抽象類多態
public static final OutputStream out:  標準輸出流
 
開發步驟:
1)創建鍵盤錄入對象
2)錄入數據
3)輸出

Scanner類中常用的方法:
  判斷的功能:
  細節:可以添加邏輯判斷
  hasNextXXX(); 在錄入數據之前,加上判斷功能,判斷是否有下一個可以錄入的XXX類型的數據
  nextXXX();//通過錄入獲取這個int類型的數據
  
舉例:
  public boolean hasNextInt()
nextInt(); 
nextInt():錄入int類型的數據
nextLine():錄入一個字符串類型
java.util.InputMismatchException:輸入和想到的數據不匹配
Scanner類中的注意事項:
  先錄入int類型的數據,在錄入String類型數據,第二次錄入的數據沒有接收到,直接輸出結果了,由於"回車"才能接收數據
回車換行符導致的!
  解決方案:
  在第二次錄入String類型數據之前,需要重新創建鍵盤錄入對象錄入String類型


C:  String類:
1.String類:
  代表字符串。Java 程序中的所有字符串字面值(如 "abc" )都作爲此類的實例實現;字符串一旦被賦值,其值不能再改變
  
String類常用的構造方
法:
  String():表示一個空字符序列。
  public String(byte[] bytes,Charset ch):默認字符集(編碼格式):GBK,如果是GBK格式,可以不寫第二個參數
  public String(byte[] bytes,int index,int length):將部分字節數組構造成一個字符串
  public String(char[] value):將字符數組構造成一個字符串
  public String(char[] value,int index,int length):將部分的字符數組構造成一個字符串
  public String(String original):通過字符串常量構造一個字符串對象

獲取字符串的長度功能:
  public int length()
面試題:
  數組中有沒有length(),字符串(字符串緩衝區:StringBuffer)中沒有length(),集合中有沒有length()(集合後面講)?
  數組沒有length(),length屬性
  字符串中有length()
集合中沒有length(),獲取集合中元素數量:size()

編碼和解碼:一定要保證編碼格式一致
  編碼:
  把能看懂的東西轉換成一個看不懂的東西:String----->byte[]:public byte[] getBytes(String charsetName)
  解碼:
  把當前的byte[]轉成能看懂的東西(String):byte[]----->String  :public String(byte[] bytes,CharsetName ch)

字符串的一個特點:一旦被賦值,其值不能被改變(不可變的字符序列)
String類型作爲形式參數和基本數據類型的效果一樣
  
面試題:
String s = "hello"
和String s = new String("hello") 兩個有什麼區別?分別創建了幾個對象
 
第一個創建了一個對象
第二個s創建兩個對象(堆內存中有new String(),然後字符串常量池中也有這樣一個字符串常量(開闢空間的地址))


2.看程序寫結果:
 @author Apple
 
public class StringDemo2 {

public static void main(String[] args) {
//創建字符串對象
String s1 = new String("hello") ;
String s2 = new String("hello") ;

System.out.println(s1==s2);//false

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

String s5 = new String("world") ;
String s6 = "world" ;
System.out.println(s5==s6);
System.out.println(s5.equals(s6));
}
}

結果: false true true false true

3.看程序,寫結果
字符串變量相加,先開闢空間,在相加
字符串常量相加:首先在字符串常量池找,有沒有當前這個常量值,有,就直接返回,沒有,需要創建!
  @author Apple
 
public class StringDemo2 {

public static void main(String[] args) {
String s1 = "hello";  
String s2 = "world";
String s3 = "helloworld";
System.out.println(s3 == s1 + s2);// false
//System.out.println(s3 == (new StringBuilder(String.valueOf(s1))).append(s2).toString());
// s1+s2 ====>new String("helloworld")
System.out.println(s3.equals((s1 + s2)));//true , 


System.out.println(s3 == "hello" + "world");//true
System.out.println(s3.equals("hello" + "world"));//true

/**
* 通過反編譯工具查看第三個輸出語句:
* System.out.println(s3 == "helloworld");
System.out.println(s3.equals("helloworld"));
* */
}
}

4.String類的中常用的判斷功能:
boolean equals(Object obj):當前該對象是否obj這個對象是否相等;
boolean equalsIgnoreCase(String str):比較字符串是否相等,忽略大小寫
boolean contains(String str):判斷str這個字符串是否包含在當前字符串中
boolean startsWith(String str):是否以str子字符串開頭
boolean endsWith(String str):判斷是否以str子字符串結尾
boolean isEmpty():判斷字符串是否爲空

String s = "" ;空字符
String s = " " ;字符串"空格"
String s = null ;當前字符串對象爲空

5.String類的獲取功能:
  int length() :獲取字符串長度功能
char charAt(int index):返回的是索引處對應的字符
int indexOf(int ch):返回指定字符在此字符串中第一次出現處的索引
爲什麼這裏的字符用int來定義: 97 'a'
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):從指定位置開始截取到指定位置結束,包前(start索引)不包後(end索引)

6.String類的轉換功能(重點)
byte[] getBytes():將字符串轉換字節數組
char[] toCharArray():將字符串轉換成 字符數組 (開發中經常使用)
static String valueOf(char[] chs):將字符數組轉換成字符串
static String valueOf(int i):將一個int類型的數據轉換成字符串
注意:
String類中的valueOf()可以將任何數據類型轉換成字符串
String toLowerCase():將字符串全部轉成小寫
String toUpperCase():將字符串全部轉換成大寫
String concat(String str):字符串拼接方法

7.方法嵌套
 Math.max(Math.max(a,b),c);
 方法遞歸:方法調用本身的一種現象
三個條件:
1)需要定義個方法
2)方法必須有出口條件
3)必須有某一種規律
   public void show(int n){
 
if(n<0){
  System.exit(0) ; //讓jvm退出,程序結束
  }
 
  System.out.println(n) ;
  show(--n) ;

8. String類中的其他功能:
替換功能:
public String replace(char oldChar,char newChar):將字符串中某一個字符用新的字符替換
public String replace(String oldStr,String newStr):將字符串中某一個子字符串用新 的字符串去替代
去除字符串兩端(中間的不管)空格:
public String trim()
兩個字符串進行比較:
public int compareTo(String anotherString)  是Comparable接口中的方法(該接口可以實現一個自然排序)
 Comparator接口可以比較器排序
在API源碼中,String的compareTo其實就是一次比較兩個字符串的ASCII碼。
如果兩個字符串的ASCII相等則繼續後續比較,否則直接返回兩個ASCII的差值。
如果兩個字符串完全一樣,則返回0.
9.面試題:
class B{
public B(){
array = new ArrayList<String>() ;
}

//成員變量的位置
public static B t1 = new B() ;
public static B t2 = new B() ;

{
System.out.println("構造代碼塊");
}

static{
System.out.println("靜態代碼塊!");
}
}
public class Demo {
public static void main(String[] args) {

B b = new B() ;//構 構 靜 構

//靜態代碼塊 ,構造代碼塊,構造方法的優先級問題:
//靜態代碼塊>構造代碼塊>構造方法...
}


D: StringBuffer類:
1.StringBuffer:
線程(多線程中講)
線程安全---->同步---->執行效率低!
舉例:
銀行的網站,醫院的平臺
StringBuilder
線程不安全--->不同步--->執行效率高
舉例:
一些論壇網站,相親網站..
在實際開發中,線程不安全可能會造成死鎖的現象!
線程安全和執行效率是相對的,並且也是困擾開發者的因素!
 StringBuffer:線程安全的可變字符序列
2.StringBuffer的構造方法:
public StringBuffer ()構造一個其中不帶字符的字符串緩衝區,其初始容量爲 16 個字符
public StringBuffer(int capacity)構造一個不帶字符,但具有指定初始容量的字符串緩衝區
public StringBuffer(String str)
構造一個字符串緩衝區,並將其內容初始化爲指定的字符串內容。該字符串的初始容量爲 16 加上字符串參數的長度。
 
3.常用的功能:
public int length():獲取字符串長度數
public int capacity():獲取當前字符串緩衝區的容量 //默認:初始容量16 + 當前字符串長度

4.StringBuffer中的和添加有關的方法
public StringBuffer append(int/String/char/boolean/double/float....):當前這個方法追加,給緩衝中追加數據,返回字符串緩衝區本身(經常使用的)
public StringBuffer insert(int offset,String str):在某一個位置處去插入str這個字符串,返回字符串緩衝區本身


5.StringBuffer的刪除功能:
public StringBuffer deleteCharAt(int index):刪除指定位置處的字符,返回的是字符串緩衝區本身!
public StringBuffer delete(int start,int end):刪除從指定位置開始到指定位置結束的字符,返回的是字符串緩衝區本身!


6. StringBuffer的反轉功能:
public StringBuffer reverse():將此字符串中的字符序列直接反轉


7. StringBuffer的替換功能:
public StringBuffer replace(int start, int end,String str)
從指定位置開始到指定位置結束的字符用str子字符串去替代
8. StringBuffer的截取功能:
public String substring(int start):從指定位置默認截取到末尾,返回值是一個新的字符串
public String substring(int start,int end):從指定位置開始截取到指定位置結束,包前不包後,返回一個新的字符串


9. 面試題:
String,StringBuffer,StringBuilder的區別?

String:一個不可變的字符序列,StringBuffer:和StringBuilder是可變的字符序列,在單個線程時候的時候(優先採用StringBuilder)
從線程角度考慮:
StringBuilder線程不安全,不同步,執行效率高!它比StringBuffer要快!

String和StringBuffer作爲形式參數
 
String類型作爲形式參數和基本數據類型作爲形式參數的效果一樣!
public class StringBufferDemo7 {

public static void main(String[] args) {

//定義兩個字符串
String s1 = "hello" ;
String s2 = "world" ;
System.out.println(s1+"---"+s2);
change(s1,s2) ;
System.out.println(s1+"---"+s2);

//定義兩個字符串緩衝區對象
StringBuffer sb1 = new StringBuffer("hello") ;
StringBuffer sb2 = new StringBuffer("world") ;
System.out.println(sb1+"---"+sb2);//hello---world
change(sb1,sb2);
System.out.println(sb1+"---"+sb2);//hello---worldworld
//緩衝區是不變
}

public static void change(StringBuffer sb1 ,StringBuffer sb2){
sb1 = sb2 ;  //緩衝區不變"world"
sb2.append(sb1) ;//worldworld
}

public static void change(String s1,String s2){
s1 = s2 ;
s2 = s1 + s2 ;
}
}


10. 類與類之間的轉換
A類型--->B類型
這裏面可能最終使用B類型裏面的功能
B類型--->A類型
有時候需要的不是B類型,所以又要將B類型-->A類型
StringBuffer--->String之間的相互轉換
//定義一個字符串
String s = "hello" ;
//方式1)使用StringBuffer的構造方式:StringBuffer(String str)
StringBuffer sb = new StringBuffer(s) ;
方式2)創建字符串緩衝區對象,使用無參構造StringBuffer(),利用append()
StringBuffer sb2 = new StringBuffer() ;
sb2.append(s) ;
StringBuffer--->String
//有一個字符串緩衝區對象
StringBuffer buffer = new StringBuffer("world") ;
//1)方式1String的另一種構造方法:String(StringBuffer buffer)
String s2 = new String(buffer) ;
System.out.println("s2:"+s2);
//2)方式2:/public String toString():
//現在的數據類型StirngBuffer類型,需要將StringBuffer類型轉換String
String s3 = buffer.toString() ;
System.out.println("s3:"+s3);
面試題:
StringBuffer和數組的區別?

兩個共同特點:都屬於容器類型的變量
數組:只能存儲同一種數據類型的元素,數組的長度是固定的
int[] arr = {10,20,40,50,"hello"}  錯誤的
StringBuffer:字符串緩衝區,可以存儲任意類型的元素,可以不斷的去給緩衝區中追加(append),字符串緩衝區中:在內存始終返回的字符串


E:  Integer類: 
1.Integer
需求:求一個整數100對應的二進制,八進制,十六進制
   需求:要求出Integer類型範圍:
 
   java的jkd5.0以後:還有一些新特性:自動拆裝箱,可變參數,增強for循環,靜態導入,枚舉...
 
   對於每個基本數據類型都會被自動封裝成一個引用類型
基本類型 引用類型
int Integer
char Character
byte Byte
boolean Boolean
double Double
float Float
long Long
short Short
 
   將基本類型--->引用類型的作用:就是爲了和String類型作爲轉換
System.out.println(Integer.MAX_VALUE);//2147483647
System.out.println(Integer.MIN_VALUE);//-2147483647


2.Integer類的構造方式:
public Integer(int value):將一個int類型的數據封裝成一個引用類型
public Integer(String s):將一個字符數類型封裝成一個Integer類型
注意事項:
該字符串必須是數字字符串!,否則:java.lang.NumberFormatException
3.int類型和String類型的相互轉換
/定義一個int類型的數據
int number = 100 ;

//方式1)字符串拼接符
String s1 = "" + number ;
System.out.println("s1:"+s1);


//方式2:int--->Integer類型
Integer i = new Integer(number) ;
//Integer--->String
//public String toString()
String s2 = i.toString() ;
//定義一個字符串:
String s = "10" ;
//方式1:String--->Integer---->int
Integer ii = new Integer(s) ;
//public int intValue()以 int 類型返回該 Integer 的值
int num = ii.intValue() ;
System.out.println("num:"+num);
//public static int parseInt(String s)
int num2 = Integer.parseInt(s) ;
System.out.println("num2:"+num2);

//Integer的內存緩存區:IntegerCashe
//low = -128
//high=127
Integer i1 = 127 ; //----->底層在封裝的時候:Integer integer = Inteter.valueOf(int i) ;
Integer i2 = 127 ;

System.out.println(i1==i2);//true
Integer i3 = 128 ;
Integer i4 = 128 ;
System.out.println(i3==i4);//false

F: character類:
1.Character 類在對象中包裝一個基本類型 char 的值。Character 類型的對象包含類型爲 char 的單個字段
構造方法:
public Character(char value)構造一個新分配的 Character 對象,用以表示指定的 char 值。 

2.Character類的判斷功能:
public static boolean isLowerCase(char ch)確定指定字符是否爲小寫字母。
public static boolenn isUpperCase(char ch)確定指定字符是否爲大寫字母
public static boolean isDigit(char ch)確定指定字符是否爲數字。


3.Character常用的轉換功能:
public static char toUpperCase(char ch):將指定字符轉換成大寫
public static char toLowerCase(char ch):將指定字符轉換成小寫

G: 冒泡排序:

冒泡排 序的思想:
兩兩比較,大的往後放,第一次比完,最大值出現在最大索引處,依次進行這樣的比較....

 int[] arr = {13,24,57,69 ,80} ;
 
冒泡排序的代碼
public static void bubbleSort(int[] arr){
for(int x = 0 ; x < arr.length-1; x ++){
for(int y = 0 ; y < arr.length-1-x ; y++){
//判斷
if(arr[y]>arr[y+1]){
int temp = arr[y] ;
arr[y] = arr[y+1] ;
arr[y+1] = temp ;
}
}
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章