01API概念
* A:API(Application Programming Interface)
* 應用程序編程接口
* B:Java API
* 就是Java提供給我們使用的類,這些類將底層的實現封裝了起來,
* 我們不需要關心這些類是如何實現的,只需要學習這些類如何使用。
* C: 演示查看Object類中的相關方法
02Object類概述
* A:Object類概述
* 類層次結構的根類
* 所有類都直接或者間接的繼承自該類
* Object中描述的所有方法子類都可以使用
* 所有類在創建對象的時候,最終找的父類就是Object。
* B:構造方法
* public Object()
* 回想面向對象中為什么說:
* 子類的構造方法默認訪問的是父類的無參構造方法
03equals方法比較內存地址
* A:equals方法比較內存地址
* a: Object類中的equals方法
* 用于比較兩個對象是否相同,Object類中就是使用兩個對象的內存地址在比較。
* Object類中的equals方法內部使用的就是==比較運算符。
* b: 案例代碼
public class Person extends Object{
private String name;
private int age;
public Person(){}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
/*
* 將父類的equals方法寫過來,重寫父類的方法
* 但是,不改變父類方法的源代碼, 方法equals 比較兩個對象的內存地址
*
*/
public boolean equals(Object obj){
return this == obj;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
//測試代碼
public class TestEquals {
public static void main(String[] args) {
//Person類繼承Object類,繼承下來了父類的方法equals
Person p1 = new Person("李四",20);
Person p2 = new Person("張三",20);
//Person對象p1,調用父類的方法equals,進行對象的比較
boolean b = p1.equals(p1);
System.out.println(b);
}
}
04重寫equals方法
* A: 重寫equals方法
* a: 開發中要比較兩個對象是否相同,經常會根據對象中的屬性值進行比較
* b: 在開發經常需要子類重寫equals方法根據對象的屬性值進行比較。
* c: ==號和equals方法的區別
* ==是一個比較運算符號,既可以比較基本數據類型,也可以比較引用數據類型,基本數據類型比較的是值,引用數據類型比較的是地址值
* equals方法是一個方法,只能比較引用數據類型,所有的對象都會繼承Object類中的方法,如果沒有重寫Object類中的equals方法,
equals方法和==號比較引用數據類型無區別,重寫后的equals方法比較的是對象中的屬性
* d: 案例代碼
public class Person extends Object{
private String name;
private int age;
public Person(){}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
/*
* 重寫父類的方法toString()
* 沒有必要讓調用者看到內存地址
* 要求: 方法中,返回類中所有成員變量的值
*/
public String toString(){
return name + age;
}
/*
* 將父類的equals方法寫過來,重寫父類的方法
* 但是,不改變父類方法的源代碼, 方法equals 比較兩個對象的內存地址
*
* 兩個對象,比較地址,沒有意義
* 比較兩個對象的成員變量,age
* 兩個對象變量age相同,返回true,不同返回false
*
* 重寫父類的equals,自己定義自己對象的比較方式
*/
public boolean equals(Object obj){
if( this == obj){
return true;
}
//對參數obj,非null判斷
if( obj == null){
return false;
}
if( obj instanceof Person){
// 參數obj接受到是Person對象,才能轉型
// 對obj參數進行類型的向下轉型,obj轉成Person類型
Person p = (Person)obj;
return this.age == p.age;
}
return false;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
//測試代碼
public class TestEquals {
public static void main(String[] args) {
//Person類繼承Object類,繼承下來了父類的方法equals
Person p1 = new Person("李四",20);
Person p2 = new Person("張三",20);
//Person對象p1,調用父類的方法equals,進行對象的比較
boolean b = p1.equals(p1);
System.out.println(b);
}
}
05重寫toString方法
* A: 重寫toString方法
* a: 為什么要重寫toString方法
* toString方法返回該對象的字符串表示,其實該字符串內容就是對象的類型+@+內存地址值。
* 由于toString方法返回的結果是內存地址,而在開發中,經常需要按照對象的屬性得到相應的字符串表現形式,因此也需要重寫它。
* Object類中的toString的核心代碼
getClass().getName() + "@" + Integer.toHexString(hashCode())
* 由于默認情況下的數據對我們來說沒有意義,一般建議重寫該方法。
* b: 案例核心代碼(重寫Person類中的toString方法)
/*
* 重寫父類的方法toString()
* 沒有必要讓調用者看到內存地址
* 要求: 方法中,返回類中所有成員變量的值
*/
public String toString(){
return name + age;
}
//Eclipse中自動生成的toString
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
//測試代碼
public class TestToString {
public static void main(String[] args) {
//調用Person類的方法toString()
//輸出語句中,寫的是一個對象,默認調用對象的toString方法
Person p = new Person("張三",20);
String s = p.toString();
System.out.println(p);
System.out.println(s);
/*
* System.out.println(p);
* System.out.println(p.toString());
*/
/*Random r = new Random();
System.out.println(r.toString());
Scanner sc = new Scanner(System.in);
System.out.println(sc.toString());*/
}
}
========================================================第二節課開始===================================================
06String類的概念和不變性
* A: String類的概念和不變性
* a:String類
* API中的String類的描述,發現String 類代表字符串
* Java 程序中的所有字符串字面值(如 "abc" )都作為此類的實例實現。
* 字符串是常量,在創建之后不能更改
* 其實就是說一旦這個字符串確定了,那么就會在內存區域中就生成了這個字符串。字符串本身不能改變,但str變量中記錄的地址值是可以改變的。
* 源碼分析,String類底層采用的是字符數組:
private final char value[]
private 修飾說明value只能在String類內部使用,而且又沒有提供get方法,所以外部無法獲取value數組,就無法改變數組中元素的值
final修飾說明value是常量,一旦創建,就不能被改變,value一旦被初始化成某個數組,將永遠指向這個數組,不可能再指向其它的數組了
* b: 案例代碼
/*
* String類特點:
* 一切都是對象,字符串事物 "" 也是對象
* 類是描述事物,String類,描述字符串對象的類
* 所有的 "" 都是String類的對象
*
* 字符串是一個常量,一旦創建,不能改變
*/
public class StringDemo {
public static void main(String[] args) {
//引用變量str執行內存變化
//定義好的字符串對象,不變
String str = "itcast";
System.out.println(str);
str = "itheima";
System.out.println(str);
}
}
07String類創建方式和比較
* A: String類創建方式和比較
* a: 創建對象的數量比較
* String s3 = "abc";
* 在內存中只有一個對象。這個對象在字符串常量池中
? * String s4 = new String("abc");
* 在內存中有兩個對象。一個new的對象在堆中,一個字符串本身對象,在字符串常量池中
* b: 案例代碼
public class StringDemo2 {
public static void main(String[] args) {
//字符串定義方式2個, 直接= 使用String類的構造方法
String str1 = new String("abc");
String str2 = "abc";
System.out.println(str1);
System.out.println(str2);
System.out.println(str1==str2);//引用數據類型,比較對象的地址 false
System.out.println(str1.equals(str2));//true
}
}
08String類構造方法
* A: String類構造方法
* a: 常見構造方法
* public String():空構造
* public String(byte[] bytes):把字節數組轉成字符串
* public String(byte[] bytes,int index,int length):把字節數組的一部分轉成字符串
* public String(String original):把字符串常量值轉成字符串
* b: 案例代碼
public class StringDemo3 {
public static void main(String[] args) {
function_1();
}
/*
* 定義方法,String類的構造方法
* String(byte[] bytes) 傳遞字節數組
* 字節數組轉成字符串
* 通過使用平臺的默認字符集解碼指定的 byte 數組,構造一個新的 String。
* 平臺 : 機器操作系統
* 默認字符集: 操作系統中的默認編碼表, 默認編碼表GBK
* 將字節數組中的每個字節,查詢了編碼表,得到的結果
* 字節是負數,漢字的字節編碼就是負數, 默認編碼表 ,一個漢字采用2個字節表示
*
* String(byte[] bytes, int offset, int length) 傳遞字節數組
* 字節數組的一部分轉成字符串
* offset 數組的起始的索引
* length 個數,轉幾個 , 不是結束的索引
*/
public static void function(){
byte[] bytes = {97,98,99,100};
//調用String類的構造方法,傳遞字節數組
String s = new String(bytes);
System.out.println(s);
byte[] bytes1 ={65,66,67,68,69};
//調用String構造方法,傳遞數組,傳遞2個int值
String s1 = new String(bytes1,1,3);
System.out.println(s1);
}
}
09String類構造方法_2
* A: String類構造方法
* a: 常見構造方法
* public String(char[] value):把字符數組轉成字符串
* public String(char[] value,int index,int count):把字符數組的一部分轉成字符串
* B: 案例代碼
/*
* String類構造方法
* String類的構造方法,重載形式
*
*/
public class StringDemo3 {
public static void main(String[] args) {
function_1();
}
/*
* String(char[] value) 傳遞字符數組
* 將字符數組,轉成字符串, 字符數組的參數,不查詢編碼表
*
* String(char[] value, int offset, int count) 傳遞字符數組
* 將字符數組的一部分轉成字符串
* offset 數組開始索引
* count 個數
*/
public static void function_1(){
char[] ch = {'a','b','c','d','e','f'};
//調用String構造方法,傳遞字符數組
String s = new String(ch);
System.out.println(s);
String s1 = new String(ch,1,4);
System.out.println(s1);
}
}
10String類的其他方法
* A:String類的其他方法
* a: 方法介紹
* int length(): 返回字符串的長度
* String substring(int beginIndex,int endIndex): 獲取字符串的一部分
* String substring(int beginIndex): 獲取字符串的一部分
* boolean startsWith(String prefix): 判斷一個字符串是不是另一個字符串的前綴,開頭
* boolean endsWith(String prefix): 判斷一個字符串是不是另一個字符串的后綴,結尾
* boolean contains (String s): 判斷一個字符串中,是否包含另一個字符串
* int indexOf(char ch): 查找一個字符,在字符串中第一次出現的索引,被查找的字符不存在,返回-1
* byte[] getBytes(): 將字符串轉成字節數組,此功能和String構造方法相反,byte數組相關的功能,查詢編碼表
* char[] toCharArray(): 將字符串轉成字符數組,功能和構造方法相反
* boolean equals(Object obj): 方法傳遞字符串,判斷字符串中的字符是否完全相同,如果完全相同返回true
* boolean equalsIgnoreCase(String s): 傳遞字符串,判斷字符串中的字符是否相同,忽略大小寫
* b: 案例代碼
public class StringDemo4 {
public static void main(String[] args) {
function_9();
}
/*
* boolean equals(Object obj)
* 方法傳遞字符串,判斷字符串中的字符是否完全相同,如果完全相同返回true
*
* boolean equalsIgnoreCase(String s)
* 傳遞字符串,判斷字符串中的字符是否相同,忽略大小寫
*/
public static void function_9(){
String str1 = "Abc";
String str2 = "abc";
//分別調用equals和equalsIgnoreCase
boolean b1 = str1.equals(str2);
boolean b2 = str1.equalsIgnoreCase(str2);
System.out.println(b1);
System.out.println(b2);
}
/*
* char[] toCharArray() 將字符串轉成字符數組
* 功能和構造方法相反
*/
public static void function_8(){
String str = "itcast";
//調用String類的方法toCharArray()
char[] ch = str.toCharArray();
for(int i = 0 ; i < ch.length ; i++){
System.out.println(ch[i]);
}
}
/*
* byte[] getBytes() 將字符串轉成字節數組
* 此功能和String構造方法相反
* byte數組相關的功能,查詢編碼表
*/
public static void function_7(){
String str = "abc";
//調用String類方法getBytes字符串轉成字節數組
byte[] bytes = str.getBytes();
for(int i = 0 ; i < bytes.length ; i++){
System.out.println(bytes[i]);
}
}
/*
* int indexOf(char ch)
* 查找一個字符,在字符串中第一次出現的索引
* 被查找的字符不存在,返回-1
*/
public static void function_6(){
String str = "itcast.cn";
//調用String類的方法indexOf
int index = str.indexOf('x');
System.out.println(index);
}
/*
* boolean contains (String s)
* 判斷一個字符串中,是否包含另一個字符串
*/
public static void function_5(){
String str = "itcast.cn";
//調用String類的方法contains
boolean b =str.contains("ac");
System.out.println(b);
}
/*
* boolean endsWith(String prefix)
* 判斷一個字符串是不是另一個字符串的后綴,結尾
* Demo.java
* .java
*/
public static void function_4(){
String str = "Demo.java";
//調用String類方法endsWith
boolean b = str.endsWith(".java");
System.out.println(b);
}
/*
* boolean startsWith(String prefix)
* 判斷一個字符串是不是另一個字符串的前綴,開頭
* howareyou
* hOw
*/
public static void function_3(){
String str = "howareyou";
//調用String類的方法startsWith
boolean b = str.startsWith("hOw");
System.out.println(b);
}
/*
* String substring(int beginIndex,int endIndex) 獲取字符串的一部分
* 返回新的字符串
* 包含頭,不包含尾巴
*
* String substring(int beginIndex)獲取字符串的一部分
* 包含頭,后面的字符全要
*/
public static void function_2(){
String str = "howareyou";
//調用String類方法substring獲取字符串一部分
str= str.substring(1, 5);
System.out.println(str);
String str2 = "HelloWorld";
str2 = str2.substring(1);
System.out.println(str2);
}
/*
* int length() 返回字符串的長度
* 包含多少個字符
*/
public static void function(){
String str = "cfxdf#$REFewfrt54GT";
//調用String類方法length,獲取字符串長度
int length = str.length();
System.out.println(length);
}
}
11String類練習
* A: 獲取指定字符串中,大寫字母、小寫字母、數字的個數
* a: 題目分析
* 為了統計大寫字母、小寫字母、數字的個數。創建3個計數的變量。
* 為了獲取到字符串中的每個字符,進行字符串的遍歷,得到每個字符。
* 對得到的字符進行判斷,如果該字符為大寫字母,則大寫字母個數+1;如果該字符為小寫字母,則小寫字母個數+1;如果該字符為數字,則數字個數+1。
* 顯示大寫字母、小寫字母、數字的個數
* b: 解題步驟
* 略
* 案例代碼
public class StringTest {
public static void main(String[] args) {
getCount("A%A3eBr1FFy");
}
/*
* 獲取指定字符串中,大寫字母、小寫字母、數字的個數。
* 思想:
* 1. 計數器,就是int變量,滿足一個條件 ++
* 2. 遍歷字符串, 長度方法length() + charAt() 遍歷
* 3. 字符判斷是大寫,是小寫,還是數字
*/
public static void getCount(String str){
//定義三個變量,計數
int upper = 0;
int lower = 0;
int digit = 0;
//對字符串遍歷
for(int i = 0 ; i < str.length() ; i++){
//String方法charAt,索引,獲取字符
char c = str.charAt(i);
//利用編碼表 65-90 97-122 48-57
if(c >='A' && c <=90){
upper++;
}else if( c >= 97 && c <= 122){
lower++;
}else if( c >= 48 && c <='9'){
digit++;
}
}
System.out.println(upper);
System.out.println(lower);
System.out.println(digit);
}
}
12String類練習_2
* A: 將字符串中,第一個字母轉換成大寫,其他字母轉換成小寫,并打印改變后的字符串。
* a: 題目分析
* 把字符串分為兩個部分,第一部分為字符串中第一個字母,第二部分為剩下的字符串。
* 把第一部分字符串轉換成大寫字母,把第二部分字符串轉換成小寫字母
* 把兩部分字符串連接在一起,得到一個完整的字符串
* b: 解題步驟
* 略
* C: 案例代碼
public class StringTest {
public static void main(String[] args) {
System.out.println(toConvert("aBc5%4dEF"));
}
/*
* 將字符串的首字母轉成大寫,其他內容轉成小寫
* 思想:
* 獲取首字母, charAt(0) substring(0,1)
* 轉成大寫 toUpperCase()
*
* 獲取剩余字符串, substring(1) toLowerCase()
*/
public static String toConvert(String str){
//定義變量,保存首字母,和剩余字符
String first = str.substring(0,1);
String after = str.substring(1);
//調用String類方法,大寫,小寫轉換
first = first.toUpperCase();
after = after.toLowerCase();
return first+after;
}
}
13String類練習_3
* A: 查詢大字符串中,出現指定小字符串的次數
* a: 題目分析
* 在大串中,查找小串出現的位置,出現了就次數+1
* 在上次小串出現位置的后面繼續查找,需要更改大串的內容為上次未查詢到的字符串。
* 回到第一步,繼續查找小串出現的位置,直到大串中查詢不到小串為止
* b: 解題步驟
* 略
* C: 案例代碼
package cn.itcast.demo02;
public class StringTest {
public static void main(String[] args) {
System.out.println(getStringCount("hellojava,nijavahaojava,javazhenbang", "java"));
}
/*
* 獲取一個字符串中,另一個字符串出現的次數
* 思想:
* 1. indexOf到字符串中到第一次出現的索引
* 2. 找到的索引+被找字符串長度,截取字符串
* 3. 計數器++
*/
public static int getStringCount(String str, String key){
//定義計數器
int count = 0;
//定義變量,保存indexOf查找后的索引的結果
int index = 0;
//開始循環找,條件,indexOf==-1 字符串沒有了
while(( index = str.indexOf(key) )!= -1){
count++;
//獲取到的索引,和字符串長度求和,截取字符串
str = str.substring(index+key.length());
}
return count;
}
}
======================================================================第三節課開始=========================================================
14StringBuffer特點可變字符數組
* A:StringBuffer類概述
* 通過JDK提供的API,查看StringBuffer類的說明
* 線程安全的可變字符序列
* 底層采用字符數組實現,初始容量為16
* B:StringBuffer和String的區別
* String是一個不可變的字符序列
* StringBuffer是一個可變的字符序列
15StringBuffer類的方法
* A: StringBuffer類的方法
* a: 方法介紹
* StringBuffer append(), 將任意類型的數據,添加緩沖區
* append 返回值,寫return this
* 調用者是誰,返回值就是誰
* delete(int start,int end): 刪除緩沖區中字符
* 開始索引包含,結尾索引不包含
* insert(int index, 任意類型): 將任意類型數據,插入到緩沖區的指定索引上
* replace(int start,int end, String str): 將指定的索引范圍內的所有字符,替換成新的字符串
* reverse(): 將緩沖區中的字符反轉
* String toString(): 繼承Object,重寫toString()
* 將緩沖區中的所有字符,變成字符串
* b: 案例代碼
public class StringBufferDemo {
public static void main(String[] args) {
function_5();
}
/*
* StringBuffer類的方法
* String toString() 繼承Object,重寫toString()
* 將緩沖區中的所有字符,變成字符串
*/
public static void function_5(){
StringBuffer buffer = new StringBuffer();
buffer.append("abcdef");
buffer.append(12345);
//將可變的字符串緩沖區對象,變成了不可變String對象
String s = buffer.toString();
System.out.println(s);
}
/*
* StringBuffer類的方法
* reverse() 將緩沖區中的字符反轉
*/
public static void function_4(){
StringBuffer buffer = new StringBuffer();
buffer.append("abcdef");
buffer.reverse();
System.out.println(buffer);
}
/*
* StringBuffer類方法
* replace(int start,int end, String str)
* 將指定的索引范圍內的所有字符,替換成新的字符串
*/
public static void function_3(){
StringBuffer buffer = new StringBuffer();
buffer.append("abcdef");
buffer.replace(1, 4, "Q");
System.out.println(buffer);
}
/*
* StringBuffer類方法 insert
* insert(int index, 任意類型)
* 將任意類型數據,插入到緩沖區的指定索引上
*/
public static void function_2(){
StringBuffer buffer = new StringBuffer();
buffer.append("abcdef");
buffer.insert(3, 9.5);
System.out.println(buffer);
}
/*
* StringBuffer類方法
* delete(int start,int end) 刪除緩沖區中字符
* 開始索引包含,結尾索引不包含
*/
public static void function_1(){
StringBuffer buffer = new StringBuffer();
buffer.append("abcdef");
buffer.delete(1,5);
System.out.println(buffer);
}
/*
* StringBuffer類方法
* StringBuffer append, 將任意類型的數據,添加緩沖區
* append 返回值,寫return this
* 調用者是誰,返回值就是誰
*/
public static void function(){
StringBuffer buffer = new StringBuffer();
//調用StringBuffer方法append向緩沖區追加內容
buffer.append(6).append(false).append('a').append(1.5);
System.out.println(buffer);
}
}
16StringBuilder類
* A:StringBuilder的概述
* 通過查看API了解一下StringBuilder類
* B:面試題
* String,StringBuffer,StringBuilder的區別
* StringBuffer和StringBuilder的區別
* StringBuffer是jdk1.0版本的,是線程安全的,效率低
* StringBuilder是jdk1.5版本的,是線程不安全的,效率高
* String和StringBuffer,StringBuilder的區別
* String是一個不可變的字符序列
* StringBuffer,StringBuilder是可變的字符序列
17StringBuffer類案例拼接數組
* A: StringBuffer類案例拼接數組
* a: 題目分析
* 定義StringBuffer對象
* 遍歷數組,按照格式要求拼接處新的字符串,追加到StringBuffer容器中
* 將StringBuffer中的內容以String的形式返回
* b: 解題步驟
* 略
* C: 案例代碼
public class StringBufferTest {
public static void main(String[] args) {
int[] arr = {4,1,4,56,7,8,76};
System.out.println(toString(arr));
}
/*
* int[] arr = {34,12,89,68};將一個int[]中元素轉成字符串
* 格式 [34,12,89,68]
* String s = "["
* 數組遍歷
* s+= arr[i];
* s+"]"
* StringBuffer實現,節約內存空間, String + 在緩沖區中,append方法
*/
public static String toString(int[] arr){
//創建字符串緩沖區
StringBuffer buffer = new StringBuffer();
buffer.append("[");
//數組遍歷
for(int i = 0 ; i < arr.length;i++){
//判斷是不是數組的最后一個元素
if(i == arr.length-1){
buffer.append(arr[i]).append("]");
}else{
buffer.append(arr[i]).append(",");
}
}
return buffer.toString();
}
}
18總結
- 把今天的知識點總結一遍。