• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String concat

 
Thiru Thangavelu
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why concat is failing here?

[ June 12, 2002: Message edited by: Thiru Thangavelu ]
 
Brian Lugo
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you compile and run the program?
If yes, what do you think is the answer?
Brian
 
Thiru Thangavelu
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there is no method in String class as concat(StringBuffer). That's why the error is. Isn't?
 
geetha nagarajan
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String class has a concat method that takes only String as parameter.
So,only concat(String) will work.
It does not work with any other parameter.
Am i right?
 
Swati Gupta
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even the line s.append("def"); will error out as String class does not have append.
 
Jie Zhou
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even the line s.append("def"); will error out as String class does not have append.

Actually s.append("def") will not cause error.
The overloaded method
StringBuffer append(String str)
appends the string "def" to this string buffer.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jie Zhou:

Actually s.append("def") will not cause error.
The overloaded method
StringBuffer append(String str)
appends the string "def" to this string buffer.

But s is a String and String does not have an append method, overloaded or not!
 
Jie Zhou
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Thomas.
I thought s is a StringBuffer variable, but it is a Stirng.
 
Bonnie Beach-Morin
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if the programmer used (and s.append("def) were removed), the code would not work as intended. The String class is immutable, so any call to the concat method returns a new String object and does not modify the original String object. The programmer would need to do something like
 
Gautam Sewani
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The last reply was the only correct reply!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic