Results 1 to 5 of 5

Thread: method calling?

  1. #1
    frejon26 is offline Member
    Join Date
    Jan 2008
    Posts
    14
    Rep Power
    0

    Default 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.

  2. #2
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    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.
    dont worry newbie, we got you covered.

  3. #3
    frejon26 is offline Member
    Join Date
    Jan 2008
    Posts
    14
    Rep Power
    0

    Default

    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?
    Last edited by frejon26; 01-24-2008 at 10:46 PM.

  4. #4
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    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.
    dont worry newbie, we got you covered.

  5. #5
    frejon26 is offline Member
    Join Date
    Jan 2008
    Posts
    14
    Rep Power
    0

    Default

    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 :)

Similar Threads

  1. Calling a method in another class
    By uncopywritable in forum New To Java
    Replies: 9
    Last Post: 10-22-2012, 04:01 PM
  2. Dynamic method calling
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-15-2008, 08:46 AM
  3. i'm no good in calling Method please help
    By lowpro in forum New To Java
    Replies: 1
    Last Post: 11-26-2007, 05:15 PM
  4. Help with Calling a method
    By Albert in forum New To Java
    Replies: 3
    Last Post: 07-10-2007, 03:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •