JavaAPI_01

来源:http://www.cnblogs.com/NeverCtrl-C/archive/2016/11/18/6079115.html
-Advertisement-
Play Games

》JavaAPI 文檔註釋可以在:類,常量,方法上聲明 文檔註釋可以被javadoc命令所解析並且根據內容生成手冊 1 package cn.fury.se_day01; 2 /** 3 * 文檔註釋可以在:類,常量,方法上聲明 4 * 文檔註釋可以被javadoc命令所解析並且根據內容生成手冊 5 ...


》JavaAPI

  文檔註釋可以在:類,常量,方法上聲明

  文檔註釋可以被javadoc命令所解析並且根據內容生成手冊

 1 package cn.fury.se_day01;
 2 /**
 3  * 文檔註釋可以在:類,常量,方法上聲明
 4  * 文檔註釋可以被javadoc命令所解析並且根據內容生成手冊
 5  * 這個類用來測試文檔註釋
 6  * @author soft01
 7  * @version 1.0
 8  * @see java.lang.String
 9  * @since jdk1.0
10  *
11  */
12 public class APIDemo {
13     public static void main(String[] args){
14         System.out.println(sayHello("fury"));
15     }
16     /**
17      * 問候語,在sayHello中被使用
18      */
19     public static final String INFO = "你好!";
20     /**
21      * 將給定的用戶名上添加問候語
22      * @param name 給定的用戶名
23      * @return  帶有問候語的字元串
24      */
25     public static String sayHello(String name){
26         return INFO+name;
27     }
28 }
測試代碼

》字元串是不變對象:字元串對象一旦創建,內容就不可更改

  》》字元串對象的重用

  **要想改變內容一定會創建新對象**

  TIP: 字元串若使用字面量形式創建對象,會重用以前創建過的內容相同的字元串對象。

  重用常量池中的字元串對象:就是在創建一個字元串對象前,先要到常量池中檢查是否這個字元串對象之前已經創建過,如果是就會進行重用,如果否就會重新創建

 1 package cn.fury.test;
 2 
 3 public class Test{
 4     public static void main(String[] args) {
 5         String s1 = "123fury"; //01
 6         String s2 = s1; //02
 7         String s3 = "123" + "fury"; //03
 8         String s4 = "warrior";
 9         System.out.println(s1 == s2);
10         System.out.println(s3 == s1);
11         System.out.println(s4 == s1);
12     }
13 }
14 
15 /**
16  * 01 以字面量的形式創建對象:會重用常量池中的字元串對象
17  * 02 賦值運算:是進行的地址操作,所以會重用常量池中的對象
18  * 03 這條語句編譯後是:String s3 = "123fury";
19  */
字元串對象的重用
 1 package cn.fury.se_day01;
 2 /**
 3  * 字元串是不變對象:字元串對象一旦創建,內容是不可改變的,
 4  *         **要想改變內容一定會創建新對象。**
 5  * 
 6  * 字元串若使用字面量形式創建對象,會重用以前創建過的內容相同的字元串對象。
 7  * @author soft01
 8  *
 9  */
