• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

which hirarcy methods will call

 
akhter wahab
Ranch Hand
Posts: 151
Java MyEclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is a code



i like to know these method will perform operations on someThing but in which hirarcy i mean which method will do action first,second third etc
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The methods are executed from left to right. The result of a method becomes the object on which the next method is invoked. Therefore all methods chained in this way must return some type instead of "void".

Marco
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All but the last method must return something. Unless the result of the entire operation is saved in a variable, then also the last method must return something.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since Strings are immutable, the result of that operation will be lost unless that line is preceded by . . .

String text =

or similar.
 
akhter wahab
Ranch Hand
Posts: 151
Java MyEclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Since Strings are immutable, the result of that operation will be lost unless that line is preceded by . . .

String text =

or similar.


can you please explain this by example ... i didn't understand
regards
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12100
30
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
someThing.toString().replaceAll("the ", "").trim().replaceAll("By ", "")

start at the beginning...you have 'someThing'. You call 'toString()' on it, which returns a string.

on THAT string, you call the 'replaceALL()' method, which returns a string.

on THAT string, you call 'trim()', which returns a string.

on THAT string, you call 'replaceAll()', which returns a string...

which is never saved. None of the string methods ever change the current string, they only build NEW strings and return those. So, you have done all this work, created (at least) four new strings, then never save what you get. You've basically done all this work for nothing. You would need to write something like



newString would then have the changes, and you could then use it somewhere.
 
akhter wahab
Ranch Hand
Posts: 151
Java MyEclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks @fred rosenberger
1) i just place this code for understanding thats why not assigning this to any thing...
2) now one more Question arrise from your explanation that is

start at the beginning...you have 'someThing'. You call 'toString()' on it, which returns a string.

on THAT string, you call the 'replaceALL()' method, which returns a string.

on THAT string, you call 'trim()', which returns a string.

on THAT string, you call 'replaceAll()', which returns a string...
[/code]

can i get all these Strings not only resultant String from
[code=java]someThing.toString().replaceAll("the ", "").trim().replaceAll("By ", "")
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
akhter wahab wrote: . . . please explain this by example ... i didn't understand
regards
Fred has done so (Thank you Fred )
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would changetoThen you can retain the details of the intermediate Strings.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic