字符串是不可变的,那么replace()的工作原理是什么? [英] Strings are immutable then how the replace() works?

查看:100
本文介绍了字符串是不可变的,那么replace()的工作原理是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将字符串的第一个字母/字符替换为最后一个,并将最后一个字符替换为第一个字符.例如.abcd => dbca.字符串在Java中是不可变的,那么我们如何解释该程序的行为呢?请看一下最终输出.str1没有字符'a',但是在最终输出中却出乎意料地出现了..怎么办?//frontBack()的参数为字符串"abcd".

I was trying to replace the first letter/char of a string by it's last one and last one by it's first one. E.g. abcd => dbca. Strings are immutable in Java then how can we explain the behavior of this program? Please have a look at final output. str1 has no char 'a' but in final output it appears unexpectedly.. how? //The argument of frontBack() is String "abcd".

 public static void frontBack(String str) {
   String first= ""+str.charAt(0);
   System.out.println("first char is "+first);
   String last = ""+str.charAt(str.length()-1);
   System.out.println("last char is "+last);
   String str1;
   str1 = str.replace(""+str.charAt(0),last);
   System.out.println("String str1 is => "+str1);
   String str2 ;
   str2 = str1.replace(""+str1.charAt(str1.length()-1),first);
   System.out.println("String str2 is derived from str1(dbcd) which has no 'a' but o/p is =>  "+str2);    
  }
 /* Have a look at output:
                        first char is a
                        last char is d
                        String str1 is => dbcd
                        String str2 is derived from str1 i.e. "dbcd" which has no 'a' in it but o/p is =>  abca*/

推荐答案

首先,您必须了解Java(非基本类型)中的变量本身不是对象,它们是引用>到对象.类 String 是不可变的,这意味着一旦创建了 String 对象,就无法更改该对象的内容.但是,您可以 使 String 变量引用另一个 String 对象.

First of all, you have to understand that variables in Java (of non-primitive types) are not objects themselves, they are references to objects. Class String is immutable, which means that once a String object has been created, there is no way to change the content of the object. However, you can make a String variable refer to a different String object.

让我们逐行看看发生了什么.我们先从 str 引用一个内容为"abcd" String 对象.

Let's look what is happening line by line. We start with str referring to a String object with the content "abcd".

String first= ""+str.charAt(0);

这使变量 first 引用具有内容"a" 的新 String 对象.

This makes the variable first refer to a new String object with the content "a".

String last = ""+str.charAt(str.length()-1);

这使变量 last 引用新的 String 对象,该对象仅包含 str 引用.因此, last 引用具有内容"d" String 对象.

This makes the variable last refer to a new String object containing only the last character of the String object that str refers to. So, last refers to a String object with the content "d".

String str1;
str1 = str.replace(""+str.charAt(0),last);

replace()方法采用两个参数:要查找的子字符串和替换它的字符串.请注意, replace()不会更改原始的 String 对象;它返回一个新的 String 对象,所有出现的第一个参数都由第二个参数替换.请参见 API文档.

The replace() method takes two arguments: the substring you want to find, and the string to replace it with. Note that replace() does not change the original String object; it returns a new String object with all occurrences of the first argument replaced by the second argument. See the API documentation.

" + str.charAt(0)"a" last "d" ,因此此行等效于:

""+str.charAt(0) is "a" and last is "d", so this line is equivalent to:

str1 = str.replace("a", "d");

此行之后, str 仍引用原始的 String ,其内容为"abcd" ,而 str1 引用到内容为"dbcd" 的新 String .

After this line, str still refers to the original String with content "abcd" and str1 refers to a new String with content "dbcd".

String str2 ;
str2 = str1.replace(""+str1.charAt(str1.length()-1),first);

在这一行中, replace()的第一个参数是:" + str1.charAt(str1.length()-1),它是"d" ( str1 所引用的 String 的最后一个字母).

In this line, the first argument to replace() is: ""+str1.charAt(str1.length()-1) which is "d" (the last letter of the String that str1 refers to).

第二个参数是 first ,它是"a" .

所以这行等效于:

str2 = str1.replace("d", "a");

此行之后, str1 仍引用内容为"dbcd" String ,而 str2 引用到新的 String 对象,其中所有字母 d 已被 a 替换,因此:"abca" .

After this line, str1 still refers to the String with content "dbcd", and str2 refers to a new String object where all the letters d have been replaced by a, so: "abca".

这篇关于字符串是不可变的,那么replace()的工作原理是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