• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Aren't Strings immutable...in which case....

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...In which case, any line below that calls a method on the String object, must also return a value...because if it doesn't. It means (in most cases) that the method altered the object itself. And that is not possible. So Line 7 should throw an compile time error too right. Not so. Can somebody explain?


Which of the following statements are true:
1. The compiler would generate an error for line 1.
2. The compiler would generate an error for line 2.
3. The compiler would generate an error for line 3.
4. The compiler would generate an error for line 4.
5. The compiler would generate an error for line 5.
6. The compiler would generate an error for line 6.
7. The compiler would generate an error for line 7.
The answer is 4,5
Can somebody explain?
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
s.trim(); does return a String.
It just does not assign it to anything.
Someone please correct me if I'm wrong
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

My compiler says the errors are at lines 4 and 6. (As oppossed to 4 and 5). There's a compiler error at line 4 because the String class doesn't have an append() method. And there's compiler error at line 6 because the concat() method doesn't take a StringBuffer object.
But to get to what I think you were asking about: Those String methods, like trim() return newly constructed String objects. You can assign them to a reference:

String myNewString = s.trim();
or not:
s.trim();
If the String object is not assigned to a new reference, then it is lost, and the string in the s String object stays the same.
StringBuffer methods DO change the value of their object, so there is no need to tie the new object to a reference, as there is with String objects.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thats correct the error will be on line 4 and 6 not on line 5
Pinky
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As rightly pointed at already, the Compile errors are thrown from lines 4 and 6.
The line 7 does not give a compile time error because the trim() method does ofcourse return a new String object but in the above given code this new String object is not assigned to any other String reference.
Hence the code is syntactically correct but nevertheless the line 7 is of no use unless the String object returned from the trim() method is stored.
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that trim only returns a new String object if there is something to trim! In this case, nothing can be trimmed, so the compiler simply returns a reference to the original String object. Let's add some code which proves this by using the == operator (which compares object references).
String s2 = s.trim();
System.out.println(s2 == s); //returns true
In contrast:
String s = new String("abc ");
String s2 = s.trim();
System.out.println(s2 == s); //returns false
[ April 22, 2003: Message edited by: Roger Chung-Wee ]
 
Wilson Mui
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it was 4, 6, I made the syntactic mistake. Thansks for the help, things make sense now.
I am guessing too then that, any method with a real return value can be either assigned or ignored, in which case, the return value is lost.
 
reply
    Bookmark Topic Watch Topic
  • New Topic