zoukankan      html  css  js  c++  java
  • 04-课堂作业总结归纳

    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

    }

    }

    第一个so==s1和第二个s0==s2比较的都是字符串的内容(java语言定义“+”运算符可用于两个字符串的连接操作),所以是true。第三个用new创建了两个对象,当直接使用new关键字创建字符串对象时,虽然值一致(都是“Hello”),但仍然是两个独立的对象。

    2、

     

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

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

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

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

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

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

    源代码:

    public class StringEquals {

    /**

         * @param args the command line arguments

         */

    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));

    }

    }

     

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

    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

    {public static void main(String[]  args)

    {  String s="aqz";

    String result=s.trim().toUpperCase().concat("qwe");

            System.out.println(result);

        }

    }

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

    Length():获取字串长度

    charAt():获取指定位置的字符

    getChars():获取从指定位置起的子串复制到字符数组中

    replace():子串替换

    toUpperCase() toLowerCase():大小写转换

    trim():去除头尾空格

    toCharArray():将字符串对象转换为字符数组

     

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

             String s=”dwfs”;

             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   s= "abc";

            Char[] ch = new char[8];

            str.getChars(2,3,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=” a  bc ”;

             System.out.println(x.trim());/

    toCharArray(): String x=”abcd”;// 将字符串对象中的字符转换为一个字符数组

               char myChar[]=x.toCharArray();

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

     

  • 相关阅读:
    Python三维绘图--Matplotlib colorbar生成
    Python三维绘图--Matplotlib
    VIM剪切板的使用
    三维点云网络PointNet——模型及代码分析
    ECCV2018--点云匹配
    hdu 1905 小数化分数2
    hdu 1755 A Number Puzzle
    hdu 1796 How many integers can you find
    hdu 1452 Happy 2004
    hdu 2837 Calculation
  • 原文地址:https://www.cnblogs.com/xxlya/p/7742435.html
Copyright © 2011-2022 走看看