• Java 字符串


    一、StringPool.java

     1 public class StringPool {
     2     
     3     public static void main(String args[])
     4     {
     5         
     6         String s1="Hello";
     7         
     8         String s2="He"+"llo";
     9                 
    10         System.out.println(s1==s2);//true
    11         
    12         System.out.println(new String("Hello")==new String("Hello"));//false
    13         
    14         String s3="ab";
    15         
    16         System.out.println(s3=="ab");//true
    17     }
    18 }

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

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

    二、String.equals()

     1 public class StringEquals {
     2     public static void main(String[] args) {
     3         String s1=new String("Hello");
     4         String s2=new String("Hello");
     5         System.out.println(s1==s2);
     6         System.out.println(s1.equals(s2));
     7         String s3="Hello"; 
     8         String s4="Hello";
     9         System.out.println(s3==s4);
    10         System.out.println(s3.equals(s4));
    11     }
    12 }

      在String类里面是这样重写equals()方法的实现的:用当前的这个字符串对象和指定的字符串对象比较,指定的字符串对象不能为空并且这个对象的字符序列和当前这个字符串对象的字符串序列一样,如果这些条件都满足,那么这两个字符串对象就是相等的。

    三、类方法的“级联”调用

     1 public class ShowMyCounter {
     2     public static void main(String[] args) {
     3         MyCounter myCounter=new MyCounter(1);
     4         myCounter.showNum();
     5         MyCounter myCounter1=myCounter.increase(100).decrease(30).increase(50);
     6         myCounter1.showNum();
     7     }
     8 }
     9 class MyCounter{
    10     private int num=0;
    11     public MyCounter(int x){
    12         num=x;
    13     }
    14     public MyCounter increase(int x){
    15         num+=x;
    16         return this;
    17     }
    18     public MyCounter decrease(int x){
    19         num-=x;
    20         return this;
    21     }
    22     public void showNum(){
    23         System.out.println("The value of num is "+num+".");
    24     }
    25 }

      连续调用类方法:所调用方法的返回值类型为该类,方法语句结束return this(返回this指针,即返回调用该方法的对象)。

    四、字符串加密

     1 import java.util.*;
     2 public class StringCode {
     3     public static void main(String[] args) {
     4         // TODO Auto-generated method stub
     5         Scanner in=new Scanner(System.in);
     6         String sourceStr;
     7         sourceStr=in.nextLine();
     8         int i;
     9         char c[]=sourceStr.toCharArray();
    10         for(i=0;i<sourceStr.length();i++){
    11             if((c[i]>='x'&&c[i]<='z')||(c[i]>='X'&&c[i]<='Z'))12                 c[i]-=23;
    13             else
    14                 c[i]+=3;
    15         }
    16         System.out.println(c);
    17     }
    18 }

  • 相关阅读:
    IDEA Rider Express expected
    .netCore与Framework相同位数下生成的hashcode是不一样的
    IDEA Rider代码错误提示关闭
    VS C# 项目一个解决方案包含多个git库项目问题
    Git Updates were rejected because the tip of your current branch is behind 一例解决方案
    IISExpress 管道模式、集成模式切换
    vs 下TGIT插件
    git常用命令
    自建git项目管理库及ssh方式使用
    Ext.net store数据加载事件
  • 原文地址:https://www.cnblogs.com/yifengyifeng/p/6005041.html
Copyright © 2020-2023  润新知