zoukankan      html  css  js  c++  java
  • 字符串

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

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

    作业二:再看……

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

    结论:给字串变量赋值意味着:两个变量(s1s2)现在引用同一个字符串对象“a”
    String对象的内容是只读的,使用“+”修改s1变量的值,实际上是得到了一个新的字符串对象,其内容为“ab”,它与原先s1所引用的对象”a”无关,所以,s1==s2返回false
    代码中的“ab”字符串是一个常量,它所引用的字符串与s1所引用的“ab”对象无关。
    String.equals()方法可以比较两个字符串的内容。

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

    1)程序代码:

    public class StringEquals {
          
        public static void main(String[] args) {
           
            String s1=new String("Hello");
           
            String s2=new String("Hello");
          
            System.out.println(s1==s2);
           
            System.out.println(s1.equals(s2));
           
            String s3="Hello";
           
            String s4="Hello";
             
            System.out.println(s3==s4);
           
            System.out.println(s3.equals(s4));
              
        }
    }

    运行结果截图:

    课后作业四: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 String1 {

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            String str="abc";
            String result=str.trim().toUpperCase().concat("defg");
            System.out.println(result);
        }

    }

    程序源代码:

    class MyCounter
    {
        int data;
        public void set(int d)
        {
            data=d;
        }
        public MyCounter increase(int i)
        {
            MyCounter a=new MyCounter();
            a.data=data+i;
            return a;
        }
        public MyCounter decrease(int d)
        {
            MyCounter a=new MyCounter();
            a.data=data-d;
            return a;
        }
    }
    public class Counter {

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            MyCounter counter1=new MyCounter();
            MyCounter counter2=new MyCounter();
            counter1.set(1);
            counter2=counter1.increase(100).decrease(2).increase(3);
            System.out.println(counter2.data);
        }
    }

    运行结果截图:、

    课后作业五:五.课后作业之字串加密、动手动脑之String.equals()方法、整理String类的Length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArray()使用说明。

    1.String.equals()方法:比较的是两个字符串的内容,与之区分的是“==”(判断对象的值和地址)。
    2.Length():获取字符串的长度。
    3.charAt():获取指定位置的字符(其中下标从0开始)。
    4. getChars():获取从指定位置起的子串复制到字符数组中。
    5.replace():子串替换。
    6.toUpperCase():全部字符变为大写,返回新字符串。
    7. toLowerCase():全部字符变为小写,返回新字符串。
    8.trim():去除头尾空格。
    9.toCharArray():将字符串对象转换为字符数组。

  • 相关阅读:
    操作系统典型调度算法
    C++ volatile 关键字
    vue class绑定 组件
    yarn 基本用法
    vscode 插件安装以及首选项配置
    git 多人协作
    git Feature分支
    git Bug分支
    git 分支策略
    git 解决冲突
  • 原文地址:https://www.cnblogs.com/kangy123/p/6007371.html
Copyright © 2011-2022 走看看