10 public class StringDemo01 {
11     public static void main(String[] args) {
12         String s1 = "fury123";
13 //        字面量賦值時會重用對象
14         String s2 = "fury123";
15 //        new創建對象時則不會重用對象
16         String s3 = new String("fury123");
17         /*
18          * java編譯器有一個優化措施,就是:
19          * 若計算表達式運算符兩邊都是字面量,那麼編譯器在生成class文件時
20          * 就會將結果計算完畢並保存到編譯後的class文件中了。
21          * 
22          * 所以下麵的代碼在class文件裡面就是:
23          * String s4 = "fury123";
24          */
25         String s4 = "fury" + "123"; //01
26         String s5 = "fury";
27         String s6 = s5 + "123"; //02
28         String s7 = "fury"+123; //01
29         String s8 = "fu"+"r"+"y"+12+"3";
30         
31         String s9 = "123fury";
32         String s10 = 12+3+"fury";  //編譯後是String s10 = "15fury";
33         String s11 = '1'+2+'3'+"fury"; //03
34         String s12 = "1"+2+"3"+"fury";
35         String s13 = 'a'+26+"fury"; //04
36         
37         System.out.println(s1 == s2); //true
38         System.out.println(s1 == s3); //false
39         System.out.println(s1 == s4); //true
40         System.out.println(s1 == s6); //false
41         System.out.println(s1 == s7); //true
42         System.out.println(s1 == s8); //true
43         System.out.println(s9 == s10); //false
44         System.out.println(s9 == s11); //false
45         System.out.println(s9 == s12); //true
46         System.out.println(s9 == s13); //true
47         /*
48          * 用來驗證03的演算法
49          */
50         int x1 = '1';
51         System.out.println(x1);
52         System.out.println('1' + 1);
53         /*
54          * 用來驗證04的演算法
55          */
56         int x2 = 'a';
57         System.out.println(x2);
58         System.out.println('a' + 26);
59 //        System.out.println(s10);
60     }
61 }
62 
63 /*
64  * 01 編譯完成後:String s4 = "fury123";  因此會重用對象
65  * 02 不是利用字面量形式創建對象,所以不會進行重用對象
66  * 03 '1'+2  的結果不是字元串形式的12,而是字元1所對應的編碼加上2後的值
67  * 04 'a'+26 的結果是字元a所對應的編碼值再加上26,即:123
68  */
list

  》》字元串長度

    中文、英文字元都是按照一個長度進行計算

 1 package cn.fury.se_day01;
 2 /**
 3  * int length()
 4  * 該方法用來獲取當前字元串的字元數量,
 5  * 無論中文還是英文每個字元都是1個長度
 6  * @author soft01
 7  *
 8  */
 9 public class StringDemo02 {
10     public static void main(String[] args) {
11         String str = "hello fury你好Java";
12         System.out.println(str.length());
13         
14         int [] x = new int[3];
15         System.out.println(x.length);
16     }
17 }
字元串的長度

  》》子串出現的位置

 1   public int indexOf(String str) {
 2         return indexOf(str, 0);
 3     }
 4 
 5     /**
 6      * Returns the index within this string of the first occurrence of the
 7      * specified substring, starting at the specified index.
 8      *
 9      * <p>The returned index is the smallest value <i>k</i> for which:
10      * <blockquote><pre>
11      * <i>k</i> &gt;= fromIndex {@code &&} this.startsWith(str, <i>k</i>)
12      * </pre></blockquote>
13      * If no such value of <i>k</i> exists, then {@code -1} is returned.
14      *
15      * @param   str         the substring to search for.
16      * @param   fromIndex   the index from which to start the search.
17      * @return  the index of the first occurrence of the specified substring,
18      *          starting at the specified index,
19      *          or {@code -1} if there is no such occurrence.
20      */
21     public int indexOf(String str, int fromIndex) {
22         return indexOf(value, 0, value.length,
23                 str.value, 0, str.value.length, fromIndex);
24     }
方法解釋
 1 package cn.fury.se_day01;
 2 /**
 3  * int indexOf(String str)
 4  * 查看給定字元串在當前字元串中的位置
 5  * 首先該方法會使用給定的字元串與當前字元串進行全匹配
 6  * 當找到位置後,會將給定字元串中第一個字元在當前字元串中的位置返回;
 7  * 沒有找到就返回  **-1**
 8  * 常用來查找關鍵字使用
 9  * @author soft01
10  *
11  */
12 public class StringDemo03 {
13     public static void main(String[] args) {
14         /*
15          * java編程思想:  Thinking in Java
16          */
17         String str = "thinking in java";
18         int index = str.indexOf("java");
19         System.out.println(index);
20         index = str.indexOf("Java");
21         System.out.println(index);
22         /*
23          * 重載方法:
24          *         從給定位置開始尋找第一次出現給定字元串的位置
25          */
26         index = str.indexOf("in", 3);
27         System.out.println(index);
28         /*
29          * int lastIndexOf(String str)
30          * 返回給定的字元串在當前字元串中最後一次出現的位置
31          */
32         int last = str.lastIndexOf("in");
33         System.out.println(last);
34     }
35 }
方法應用

  》》截取部分字元串

    String substring(int start, int end)

 1   Open Declaration   String java.lang.String.substring(int beginIndex, int endIndex)
 2 
 3 
 4 Returns a string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex. 
 5 
 6 Examples: 
 7 
 8  "hamburger".substring(4, 8) returns "urge"
 9  "smiles".substring(1, 5) returns "mile"
