zoukankan      html  css  js  c++  java
  • 动手动脑(补)

    public class StringPool {

        

    public static void main(String args[])

        {

            

    String s1="a";

            

    String s2=s1;

                    

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

                    s1+="b";

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

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

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

    }

    }

    分析:给字符串赋值意味着两个变量现在引用同一个字符串变量对象,所以s1==s2返回值为true,String对象是只读的,使用+修改了s1变量的值,实际上得到了一个新的字符串,内容为ab,与原来s1所引用的对象无关,所以返回值是false,字符串常量引用的字符串与s1引用的对象无关,string.equas()方法可以比较两个字符串的内容。

    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);//使用new关键字创建了两个不同的对象,所以返回值为false

            

    System.out.println(s1.equals(s2));//内容相同,值为true

            

    String s3="Hello";

            

    String s4="Hello";

              

    System.out.println(s3==s4);//内容相同的常量,引用同一个对象,返回值为true

            

    System.out.println(s3.equals(s4));//内容相同,返回值为true

            

        

    }

    }

    Length()//返回当前字符串长度

    charAt(int index):取字符串中的某一个字符,其中的参数index指的是字符串中的序数。字符串的序数从0到length()-1.

    getChars(int srcBegin,int srcEnd,char[]dst,int dstBegin):该方法将字符串拷贝到字符数组中。其中srcBegin为拷贝的起始位置,srcEnd为拷贝的结束位置,字符串数组dst为目标字符数组,dstBegin为目的字符数组拷贝的起始位置。

    replace(char oldchar,char newchar)//将字符串中的第一个oldchar替换成newchar

    toUpperCase()://将字符串转化成大写

    toLowerCase()://将字符串转化为小写

    trim()://返回该字符串去掉开头和结尾空格后的字符串

    toCharArray()://将string对象转换成char数组

      

  • 相关阅读:
    PL/SQL中的 not
    正则12和\1的理解
    eclipse/myeclipse注释模板的修改
    jboss修改内存
    myEclipse开发内存溢出解决办法myEclipse调整jvm内存大小 java.lang.OutOfMemoryError: PermGen space及其解决方法
    MyEclipse 启动报错:'Building workspace' has encountered a problem解决方法
    jboss 7.1.1.final 报错 set the maxParameterCount attribute on the Connector
    在 Ubuntu/Debian 下安装 PHP7.3 教程
    mariadb新安装解决远程访问以及root登录
    Docker 探索安装WordPress+Mysql8.0
  • 原文地址:https://www.cnblogs.com/liying123/p/4909608.html
Copyright © 2011-2022 走看看