6

I've ran into a bit of a confusion.

I know that String objects are immutable. This means that if I call a method from the String class, like replace() then the original contents of the String are not altered. Instead, a new String is returned based on the original. However the same variable can be assigned new values.

Based on this theory, I always write a = a.trim() where a is a String. Everything was fine until my teacher told me that simply a.trim() can also be used. 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.

Please tell me who has the correct theory, because I've absolutely no idea!

Hungry Blue Dev
  • 1,313
  • 16
  • 30
  • 6
    Maybe she means `System.out.println(a.trim())`. This will print the trimed string but do not change actual string `a`. – AJ. Jan 04 '14 at 06:08
  • 1
    Drudged up the Java 6 docs; there isn't any difference in `trim()` as far as I can see. Have a look yourself: the [Java 6 doc](http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#trim()) and the [Java 7 doc](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#trim()). – Dennis Meng Jan 04 '14 at 06:11
  • @ambigram_maker Ask your teacher about the the version she was talking. – AJ. Jan 04 '14 at 06:12
  • 1
    Well, we use jdk1.4 in our school. => obviously no match for jdk1.7! ;) – Hungry Blue Dev Jan 04 '14 at 06:14
  • 2
    ...Sun/Oracle stopped supporting 1.4 over 5 years ago. Not to mention you're missing out on a lot of good things that have been introduced since then. – Dennis Meng Jan 04 '14 at 06:16
  • 1
    Could you ask your teacher to code and compile an example using `a.trim()`? Maybe there's been a misunderstanding such as AJ had suggested. – PakkuDon Jan 04 '14 at 06:17
  • As for the application @PakkuDon, she's been using it everywhere... Not just in System.out.println(a.trim()); – Hungry Blue Dev Jan 04 '14 at 06:25
  • `trim()` was as it is from day 1, I believe your teacher made a mistake, or she has done something like `AJ` mentioned. – Amir Pashazadeh Jan 04 '14 at 06:25
  • 1
    Actually, the day that my teacher started to teach us the methods of class String, she only mentioned "a.trim()". I was the first (and only ) person to raise a question that 'Shouldn't it be "a = a.trim()"?' The reply was "Both are accepted." – Hungry Blue Dev Jan 04 '14 at 06:35

4 Answers4

10

String is immutable in java. And trim() returns a new string so you have to get it back by assigning it.

    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.

Please find a new java teacher. That's completely a false statement with no evidence.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • Well, that's what I pointed out but my teacher wont't accept it. (plz read last para). – Hungry Blue Dev Jan 04 '14 at 06:09
  • 1
    Funny @Sanjay T. Sharma, that's exactly what we students think! ;) – Hungry Blue Dev Jan 04 '14 at 06:17
  • 2
    Teachers can be wrong. Sometimes it's about subjective things, and you just have to shrug and wonder if they're actually right. In lucky cases like this, the teacher is clearly and objectively wrong. Strings have been immutable [since the beginning](http://web.mit.edu/java_v1.0.2/www/javadoc/java.lang.String.html#_top_). – yshavit Jan 04 '14 at 06:19
  • 2
    @yshavit Reminds me of the time a teacher of mine claimed that Java threw an exception if you overflowed an int. (The guy was teaching C at the time, but he *had* taught Java before) – Dennis Meng Jan 04 '14 at 06:23
  • 1
    Thnx @sᴜʀᴇsʜ ᴀᴛᴛᴀ for supporting that my point of view is correct. Well, I'll make sure that I'll always write "a = a.trim()" as I cannot find a new teacher. ;) – Hungry Blue Dev Jan 04 '14 at 07:00
  • Glad to help you :) `A good book also can be a great teacher` :) – Suresh Atta Jan 04 '14 at 07:04
  • As a matter of fact I follow [Java:The complete reference 8th edition by Herbert Schildt](http://www.herbschildt.com) – Hungry Blue Dev Jan 04 '14 at 07:19
3

Simply using "a.trim()" might trim it in memory (or a smart compiler will toss the expression entirely), but the result isn't stored unless you precede with assigning it to a variable like your "a=a.trim();"

EkriirkE
  • 2,277
  • 19
  • 13
2

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. So this should be used:

a = a.trim()
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
1

You have to store string value in same or different variable if you want some operation (e.g 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"
Anirban Pal
  • 529
  • 4
  • 10