10  
11 Parameters:beginIndex the beginning index, inclusive.endIndex the ending index, exclusive.Returns:the specified substring.Throws:IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.
方法解釋
 1 package cn.fury.se_day01;
 2 /**
 3  * String substring(int start, int end)
 4  * 截取當前字元串的部分內容
 5  * 從start處開始,截取到end(但是不含有end對應的字元)
 6  * 
 7  * java API有個特點,凡是使用兩個數字表示範圍時,通常都是“含頭不含尾”
 8  * @author soft01
 9  *
10  */
11 public class StringDemo04 {
12     public static void main(String[] args) {
13         String str = "www.oracle.com";
14         
15         //截取oracle
16         String sub = str.substring(4, 10);
17         System.out.println(sub);
18         
19         /*
20          * 重載方法,只需要傳入一個參數,從給定的位置開始連續截取到字元串的末尾
21          */
22         sub = str.substring(4);
23         System.out.println(sub);
24     }
25 }
方法應用
 1 package cn.fury.test;
 2 
 3 import java.util.Scanner;
 4 
 5 /**
 6  * 網址功能變數名稱截取
 7  * @author Administrator
 8  *
 9  */
10 public class Test{
11     public static void main(String[] args) {
12         Scanner sc = new Scanner(System.in);
13         System.out.print("請輸入網址:");
14         String s1 = sc.nextLine();
15         int index1 = s1.indexOf(".");
16         int index2 = s1.indexOf(".", index1 + 1);
17         String s2 = s1.substring(index1 + 1, index2);
18         System.out.println("你輸入的網址是:");
19         System.out.println(s1);
20         System.out.println("你輸入的網址的功能變數名稱為:");
21         System.out.println(s2);
22     } 
23 }
實際應用

  》》去除當前字元串中兩邊的空白

    String trim()
    去除當前字元串中兩邊的空白

 1   Open Declaration   String java.lang.String.trim()
 2 
 3 
 4 Returns a string whose value is this string, with any leading and trailing whitespace removed. 
 5 
 6 If this String object represents an empty character sequence, or the first and last characters of character sequence represented by this String object both have codes greater than '\u005Cu0020' (the space character), then a reference to this String object is returned. 
 7 
 8 Otherwise, if there is no character with a code greater than '\u005Cu0020' in the string, then a String object representing an empty string is returned. 
 9 
10 Otherwise, let k be the index of the first character in the string whose code is greater than '\u005Cu0020', and let m be the index of the last character in the string whose code is greater than '\u005Cu0020'. A String object is returned, representing the substring of this string that begins with the character at index k and ends with the character at index m-that is, the result of this.substring(k, m + 1). 
11 
12 This method may be used to trim whitespace (as defined above) from the beginning and end of a string.
13 Returns:A string whose value is this string, with any leading and trailing white space removed, or this string if it has no leading or trailing white space.
方法解釋
 1 package cn.fury.se_day01;
 2 /**
 3  * String trim()
 4  * 去除當前字元串中兩邊的空白
 5  * @author soft01
 6  *
 7  */
 8 public class StringDemo06 {
 9     public static void main(String[] args) {
10         String str1 = "  Keep Calm and Carry on.        ";    
11         System.out.println(str1);
12         String str2 = str1.trim();  //01
13         System.out.println(str2);
14         System.out.println(str1 == str2);  //02
15     }
16 }
17 
18 /*
19  * 01 改變了內容,因此創建了新對象,所以02的輸出結果為false
20  */
方法應用

  》》查看指定位置的字元

    char charAt(int index)

    返回當前字元串中給定位置處對應的字元

