安庆网站建设,四川做网站的公司有哪些,网站备案号格式说明书,温州app软件开发JAVA把String类定义为final类(因此用户不能扩展String类#xff0c;即String类不可以有子类) String对象可以用进行并置运算 identityHashCode会返回对象的hashCode#xff0c;而不管对象是否重写了hashCode方法。
public class Example8_1 {public static void…JAVA把String类定义为final类(因此用户不能扩展String类即String类不可以有子类) String对象可以用进行并置运算 identityHashCode会返回对象的hashCode而不管对象是否重写了hashCode方法。
public class Example8_1 {public static void main(String args[]) {String hello 你好;String testOne 你好; //【代码1】int address System.identityHashCode(你好);System.out.printf(\你好\的引用:%x\n,address);address System.identityHashCode(hello);System.out.printf(hello的引用:%x\n,address);address System.identityHashCode(testOne);System.out.printf(testOne的引用:%x\n,address);System.out.println(hello testOne); //输出结果是trueSystem.out.println(你好 testOne); //输出结果是trueSystem.out.println(你好 hello); //输出结果是trueString you 你;String hi 好;String testTwo youhi; //【代码2】address System.identityHashCode(你);System.out.printf(\你\的引用:%x\n,address);address System.identityHashCode(好);System.out.printf(\好\的引用:%x\n,address);address System.identityHashCode(testTwo);System.out.printf(testTwo的引用:%x\n,address);System.out.println(hello testTwo); //输出结果是false}} String类中的方法 1.public int length():用来获取一个String对象的字符序列
int numbers1.length();2.public boolean equals(String s)用于比较当前String 对象的字符序列是否与s指定的String对象的字符序列相同
System.out.println(s1.equals(s2));3.public boolean startsWith(String s) 判断当前String对象的字符串序列的前缀是否是参数指定的String对象s的字符序列 public boolean endsWith(String s)
4.public int compareTo(String s) 按照字典序与参数指定的String对象S的字符串序列比较大小
单词的排序 用Array类自带的方法 Arrays.sort(b); b里面有所有的单词组成的大字符串
package lg;import java.util.*;
public class Example8_3 {public static void main(String args[]) {String [] a{melon,apple,pear,banana};String [] b{西瓜,苹果,梨,香蕉};System.out.println(使用SortString类的方法按字典序排列数组a:);for(int i0;ia.length;i) {System.out.print( a[i]);}System.out.println( );SortString.sort(a);for(int i0;ia.length;i) {System.out.print( a[i]);}System.out.println();System.out.println(使用类库中的Arrays类按字典序排列数组b:); Arrays.sort(b);for(int i0;ib.length;i) {System.out.print( b[i]);}}
}
public class SortString {public static void sort(String a[]) {int count0; for(int i0;ia.length-1;i) {int saveMinIndex i;for(int ji1;ja.length;j) { if(a[j].compareTo(a[saveMinIndex])0) {saveMinIndex j;}} String tempa[i];a[i]a[saveMinIndex];a[saveMinIndex]temp;}}
} 5.public boolean contains(String s) 判断当前String的对象字符序列是否包含参数S的字符序列 6.public int indexOf(String s) 从当前String对象的字符序列的0索引位置开始检索首次出现str的字符序列的位置并返回位置 public int lastIndexOf(String s) 从当前String对象的字符序列的0索引位置开始检索最后一次出现str的字符序列的位置并返回位置
找不到返回-1
String tom I am a good cat;
tom.indexOf(a);//值是2
tom.lastIndexOf(a);//值是137.public String substring(int startpoint) substring(int startpoint)方法是字符串对象调用该方法获得一个当前字符串的子串该子串是从当前字符串的startpoint处截取到最后所得的字符串注字符串的起始位置是从0开始的截取的时候startpoint位置的字符也被截取
substring(int start,int end) 方法是获取一个当前字符串的子串该子串是通过复制当前字符串start到end-1位置上的字符串注字符串的起始位置是从0开始 8。public char charAt(int index) charAt()方法是用来输出一个字符串中的单个字符例如 String s “hello world”; system.out.println(s.charAt(1)); 输出的结果就为e