Aren't Strings Immutable... in Which Case...
Aren't Strings Immutable... in Which Case...
Java FAQ
Recent Topics
Register / Login A friendly place for programming This week's giveaway is in the Jobs Discussion forum. greenhorns! We're giving away four copies of Presenting for Geeks
and have Dirk Haun on-line! See this thread for details.
Author
Wilson Mui Ranch Hand Joined: Apr 09, 2003 Posts: 140
...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?
view plain c opy to c lipboard print ?
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 .
1 .S t r i n g B u f f e rs b=n e wS t r i n g B u f f e r ( " a b c " ) ; 2 .S t r i n gs=n e wS t r i n g ( " a b c " ) ; 3 .s b . a p p e n d ( " d e f " ) ; 4 .s . a p p e n d ( " d e f " ) ; 5 .s b . i n s e r t ( 1 ," z z z " ) ; 6 .s . c o n c a t ( s b ) ; 7 .s . t r i m ( ) ;
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.
www.coderanch.com/t/241564/java-programmer-SCJP/certification/Aren-Strings-immutable-case 1/4
7/16/13
3. The compiler would generate 4. The compiler would generate 5. The compiler would generate 6. The compiler would generate 7. The compiler would generate The answer is 4,5 Can somebody explain?
an an an an an
3. 4. 5. 6. 7.
s.trim(); does return a String. It just does not assign it to anything. Someone please correct me if I'm wrong
view plain
c opy to c lipboard
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 .
S t r i n g B u f f e rs b=n e wS t r i n g B u f f e r ( " a b c " ) ; S t r i n gs=n e wS t r i n g ( " a b c " ) ; s b . a p p e n d ( " d e f " ) ; s . a p p e n d ( " d e f " ) ; / /C o m p i l eE r r o r :N oa p p e n d / / m e t h o di nt h eS t r i n gC l a s s s b . i n s e r t ( 1 ," z z z " ) ; s . c o n c a t ( s b ) ;/ /C o m p i l eE r r o r :S t r i n g ' sc o n c a t / / d o e s n ' tt a k eaS t r i n g B u f f e ro b j e c t s . t r i m ( ) ;
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.
thats correct the error will be on line 4 and 6 not on line 5 Pinky
2/4
www.coderanch.com/t/241564/java-programmer-SCJP/certification/Aren-Strings-immutable-case
7/16/13
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.
Harry D'Souza<br />SJC P 1.4 , SC WC D Roger ChungWee Ranch Hand Joined: Sep 29, 2002 Posts: 1683
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 ]
SC JP 1.4, SC WC D 1.3, SC BC D 1.3
Wilson Mui Ranch Hand Joined: Apr 09, 2003 Posts: 140
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.
Similar Threads
www.coderanch.com/t/241564/java-programmer-SCJP/certification/Aren-Strings-immutable-case 3/4
7/16/13
Problem with String and StringXxx objects Pls help me out to solve this question StringBuffer question when a String is mutable A question about String class!
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jul 16, 2013 07:27:40 .
www.coderanch.com/t/241564/java-programmer-SCJP/certification/Aren-Strings-immutable-case
4/4