1   Open Declaration   char java.lang.String.charAt(int index)
2 
3 
4 Returns the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing. 
5 
6 If the char value specified by the index is a surrogate, the surrogate value is returned.
7 
8 Specified by: charAt(...) in CharSequence
9 Parameters:index the index of the char value.Returns:the char value at the specified index of this string. The first char value is at index 0.Throws:IndexOutOfBoundsException - if the index argument is negative or not less than the length of this string.
方法解釋
 1 package cn.fury.se_day01;
 2 /**
 3  * char charAt(int index)
 4  *  返回當前字元串中給定位置處對應的字元
 5  * @author soft01
 6  *
 7  */
 8 public class StringDemo07 {
 9     public static void main(String[] args) {
10         String str = "Thinking in Java";
11 //        查看第10個字元是什麼?
12         char chr = str.charAt(9);
13         System.out.println(chr);
14         
15         /*
16          * 檢查一個字元串是否為迴文?
17          */
18     }
19 }
方法應用
 1 package cn.fury.test;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Test{
 6     public static void main(String[] args) {
 7         Scanner sc = new Scanner(System.in);
 8         System.out.print("請輸入一個字元串(第三個字元必須是字元W):");
 9         while(true){
10             String s1 = sc.nextLine();
11             if(s1.charAt(2) == 'W'){
12                 System.out.println("輸入正確");
13                 break;
14             }else{
15                 System.out.print("輸入錯誤,請重新輸入。");
16             }
17         }
18     } 
19 }
實際應用
 1 package cn.fury.se_day01;
 2 /*
 3  * 判斷一個字元串是否是迴文數:個人覺得利用StringBuilder類中的反轉方法reverse()更加簡單
 4  */
 5 public class StringDemo08 {
 6     public static void main(String[] args) {
 7         /*
 8          * 上海的自來水來自海上
 9          * 思路:
10          *         正數和倒數位置上的字元都是一致的,就是迴文
11          */
12         String str = "上海自水來自海上";
13         System.out.println(str);
14 //        myMethod(str);
15         teacherMethod(str);
16     }
17 
18     private static void teacherMethod(String str) {
19         for(int i = 0; i < str.length() / 2; i++){
20             if(str.charAt(i) != str.charAt(str.length() - 1 - i)){
21                 System.out.println("不是迴文");
22                 /*
23                  * 方法的返回值類型是void時,可以用return來結束函數
24                  * return有兩個作用
25                  *         1 結束方法
26                  *         2 將結果返回
27                  * 但是若方法返回值為void時,return也是可以單獨使用的,
28                  * 用於結束方法
29                  */
30                 return;
31             }
32         }
33         System.out.println("是迴文");
34     }
35 
36     private static void myMethod(String str) {
37         int j = str.length() - 1;
38         boolean judge = false;
39         for(int i = 0; i <= j; i++){
40             if(str.charAt(i) == str.charAt(j)){
41                 judge = true;
42                 j--;
43             }else{
44                 judge = false;
45                 break;
46             }
47         }
48         if(judge){
49             System.out.println("是迴文");
50         }else
51         {
52             System.out.println("不是迴文");
53         }
54     }
55 }
實際應用2_迴文數的判斷

  》》開始、結束字元串判斷

     boolean startsWith(String star)

     boolean endsWith(String str)

    前者是用來判斷當前字元串是否是以給定的字元串開始的,
    後者是用來判斷當前字元串是否是以給定的字元串結尾的。

 1 package cn.fury.se_day01;
 2 /**
 3  * boolean startsWith(String star)
 4  * boolean endsWith(String str)
 5  * 前者是用來判斷當前字元串是否是以給定的字元串開始的,
 6  * 後者是用來判斷當前字元串是否是以給定的字元串結尾的。
 7  * @author soft01
 8  *
 9  */
