课程作业04-汇总整理

04-String

动手动脑问题和课后实验性问题总结

一、请运行以下示例代码,查看其输出结果。如何解释这样的输出结果?从中你能总结出什么?

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中,内容相同的字串常量(“Hello”)只保存一份以节约内存,所以s0s1s2实际上引用的是同一个对象。编译器在编译s2一句时,会去掉“+”号,直接把两个字串连接起来得一个字串(“Hello”)。这种优化工作由Java编译器自动完成。当直接使用new关键字创建字符串对象时,虽然值一致(都是“Hello”),但仍然是两个独立的对象。

 

二、为什么会有上述的输出结果?从中你又能总结出什么?

public class StringPool {

public static void main(String args[])

    {

        String s1="a";

        String s2=s1;

        System.out.println(s1==s2);

        s1+="b";

        System.out.println(s1==s2);

        System.out.println(s1=="ab");

        System.out.println(s1.equals("ab"));

    }

}

 

原因分析:

给字串变量赋值意味着:两个变量(s1,s2)现在引用同一个字符串对象“a”!

String对象的内容是只读的,使用“+”修改s1变量的值,实际上是得到了一个新的字符串对象,其内容为“ab”,它与原先s1所引用的对象”a”无关,所以,s1==s2返回false;

代码中的“ab”字符串是一个常量,它所引用的字符串与s1所引用的“ab”对象无关。

String.equals()方法可以比较两个字符串的内容。

使用equals()或equalsIgnoreCase()方法比较两字串内容是否相同,使用==比较两字串变量是否引用同一字串对象。

 

三、请查看String.equals()方法的实现代码,注意学习其实现方法。

public boolean equals(Object anObject)

{

if(this == anObject)

{

return true;

}

if(anObject instanceof String)

{

String anotherString=(String) anObject;

int n=value.length;

if(n == anotherString.value.length)

{

char v1[]=value;

char v2[]=anotherString.value;

int i=0;

while(n-- != 0)

{

if(v1[i] != v2[i])

return false;

i++;

}

return true;

}

}

return false;

}

 

四、整理String类的Length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArray()使用说明

length():

public int length();//求字符串长度

String s=”abcdefg”;

System.out.println(s.length());

 

charAt():

String s=new String(“abcdefg”);

//取字符串中的某一个字符。字符串的序数从0开始到length()-1.

System.out.println(“s.charAt(2):”+s.charAt(2));

 

getChars()

public int getChars();//将字符从此字符串复制到目标字符数组

String str=”abcdefghijk”;

Char[] ch=new char[8];

Str.gerChars(2,5,ch,0);

 

replace()

Public int replace();//替换字符串

String s=”abc”;

System.out.println(s.replace(“abc”,”def”));

 

toUpperCase()

Public String toUpperCase()//将字符串全部转换成大写格式

System.out.println(new String(“abcdef”).toUpperCase());

 

toLowerCase()

Public String toLowerCase()//将字符串全部转换成小写
System.out.println(new String(“ABCDEF”).toLowerCase());

 

trim()

Public String trim();//去掉两边的空格

String s=” a aa”;

System.out.println(s.trim());

 

toCharArray()

String x=” abcdefg”;//将字符串对象中的字符转换为一个字符数组

Char myChar[]=x.toCharArray();

System.out.println(“myChar[1]”+myChar[1]);

五、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 MyCounter {

int data;

public MyCounter(int data)

{

this.data=data;

}

public MyCounter()

{

}

public MyCounter Increase(int d)

{

MyCounter m=new MyCounter();

m.data=data+d;

return m;

}

public MyCounter Decrease(int d)

{

MyCounter m=new MyCounter();

m.data=data-d;

return m;

}

public static void main(String[] args)

{

MyCounter counter1=new MyCounter(1);

MyCounter counter2=counter1.Increase(100).Decrease(2).Increase(3);

System.out.println(counter2.data);

}

}

posted @ 2017-10-26 15:41  博呀客  阅读(168)  评论(0编辑  收藏  举报