zoukankan      html  css  js  c++  java
  • 04-String-动手动脑

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

    结果为:

    结论:

    结论:

    1、在Java中,内容相同的字符常量只保存一份以节省内存,所以s0,s1,s2实际上引用的是同一个对象。

    2、编译器在编译s2一句时,会去掉“+”号,直接把两个字串连接起来得一个字串。这种优化工作由Java编译器自动完成。

    3、当直接使用new关键字创建字符串对象时,虽然值一致,但仍然是两个独立的对象。

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

     1 public boolean equals(Object anObject) {
     2     if (this == anObject) {
     3         return true;
     4     }
     5     if (anObject instanceof String) {
     6         String anotherString = (String)anObject;
     7         int n = count;
     8         if (n == anotherString.count) {
     9         char v1[] = value;
    10         char v2[] = anotherString.value;
    11         int i = offset;
    12         int j = anotherString.offset;
    13         while (n-- != 0) {
    14             if (v1[i++] != v2[j++])
    15             return false;
    16         }
    17         return true;
    18         }
    19     }
    20     return false;
    21     }

     由代码可知,这个函数首先判断这两个对象是否引用同一个字符串对象,如果是直接比较是否相同,如果不是,则比较两个对象中的字符是否相等。而且比较的方式是比较单个字符。

     三、整理String类的length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArray()使用说明。

    1.int length():返回字符串的长度。

     1 public class Test {
     2     
     3     public static void main(String[] args) {
     4         
     5         String s1 = new String("hello world!");
     6         
     7         String s2 = new String("hello");
     8         
     9         System.out.println("s1的长度为:"+s1.length());
    10         
    11         System.out.println("s2的长度为:"+s2.length());
    12         
    13     }
    14 
    15 }

    结果:

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

     1 public class Test {
     2     
     3     public static void main(String[] args) {
     4         
     5         String s1 = new String("hello world!");
     6         
     7         System.out.println("排在s1的第7位的字符为:"+s1.charAt(6));
     8         
     9     }
    10 
    11 }

    结果:

    3.void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin):将字符从字符串复制到目标字符数组。

    (1)srcBegin -- 字符串中要复制的第一个字符的索引。

    (2)srcEnd -- 字符串中要复制的最后一个字符之后的索引。

    (3)dst -- 目标数组。

    (4)dstBegin -- 目标数组中的起始偏移量。

    (5)无返回值,但会抛出IndexOutOfBoundsException异常

     1 public class Test {
     2     
     3     public static void main(String[] args) {
     4         
     5         String s1 = new String("hello world!");
     6         
     7         char s2[] = new char[10];
     8         
     9         s1.getChars(6, 12, s2, 0);
    10         
    11         System.out.println(s2);
    12         
    13     }
    14 
    15 }

    结果:

    4.String replace(char oldChar,char newChar):将字符串中的所有oldChar替换成newChar

    public class Test {
        
        public static void main(String[] args) {
            
            String s1 = new String("hello world!");
            
            String s2 = s1.replace('o','a');
            
            System.out.println("修改之后的字符串为:"+s2);
            
        }
    
    }

    结果:

    5.String toUpperCase():全部字符变为大写,返回新字符串。

     1 public class Test {
     2     
     3     public static void main(String[] args) {
     4         
     5         String s1 = new String("hello world!");
     6         
     7         System.out.println("修改之后的字符串为:"+s1.toUpperCase());
     8         
     9     }
    10 
    11 }

    结果:

    6.String toLowerCase():全部字符变为小写,返回新字符串

     1 public class Test {
     2     
     3     public static void main(String[] args) {
     4         
     5         String s1 = new String("HELLO WORLD!");
     6         
     7         System.out.println("修改之后的字符串为:"+s1.toLowerCase());
     8         
     9     }
    10 
    11 }

    结果:

    7.String trim():去掉字符串首尾的空格。

     1 public class Test {
     2     
     3     public static void main(String[] args) {
     4         
     5         String s1 = new String("   hello world!    ");
     6         
     7         System.out.println("修改之后的字符串为:"+s1.trim());
     8 
     9     }
    10 
    11 }

    结果:

    8.char [] toCharArray():将该String对象转换成char数组。

     1 public class Test {
     2     
     3     public static void main(String[] args) {
     4         
     5         String s1 = new String("hello world!");
     6         
     7         char s2[] = new char[12];
     8         
     9         s2 = s1.toCharArray();
    10         
    11         for(int i = 0 ; i < 12 ; i ++){
    12             
    13             System.out.print(s2[i]+" ");
    14             
    15         }
    16         
    17     }
    18 
    19 }

    结果:

  • 相关阅读:
    常用排序算法及java语言实现
    机器学习实战笔记(python3实现)01--概述
    笔试错题--(字符串常量池和JVM运行时数据区)
    笔试错题(典型题)
    java进阶--java网络编程
    01_Java基础_第1天(Java概述、环境变量、注释、关键字、标识符、常量)
    数据库3(DBUtils)
    数据库2(JDBC、DBUtils)
    数据库1(数据库、表及表数据、SQL语句)
    Linux的基本命令
  • 原文地址:https://www.cnblogs.com/kangxy/p/7744028.html
Copyright © 2011-2022 走看看