JAVA學習第二十八課(常用對象API)- String類,apistring

來源:互聯網
上載者:User

JAVA學習第二十八課(常用對象API)- String類,apistring

多線程告一段落,開始常用對象API的涉及,背也要背下來!!!

日後開發,遇見最多的對象是文字,也就是字串

String類

字串是一個特殊對象

字串一旦初始化就不可以被改變


一、特點

public class Main {public static void main(String[] args){Demo1();System.out.println("--------------");Demo2();}/*示範字串的第一種定義方式,並明確字串常量池的特點*/private static void Demo1(){String s1 = "asd";s1 = "sd";//s1隻是字串的引用,而字串“asd”本身不變String s2 = new String("sdf");System.out.println(s1);s2 = "sd";System.out.println(s2==s1);//s1 和 s2實際上指向的同一個地址值//sd儲存在緩衝區(字串常量池)//如果沒有,就建立,沒有就不建立}/*示範字串的第二種定義方式,*/public static void Demo2() {String s = "abc";//在字串常量池中建立一個對象String s1 = new String("abc");//沒有在字串常量池中建立,在堆記憶體建立對象兩個對象//一個是s1 一個“abc”System.out.println(s == s1);//false,比較的地址值不一樣System.out.println(s.equals(s1));//true/*String類中的equals複寫了Object中的equals建立了String類 * 自己的判斷字串對象是否相同 * 也就是比較字串內容*/System.out.println("s = "+s);System.out.println("s1 = "+s1);}}

二、建構函式

public class Main{public static void main(String[] args){StringconstructorDemo1();charDemo();}public static void StringconstructorDemo1(){String s = new String(); // 等效於String s = "";地址值不一樣,不等效String s = nullbyte[] sub = {97,66};String s1 = new String(sub);System.out.println("s1 = "+s1);}public static void charDemo(){char[] ch = {'a','b','c','d','e','f'};String string = new String(ch);//將數組變成字串,只能是byte 和 charSystem.out.println("string = "+string);String string1 = new String(ch,0,3);//從0角標開是截取3個字元System.out.println("string1 = "+string1);//String類剩下的方法,查手冊}

未完。。。。




java中的類超級無敵多,我是一個新手,現在學起來覺得很吃力,我要怎學習java中的API類庫才更有效率?

敲代碼,我從0開始自學Java
就是從helloworld這樣的小程式,一點一點親手敲代碼,
做記事本程式時,對照的API一個一個查,看API的方法說明,自己編程。
敲了無數的代碼,現在可以直接找到API中的類和方法了。
 
Java中StringBuilder常用API,Integer類常用API,Double類常用API分別有什?

到baidu上搜"Java JDK API 1.6.0 中文版文檔 " 把api下載下來吧,什麼都有。
學java必須要學會看api,不然老跟別人後面走,呵呵。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.