1、请运行以下示例代码StringPool.java,查看其输出结果。如何解释这样的输出结果?从中你能总结出什么?
public class StringPool {
public static void main(String args[])
{
String s0="Hello";
String s1="Hello";
String s2="He"+"llo";
System.out.println(s0==s1);//true
System.out.println(s0==s2);//true
System.out.println(new String("Hello")==new String("Hello"));//false
}
}
在java中+是连接符,编译器编译s2时会去掉+,s0,s1,s2指向同一个对象,所以出现true,而后来新创建的两个地址不同,所以false。
String s1 = "a";
String s2 = s1;
System.out.println(s1==s2); //true
s1+="b";
System.out.println(s1==s2);//false
System.out.println(s1=="ab");//false
System.out.println(s1.equals("ab"));//true
两个变量(s1,s2)现在引用同一个字符串对象“a”! String对象的内容是只读的,使用“+”修改s1变量的值,实际上是得到了一个新的字符串对象,其内容为“ab”,它与原先s1所引用的对象”a”无关,所以,s1==s2返回false; 代码中的“ab”字符串是一个常量,它所引用的字符串与s1所引用的“ab”对象无关。 String.equals()方法可以比较两个字符串的内容。
2.请查看String.equals()方法的实现代码,注意学习其实现方法。
当对象不同,内容相同,"=="返回false,equals返回true
当同一对象,"=="和equals结果相同
如果值不相同,对象就不相同,所以"==" 和equals结果一样
public class StringPool {
public static void main(String args[])
{
String s0="Hello";
String s1="Hello";
String s2="He"+"llo";
System.out.println(s0==s1);
System.out.println(s0==s2);
String S3 = new String("Hello");
String S4=new String("Hello");
System.out.println(S3.equals(S4));
}
}
可用s3.equals(s4)来实现。
3.String类的方法可以连续调用: String str="abc"; String result=str.trim().toUpperCase().concat("defg"); 请阅读JDK中String类上述方法的源码,模仿其编程方式,编写一个MyCounter类,它的方法也支持上述的“级联”调用特性,其调用示例为: MyCounter counter1=new MyCounter(1); MyCounter counter2=counter1.increase(100).decrease(2).increase(3); ….
public class StringPool {
public static void main(String args[])
{
String str="abc";
String result=str.trim().toUpperCase().concat("defg");
System.out.println(result);
}
}
4、字串加密
import java.util.Scanner;
public class Testfive {
public static void main(String[]args){
System.out.println("请输入任意字串");
Scanner in=new Scanner(System.in);//读入字符串
String s1=null;
s1=in.nextLine();
char a[]=new char[s1.length()];
s1.getChars(0,s1.length(),a,0);//获取从指定位置起的子串复制到字符数组中
char b[]=new char[s1.length()];
for(int i=0;i<s1.length();i++)
{
b[i]=(char) (a[i]+3);
}
for(int i=0;i<s1.length();i++)
{
System.out.print(b[i]);
}
}
}
程序设计思想:
在键盘上输入任意字符串,然后先创建一个与输入字符串同等长度的数组,然后将字符串复制到此数组中,再重新创建一个同等长度的数组用来储存加密后的字符,然后把加密后的字符输出即可。
程序流程图:
5、整理String类的Length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArray()使用说明、阅读笔记发表到博客园。
1)length():public int length()//求字符串长度
String s=”abcdefg”;
System.out.println(s.length());
charAt():public charAt(int index)//index 是字符下标,返回字符串中指定位置的字符
String s=”Hello”;
System.out.println(s.charAt(3));
getChars():public int getChars()//将字符从此字符串复制到目标字符数组
replace():public int replace()//替换字符串
String s=”\\\”;
System.out.println(s.replace(“\\\”,”///”));
结果///;
toUpperase():public String toUpperCase()//将字符串全部转换成大写
System.out.println(new String(“hello”).toUpperCase());
toLowerCse():public String toLowerCase()//将字符串全部转换成小写
System.out.println(new String(“HELLO”).toLowerCase());
trim():public String trim()
String x=”ax c”;
System.out.println(x.trim());//是去两边空格的方法
toCharArray(): String x=”abcd”;// 将字符串对象中的字符转换为一个字符数组
char myChar[]=x.toCharArray();
System.out.println(“myChar[1]”+myChar[1]);