Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By JosAH

Thread: Code architecture

  1. #1
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Code architecture

    Have four different code examples with same output result.
    Question is, which example is better?

    Java Code:
        public static void main (String[] args) {
            String prefix = prefix("xxx00", "xxxxxxxxxx");
        }
        
        public static String prefix(String s1, String s2) {
            int min = s1.length() < s2.length() ? s1.length() : s2.length();
            String prefix = "";
            int i = 0;
            while (i < min && s1.charAt(i) == s2.charAt(i)) {           
                prefix += s1.charAt(i++);
            }
            return prefix;
        }
    or:

    Java Code:
            while (i < min) {           
                if (s1.charAt(i) == s2.charAt(i))
                    prefix += s1.charAt(i);
                i++;
            }
    or:

    Java Code:
            for (int i = 0; i < min; i++)
                if (s1.charAt(i) == s2.charAt(i))
                    prefix += s1.charAt(i);
    or:

    Java Code:
            for (int i = 0; i < min && s1.charAt(i) == s2.charAt(i); i++)
                prefix += s1.charAt(i);
    or?

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,763
    Blog Entries
    7
    Rep Power
    21

    Default Re: Code architecture

    I'd get the prefix after the loop has finished; it doesn't matter much for your small examples but if the common prefix happens to be large, a lot of Strings have been created and thrown away during those loops.

    kind regards,

    Jos
    diamonddragon likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Code architecture

    Do You mean each time I concatenate String object, it is actually new String object, because of String immutability?

    So, is something like this best solution?
    Java Code:
            int i;
            for (i = 0; i < min && s1.charAt(i) == s2.charAt(i); i++);
            prefix = s1.substring(0, i);
    Last edited by diamonddragon; 01-27-2012 at 09:27 PM.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,763
    Blog Entries
    7
    Rep Power
    21

    Default Re: Code architecture

    Quote Originally Posted by diamonddragon View Post
    Do You mean each time I concatenate String object, it is actually new String object, because of String immutability?

    So, is something like this best solution?
    Java Code:
            int i;
            for (i = 0; i < min && s1.charAt(i) == s2.charAt(i); i++);
            prefix = s1.substring(0, i);
    Yup, that's much better (big-Oh wise that is).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. What is MVC architecture, how do I use it
    By africanhacker in forum New To Java
    Replies: 6
    Last Post: 03-21-2011, 03:49 PM
  2. Architecture of pos
    By j-mi-jim in forum Advanced Java
    Replies: 6
    Last Post: 11-12-2010, 02:57 PM
  3. Question about architecture
    By sdmente in forum Advanced Java
    Replies: 7
    Last Post: 07-28-2010, 02:07 PM
  4. Architecture Rules 2.0.1
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 11-17-2007, 03:03 PM
  5. Architecture of Any IDE...
    By vikki_pu in forum Other IDEs
    Replies: 5
    Last Post: 10-30-2007, 02:08 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
  •