-
method calling?
hello,
I was wondering in what order are method calls completed on an object?
left to right? or right to left?
say you have a large StringBuffer object with many characters and you want to 1). convert it to a String, 2). get a substring within it and 3). test for an indexOf(".exe");
does it matter how it is written?
string_buff.toString().substring(start,end).indexO f(SPECIAL_EXTENSION);
-thanks.
-
This is simple, go over your code above from both direction you will realize you can't get index of exe before you have that substring and you cant get that substring before you convert that object to string.
Answer is obvious. Forgive me not giving direct answer but i hope you will love the approach.
-
well that is my question, from which direction does it start? if from left to right then, 1)it first turns it to a string, then 2) it turns it to a substring then 3) gets the index of it.
if method calling starts from the right then it would make sense to write the code,
string_buff.indexOf(SPECIAL_EXTENSION).substring(s tart,end).toString();
so which way does the virtual machine call the methods?
-
Let's for the sake of discussion indexOf returns String and we are playing with string only.
If we see last part it's toString() of something not substring of toString() of nothing. Rather than the direction it's problem of the dot operator. I think both of us know the answer and do you like if the dot operator on string_buff is associated with toString() in your second code.
-
thanks roots,
I'm still a little confused but I don't think it makes sense the other way I wrote it (my second post). I also didn't ask the question very well. I see that it is objects calling methods, so...
if you have object.method1().method2().method3();
it goes object.method1() which in its place is the return on the method so then return value is used with the next value return.method2() which then returns another value which will finally be used with method3();
thanks :)