zoukankan      html  css  js  c++  java
  • Java课堂疑问解答与思考4

    一、

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

    答:定义的三个字符串如果相等,系统自动创建一个,并调用这个,对于由new创建的字符串,是开辟了一个新的空间,当直接使用new关键字创建字符串对象时,虽然值一致(都是“Hello”),但仍然是两个独立的对象空间之间不能直接向比较。

    二、

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

     

    答:对于新开辟的两个空间对象,可以通过系统父类中的equals类直接进行比较。

    编译器在编译s2一句时,会去掉“+”号,直接把两个字串连接起来得一个字串(“Hello”)。这种优化工作由Java编译器自动完成。

    三、

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

    答:

    public boolean equals(Object anObject) {

        if (this == anObject) {

            return true;

        }

        if (anObject instanceof String) {

            String anotherString = (String)anObject;

            int n = count;

            if (n == anotherString.count) {

            char v1[] = value;

            char v2[] = anotherString.value;

            int i = offset;

            int j = anotherString.offset;

            while (n-- != 0) {

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

                return false;

            }

            return true;

            }

        }

        return false;

    }

     四、

    String类的方法可以连续调用:

    String str="abc";

    String result=str.trim().toUpperCase().concat("defg");

    请阅读JDKString类上述方法的源码,模仿其编程方式,编写一个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)//实现data加的方法,并返回对象

        {

           MyCounter a=new MyCounter();

           a.data=data+d;

           return a;

        }

        public MyCounter decrease(int d)// 实现data减的方法,并返回对象

        {

           MyCounter a=new MyCounter();

           a.data=data-d;

           return a;

        }

        public static void main(String[] args)

        {

           MyCounter counter1=new MyCounter(1);//创建对象counter1

           MyCounter counter2=counter1.increase(100).decrease(2).increase(3);//创建对象counter2

           System.out.println(counter2.data);//输出counter对象的data结果

        }

    }

    五、

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

    答:

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

             String s=”dwfsdfwfsadf”;

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

    charAt():public charAt(int index)//index 是字符下标,返回字符串中指定位置的字符

            String s=”Hello”;

            System.out.println(s.charAt(3));

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

            String str = "abcdefghikl";

            Char[] ch = new char[8];

            str.getChars(2,5,ch,0);

    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=new String(“abcd”);// 将字符串对象中的字符转换为一个字符数组

               char myChar[]=x.toCharArray();

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

     

    .

  • 相关阅读:
    解决VsCode中Go插件依赖安装失败问题
    C# httpclient获取cookies实现模拟web登录
    C#中调用HttpWebRequest类中Get/Post请求无故失效的诡异问题
    VisualSVN 5.1.7破译License Key
    AutoResetEvent类的使用
    26种设计模式之单例模式
    WPF的一些感悟
    vim 常用指令
    myeclipse 的.jsp文件中的<option>无法使用
    flume部署问题解决
  • 原文地址:https://www.cnblogs.com/2016-zck/p/7743047.html
Copyright © 2011-2022 走看看