10 public class StringDemo09 {
11     public static void main(String[] args) {
12         String str = "thinking in java";
13         System.out.println(str.startsWith("th"));
14         System.out.println(str.endsWith("va"));
15     }
16 }
方法應用

  》》大小寫轉換

    String toUpperCase()

    String toLowerCase()

    作用:忽略大小寫
    應用:驗證碼的輸入

 1 package cn.fury.se_day01;
 2 /**
 3  * 將一個字元串中的英文部分轉換為全大寫或者全小寫
 4  * 只對英文部分起作用
 5  * String toUpperCase()
 6  * String toLowerCase()
 7  * 
 8  * 作用:忽略大小寫
 9  * 應用:驗證碼的輸入
10  * @author soft01
11  *
12  */
13 public class StringDemo10 {
14     public static void main(String[] args) {
15         String str = "Thinking in Java你好";
16         System.out.println(str);
17         System.out.println(str.toUpperCase());
18         System.out.println(str.toLowerCase());
19     }
20 }
方法應用
 1 package cn.fury.test;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Test{
 6     public static void main(String[] args) {
 7         Scanner sc = 	   

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • Nhibernate 4.0 教程 目錄 1. 下載Nhibernate 4.04. 1 2. 入門教程... 2 3. 測試項目詳解... 3 4. 總結... 7 附:關聯知識點... 7 知識點1:C#靜態構造函數... 7 知識點2:關於Visual Studio文件生成操作... 8 前言 ...
  • 繼承的特點:繼承父類的屬性和方法。單繼承(多層繼承)c++里的繼承是多繼承 特性 :方法的覆寫(重寫) java中的繼承和OC中一樣。 比如:人可以養狗; 人 >狗 :整體和部分(擁有)關係。 球隊 >球員 :整體和部分的關係。 代碼中是最常見 has a 的關係 人 >學生 :學生是人 : 說明有 ...
  • php的socket編程算是比較難以理解的東西吧,不過,我們只要理解socket幾個函數之間的關係,以及它們所扮演的角色,那麼理解起來應該不是很難了,在筆者看來,socket編程,其實就是建立一個網路服務的客戶端和服務端,這和mysql的客戶端和服務端是一樣的,你只要理解mysql的客戶端和服務端是 ...
  • 最近在開發一個jsp學生信息管理系統,由於剛剛接觸jsp,遇到問題比較多,特此記錄與大家分享。 Jquery ajax提交表單到servlet示例 前臺部分代碼: ajax提交表單代碼: web.xml配置代碼: addStudents.java代碼(採用POST提交方式): ajax提交表單 通過 ...
  • 從根本上講,Python是一種面向對象的語言。它的類模塊支持多態,操作符重載和多重繼承等高級概念,並且以Python特有的簡潔的語法和類型,OOP十分易於使用。Python的語法簡單,容易上手。 Python程式可以分解成模塊、語句、表達式以及對象。1.程式由模塊構成。2.模塊包含語句。3.語句包含 ...
  • 2.1上傳 2.2解壓jdk #創建文件夾 mkdir /usr/java #解壓 tar -zxvf jdk-7u55-linux-i586.tar.gz -C /usr/java/ 2.3將java添加到環境變數中 vim /etc/profile #在文件最後添加 export JAVA_HO ...
  • 前面幾篇講解瞭如何使用rabbitMq,這一篇主要講解spring集成rabbitmq。 首先引入配置文件org.springframework.amqp,如下 一:配置消費者和生成者公共部分 二:配置生成者 三:生產者程式 其中convertAndSend方法預設第一個參數是交換機名稱,第二個參數 ...
  • 今天我們來探索python中大部分的異常報錯 首先異常是什麼,異常白話解釋就是不正常,程式裡面一般是指程式員輸入的格式不規範,或者需求的參數類型不對應,不全等等。 打個比方很多公司年終送蘋果筆記本,你程式話思維以為是(MAC)電腦筆記本,結果給你個蘋果+筆記本。。。首先類型不對,數量也不對。 先來看 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...