zoukankan      html  css  js  c++  java
  • 动手动脑

    Stringequals()方法:

    String类型当比较不同对象内容是否相同时,应该用equals,因为“==”用于比较引用类型和比较基本数据类型时具有不同的功能。

    1:当对象不同,内容相同,"=="返回false,equals返回true

    String s1=new String(“java”);

    String s2=new String(“java”);

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

    System.out.println(s1.equals(s2));//true

    2:当同一对象,"=="和equals结果相同

    String s1=new String(“java”);

    String s2=s1;

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

    System.out.println(s1.equals(s2));//true

    3:如果值不相同,对象就不相同,所以"==" 和equals结果一样

    String s1=”java”;

    String s2=”java”;

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

    System.out.println(s1.equals(s2));//true

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

               char myChar[]=x.toCharArray();

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

    请阅读JDK中String类上述方法的源码,模仿其编程方式,编写一个MyCounter类,它的方法也支持上述的“级联”调用特性,其调用示例为:

    MyCounter counter1=new MyCounter(1);

    MyCounter counter2=counter1.increase(100).decrease(2).increase(3);

    package aaa;

     

    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结果

        }

    }

    结果截图:

     

     

     

  • 相关阅读:
    Error: could not open `C:Program FilesJavajre6libi386jvm.cfg'
    异常:java.lang.IllegalStateException: Ambiguous handler methods mapped for HTTP path '/app/userInfoMaint/getProvince.do'
    解析Java反射
    Cause: java.sql.SQLException: 无效的列索引
    Internet Explorer 无法打开该 Internet 站点,请求的站点不可用或无法找到
    解决The JSP specification requires that an attribute name is preceded by whitespace问题
    pl/sql的to_char和to_date
    oracle 在xml中批量插入,批量修改及多组条件查询
    时间转换模板
    Java网络编程从入门到精通(5):使用InetAddress类的getHostName方法获得域名
  • 原文地址:https://www.cnblogs.com/woshitiancai/p/7742980.html
Copyright © 2011-2022 走看看