星空网 > 软件开发 > Java

JavaAPI_01

JavaAPI

  文档注释可以在:类,常量,方法上声明

  文档注释可以被javadoc命令所解析并且根据内容生成手册

JavaAPI_01JavaAPI_01
 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.010  *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: 字符串若使用字面量形式创建对象,会重用以前创建过的内容相同的字符串对象。

  重用常量池中的字符串对象:就是在创建一个字符串对象前,先要到常量池中检查是否这个字符串对象之前已经创建过,如果是就会进行重用,如果否就会重新创建

JavaAPI_01JavaAPI_01
 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 */

字符串对象的重用
JavaAPI_01JavaAPI_01
 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"; //0126     String s5 = "fury";27     String s6 = s5 + "123"; //0228     String s7 = "fury"+123; //0129     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"; //0334     String s12 = "1"+2+"3"+"fury";35     String s13 = 'a'+26+"fury"; //0436     37     System.out.println(s1 == s2); //true38     System.out.println(s1 == s3); //false39     System.out.println(s1 == s4); //true40     System.out.println(s1 == s6); //false41     System.out.println(s1 == s7); //true42     System.out.println(s1 == s8); //true43     System.out.println(s9 == s10); //false44     System.out.println(s9 == s11); //false45     System.out.println(s9 == s12); //true46     System.out.println(s9 == s13); //true47     /*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,即:12368 */

list

  》》字符串长度

    中文、英文字符都是按照一个长度进行计算

JavaAPI_01JavaAPI_01
 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 }

字符串的长度

  》》子串出现的位置

JavaAPI_01JavaAPI_01
 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   }

方法解释
JavaAPI_01JavaAPI_01
 1 package cn.fury.se_day01; 2 /** 3  * int indexOf(String str) 4  * 查看给定字符串在当前字符串中的位置 5  * 首先该方**使用给定的字符串与当前字符串进行全匹配 6  * 当找到位置后,会将给定字符串中第一个字符在当前字符串中的位置返回; 7  * 没有找到就返回 **-1** 8  * 常用来查找关键字使用 9  * @author soft0110  *11 */12 public class StringDemo03 {13   public static void main(String[] args) {14     /*15      * java编程思想: Thinking in Java16     */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)

JavaAPI_01JavaAPI_01
 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.

方法解释
JavaAPI_01JavaAPI_01
 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     //截取oracle16     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 }

方法应用
JavaAPI_01JavaAPI_01
 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()
    去除当前字符串中两边的空白

JavaAPI_01JavaAPI_01
 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.

方法解释
JavaAPI_01JavaAPI_01
 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(); //0113     System.out.println(str2);14     System.out.println(str1 == str2); //0215   }16 }17 18 /*19  * 01 改变了内容,因此创建了新对象,所以02的输出结果为false20 */

方法应用

  》》查看指定位置的字符

    char charAt(int index)

    返回当前字符串中给定位置处对应的字符

JavaAPI_01JavaAPI_01
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 CharSequence9 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.

方法解释
JavaAPI_01JavaAPI_01
 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 }

方法应用
JavaAPI_01JavaAPI_01
 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 }

实际应用
JavaAPI_01JavaAPI_01
 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     }else51     {52       System.out.println("不是回文");53     }54   }55 }

实际应用2_回文数的判断

  》》开始、结束字符串判断

     boolean startsWith(String star)

     boolean endsWith(String str)

    前者是用来判断当前字符串是否是以给定的字符串开始的,
    后者是用来判断当前字符串是否是以给定的字符串结尾的。

JavaAPI_01JavaAPI_01
 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()

    作用:忽略大小写
    应用:验证码的输入

JavaAPI_01JavaAPI_01
 1 package cn.fury.se_day01; 2 /** 3  * 将一个字符串中的英文部分转换为全大写或者全小写 4  * 只对英文部分起作用 5  * String toUpperCase() 6  * String toLowerCase() 7  *  8  * 作用:忽略大小写 9  * 应用:验证码的输入10  * @author soft0111  *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 }

方法应用
JavaAPI_01JavaAPI_01
 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("请输入验证码(F1w3):"); 9     while(true){10       String s1 = sc.nextLine();11       if(s1.toLowerCase().equals("f1w3")){12         System.out.println("验证码输入正确");13         break;14       }else{15         System.out.print("验证码码输入错误,请重新输入:");16       }17     }18   }19 }

实际应用_验证码判断

  》》静态方法valueOf()

    该方法有若干的重载,用来将其他类型数据转换为字符串 ;常用的是将基本类型转换为字符串

JavaAPI_01JavaAPI_01
 1 package cn.fury.test; 2  3 import java.util.Scanner; 4  5 public class Test { 6   public static void main(String[] args) { 7     int x = 123; 8     System.out.println(x); 9     String s1 = "123";10     String s2 = String.valueOf(x);  //0211     String s3 = x + "";  //0112     System.out.println(s1 == s2);13     System.out.println(s1.equals(x));14     System.out.println("===========");15     System.out.println(s1.equals(s3));16     System.out.println(s1.equals(s2));17   }18 }19 20 /**21  * 01 02 的效果是一样的,但是02的速度快22 */

方法应用

》利用StringBuilder进行字符串的修改

  StringBuilder内部维护了一个可变的字符数组,从而保证了无论修改多少次字符串内容,都是在这个数组中完成;当然,若数组内容被超出,会扩容;但是与字符串的修改相比较,内存上的消耗是明显要少很多的。

JavaAPI_01JavaAPI_01
 1 package cn.fury.se_day01; 2 /** 3  * StringBuilder内部维护了一个可变的字符数组 4  * 从而保证了无论修改多少次字符串内容,都是在这个数组中完成 5  * 当然,若数组内容被超出,会扩容;但是与字符串的修改相比较,内存上的消耗是明显要少很多的 6  * 其提供了用于修改字符串内容的常用修改方法: 7  *     增:append 8  *     删:delete 9  *     改:replace10  *     插:insert11  * @author soft0112  *  13 */14 public class StringBuilderDemo01 {15   public static void main(String[] args) {16     System.out.println("===start===");17     String str = "You are my type.";18     System.out.println(str);19     20     /*21      * 若想修改字符串,可以先将其变换为一个22      * StringBuilder类型,然后再改变内容,就不会再创建新的对象啦23     */24     System.out.println("===one===");25     StringBuilder builder = new StringBuilder(str);26     //追加内容27     builder.append("I must stdy hard so as to match you.");28     //获取StringBuilder内部表示的字符串29     System.out.println("===two===");30     str = builder.toString();31     System.out.println(str);32     //替换部分内容33     System.out.println("===three===");34     builder.replace(16, builder.length(),"You are my goddness.");35     str = builder.toString();36     System.out.println(str);37     //删除部分内容38     System.out.println("===four===");39     builder.delete(0, 16);40     str = builder.toString();41     System.out.println(str);42     //插入部分内容43     System.out.println("===five===");44     builder.insert(0, "To be living is to change world.");45     str = builder.toString();46     System.out.println(str);47     48     //翻转字符串49     System.out.println("===six===");50     builder.reverse();51     System.out.println(builder.toString());52     //利用其来判断回文53   }54 }

常用方法应用
JavaAPI_01JavaAPI_01
 1 package cn.fury.se_day01; 2  3 public class StringBuilderDemo02 { 4   public static void main(String[] args) { 5 //    StringBuilder builder = new StringBuilder("a"); //高效率 6 //    for(int i = 0; i < 10000000; i++){ 7 //      builder.append("a"); 8 //    } 9     String str = "a"; //低效率10     for(int i = 0; i < 10000000; i++){11       str += "a";12     }13   }14 }15 16 /*17  * 疑问:字符串变量没改变一下值就会重新创建一个对象吗??18  *       答案:是的,因为字符串对象是不变对象19 */

与字符串连接符“+”的效率值对比

》作业

  生成一个包含所有汉字的字符串,即,编写程序输出所有汉字,每生成50个汉字进行换行输出。在课上案例“测试StringBuilder的append方法“的基础上完成当前案例。

JavaAPI_01JavaAPI_01
 1 package cn.fury.work.day01; 2 /** 3  * 生成一个包含所有汉字的字符串,即,编写程序输出所有汉字,每生成50个汉字进行换 4  * 行输出。在课上案例“测试StringBuilder的delete方法“的基础上完成当前案例。 5  * @author soft01 6  * 7 */ 8 public class Work03 { 9   public static void main(String[] args) {10     StringBuilder builder = new StringBuilder();11     /*12      * 中文范围(unicode编码):13      * \u4e00 ------ \u9fa514     */15     System.out.println(builder.toString());16     System.out.println("=======");17     char chr1 = '\u4e00';18     System.out.println(chr1);19     String str = "\u4e00";20     System.out.println(str);21     22     for(char chr = '\u4e00', i = 1; chr <= '\u9fa5'; chr++,i++){23       builder.append(chr);24       if(i % 50 == 0){25         builder.append("\n");26       }27     }28     29     System.out.println(builder.toString());30   }31 }

View Code

 




原标题:JavaAPI_01

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流