[英]What is the difference between a = a.trim() and a.trim()?
I've ran into a bit of a confusion. 我遇到了一点混乱。
I know that String
objects are immutable . 我知道 String
对象是不可变的 。 This means that if I call a method from the String
class, like replace()
then the original contents of the String
are not altered. 这意味着如果我从String
类调用一个方法,比如replace()
那么String
的原始内容不会改变。 Instead, a new String
is returned based on the original. 相反,将根据原始String
返回新的 String
。 However the same variable can be assigned new values. 但是,可以为同一变量分配新值。
Based on this theory, I always write a = a.trim()
where a
is a String
. 基于这个理论,我总是写a = a.trim()
,其中a
是一个String
。 Everything was fine until my teacher told me that simply a.trim()
can also be used. 一切都很好,直到我的老师告诉我,也可以使用a.trim()
。 This messed up my theory. 这搞砸了我的理论。
I tested my theory along with my teacher's. 我和老师一起测试了我的理论。 I used the following code: 我使用了以下代码:
String a = " example ";
System.out.println(a);
a.trim(); //my teacher's code.
System.out.println(a);
a = " example ";
a = a.trim(); //my code.
System.out.println(a);
I got the following output: 我得到以下输出:
example
example
example
When I pointed it out to my teacher, she said, 当我向老师指出时,她说,
it's because I'm using a newer version of Java (jdk1.7) and
a.trim()
works in the previous versions of Java. 这是因为我使用的是较新版本的Java(jdk1.7),而a.trim()
适用于以前的Java版本。
Please tell me who has the correct theory, because I've absolutely no idea ! 请告诉我谁有正确的理论,因为我完全不知道 !
String is immutable in java. 字符串在java中是不可变的。 And trim()
returns a new string so you have to get it back by assigning it. 而trim()
返回一个新字符串,所以你必须通过赋值来获取它。
String a = " example ";
System.out.println(a);
a.trim(); // String trimmed.
System.out.println(a);// still old string as it is declared.
a = " example ";
a = a.trim(); //got the returned string, now a is new String returned ny trim()
System.out.println(a);// new string
Edit: 编辑:
she said that it's because I'm using a newer version of java (jdk1.7) and a.trim() works in the previous versions of java. 她说这是因为我使用的是较新版本的java(jdk1.7),而a.trim()在以前版本的java中有效。
Please find a new java teacher. 请找一位新的java老师。 That's completely a false statement with no evidence. 这完全是一个没有证据的虚假陈述。
String are immutable and any change to it will create a new string. 字符串是不可变的,对它的任何更改都将创建一个新字符串。 You need to use the assignment in case you want to update the reference with the string returned from trim
method. 如果要使用trim
方法返回的字符串更新引用,则需要使用该赋值。 So this should be used: 所以这应该用于:
a = a.trim()
简单地使用“a.trim()”可能会在内存中修剪它(或者智能编译器会完全抛出表达式),但结果不会存储,除非您先将其分配给变量,如“a = a.trim” ();”
You have to store string value in same or different variable if you want some operation (eg trim)on string. 如果要对字符串进行某些操作(例如修剪),则必须将字符串值存储在相同或不同的变量中。
String a = " example ";
System.out.println(a);
a.trim(); //output new String is not stored in any variable
System.out.println(a); //This is not trimmed
a = " example ";
a = a.trim(); //output new String is stored in a variable
System.out.println(a); //As trimmed value stored in same a variable it will print "example"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.