java api都要记住嘛_JavaAPI_01

》JavaAPI

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

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecn.fury.se_day01;2 /**

3 * 文档注释可以在:类,常量,方法上声明4 * 文档注释可以被javadoc命令所解析并且根据内容生成手册5 * 这个类用来测试文档注释6 *@authorsoft017 *@version1.08 *@seejava.lang.String9 *@sincejdk1.010 *11 */

12 public classAPIDemo {13 public static voidmain(String[] args){14 System.out.println(sayHello("fury"));15 }16 /**

17 * 问候语,在sayHello中被使用18 */

19 public static final String INFO = "你好!";20 /**

21 * 将给定的用户名上添加问候语22 *@paramname 给定的用户名23 *@return带有问候语的字符串24 */

25 public staticString sayHello(String name){26 return INFO+name;27 }28 }

测试代码

》字符串是不变对象:字符串对象一旦创建,内容就不可更改

》》字符串对象的重用

**要想改变内容一定会创建新对象**

TIP: 字符串若使用字面量形式创建对象,会重用以前创建过的内容相同的字符串对象。

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecn.fury.test;2

3 public classTest{4 public static voidmain(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 */

字符串对象的重用

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecn.fury.se_day01;2 /**

3 * 字符串是不变对象:字符串对象一旦创建,内容是不可改变的,4 * **要想改变内容一定会创建新对象。**5 *6 * 字符串若使用字面量形式创建对象,会重用以前创建过的内容相同的字符串对象。7 *@authorsoft018 *9 */

10 public classStringDemo01 {11 public static voidmain(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,即:12368 */

list

》》字符串长度

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecn.fury.se_day01;2 /**

3 * int length()4 * 该方法用来获取当前字符串的字符数量,5 * 无论中文还是英文每个字符都是1个长度6 *@authorsoft017 *8 */

9 public classStringDemo02 {10 public static voidmain(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 }

字符串的长度

》》子串出现的位置

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 public intindexOf(String str) {2 return indexOf(str, 0);3 }4

5 /**

6 * Returns the index within this string of the first occurrence of the7 * specified substring, starting at the specified index.8 *9 *

The returned index is the smallest value k for which:10 *

11 * k >= fromIndex {@code&&} this.startsWith(str, k)12 * 
13 * If no such value of k exists, then {@code-1} is returned.14 *15 *@paramstr the substring to search for.16 *@paramfromIndex the index from which to start the search.17 *@returnthe 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, intfromIndex) {22 return indexOf(value, 0, value.length,23 str.value, 0, str.value.length, fromIndex);24 }

方法解释

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecn.fury.se_day01;2 /**

3 * int indexOf(String str)4 * 查看给定字符串在当前字符串中的位置5 * 首先该方法会使用给定的字符串与当前字符串进行全匹配6 * 当找到位置后,会将给定字符串中第一个字符在当前字符串中的位置返回;7 * 没有找到就返回 **-1**8 * 常用来查找关键字使用9 *@authorsoft0110 *11 */

12 public classStringDemo03 {13 public static voidmain(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)

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 Open Declaration String java.lang.String.substring(int beginIndex, intendIndex)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.

方法解释

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecn.fury.se_day01;2 /**

3 * String substring(int start, int end)4 * 截取当前字符串的部分内容5 * 从start处开始,截取到end(但是不含有end对应的字符)6 *7 * java API有个特点,凡是使用两个数字表示范围时,通常都是“含头不含尾”8 *@authorsoft019 *10 */

11 public classStringDemo04 {12 public static voidmain(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 }

方法应用

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecn.fury.test;2

3 importjava.util.Scanner;4

5 /**

6 * 网址域名截取7 *@authorAdministrator8 *9 */

10 public classTest{11 public static voidmain(String[] args) {12 Scanner sc = newScanner(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()

去除当前字符串中两边的空白

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 Open Declaration String java.lang.String.trim()2

3

4 Returns a string whose value is thisstring, 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 thisString 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.

方法解释

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecn.fury.se_day01;2 /**

3 * String trim()4 * 去除当前字符串中两边的空白5 *@authorsoft016 *7 */

8 public classStringDemo06 {9 public static voidmain(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的输出结果为false20 */

方法应用

》》查看指定位置的字符

char charAt(int index)

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 Open Declaration char java.lang.String.charAt(intindex)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 forarray indexing.5

6 If the charvalue 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.

方法解释

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecn.fury.se_day01;2 /**

3 * char charAt(int index)4 * 返回当前字符串中给定位置处对应的字符5 *@authorsoft016 *7 */

8 public classStringDemo07 {9 public static voidmain(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 }

方法应用

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecn.fury.test;2

3 importjava.util.Scanner;4

5 public classTest{6 public static voidmain(String[] args) {7 Scanner sc = newScanner(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 }

实际应用

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecn.fury.se_day01;2 /*

3 * 判断一个字符串是否是回文数:个人觉得利用StringBuilder类中的反转方法reverse()更加简单4 */

5 public classStringDemo08 {6 public static voidmain(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 voidteacherMethod(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 voidmyMethod(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)

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

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecn.fury.se_day01;2 /**

3 * boolean startsWith(String star)4 * boolean endsWith(String str)5 * 前者是用来判断当前字符串是否是以给定的字符串开始的,6 * 后者是用来判断当前字符串是否是以给定的字符串结尾的。7 *@authorsoft018 *9 */

10 public classStringDemo09 {11 public static voidmain(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()

作用:忽略大小写

应用:验证码的输入

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecn.fury.se_day01;2 /**

3 * 将一个字符串中的英文部分转换为全大写或者全小写4 * 只对英文部分起作用5 * String toUpperCase()6 * String toLowerCase()7 *8 * 作用:忽略大小写9 * 应用:验证码的输入10 *@authorsoft0111 *12 */

13 public classStringDemo10 {14 public static voidmain(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 }

方法应用

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecn.fury.test;2

3 importjava.util.Scanner;4

5 public classTest{6 public static voidmain(String[] args) {7 Scanner sc = newScanner(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()

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecn.fury.test;2

3 importjava.util.Scanner;4

5 public classTest {6 public static voidmain(String[] args) {7 int x = 123;8 System.out.println(x);9 String s1 = "123";10 String s2 = String.valueOf(x); //02

11 String s3 = x + ""; //01

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecn.fury.se_day01;2 /**

3 * StringBuilder内部维护了一个可变的字符数组4 * 从而保证了无论修改多少次字符串内容,都是在这个数组中完成5 * 当然,若数组内容被超出,会扩容;但是与字符串的修改相比较,内存上的消耗是明显要少很多的6 * 其提供了用于修改字符串内容的常用修改方法:7 * 增:append8 * 删:delete9 * 改:replace10 * 插:insert11 *@authorsoft0112 *13 */

14 public classStringBuilderDemo01 {15 public static voidmain(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 = newStringBuilder(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 }

常用方法应用

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecn.fury.se_day01;2

3 public classStringBuilderDemo02 {4 public static voidmain(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方法“的基础上完成当前案例。

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecn.fury.work.day01;2 /**

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

8 public classWork03 {9 public static voidmain(String[] args) {10 StringBuilder builder = newStringBuilder();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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值