Saving args2-8 in a string?

Discussion in 'Plugin Development' started by iPhysX, Dec 4, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    iPhysX

    So simple, yet i don't know how to do it.
    Code:java
    1. if (args.length > 2) {
    2. String s = args;
    3. }


    that little bit there is to try to describe what i mean a little further..

    i want args 2 - 8 to be in a database in a single field.

    Any help would be appreciated.
     
  2. Offline

    Jogy34

    why don't you just do:
    Code:
    if(args.length > 2)
    {
    String newString = "";
        for(int i = 2;  !args[i].equals(null); i++)
        {
            newString += args[i] + " ";
        }
    }
     
    iPhysX likes this.
  3. Offline

    darkmage0252


    i think you mean

    if (args.length > 2) {
    String s = s + args;
    }
     
  4. Offline

    iPhysX

    This is exactly what i wanted to do. Thanks alot!

    EDIT: what in the hell has happened to this post :O
     
  5. Offline

    Jogy34

    You cant have any special text (eg. BOLD, ITALICS, UNDERLINE, etc...) in a CODE/QUOTE box
     
  6. Offline

    Sagacious_Zed Bukkit Docs

    arg[ i ] will break it in code however.

    anyways
    Code:
    String s = Joiner.on(" ").join(Array.copyOfRange(args, 2, 9));
    @darkmage0252
    dont use string concatenation for this, gets rather slow.
     
  7. Offline

    DrBowe

    @Jogy34
    @iPhysX

    A StringBuilder would be more efficient there, or better yet, what @Sagacious_Zed suggested.
    It's a negligible difference, but still helps to know.
     
  8. Offline

    Brain

    If you don't want to use StringBuilder you can generate a new String object. This is ok for small use as in commands somebody typed on a command line. I wouldn't recommend merging 100 MByte-sized strings like this, this is where StringBuilder performs better and for ultimate performance probably ByteBuffer.

    Code:
    String compositeString = new String();
    for(i = 0, etc)
        compositeString += args[i];
    
    The following is a short explanation about the way Java handles Strings:

    The reason is that java Strings use a process called interning to save memory. There is a master string as an array of characters. If you create new Strings java tries to find it inside the master string (and adds it if need be) and only saves offset and length on the master string. Typical trade-off processing power vs. memory usage. As a side-effect Java Strings are also immutable.If you explicitly generate a String object with "new" you generate a completely new String instance that also uses its own master string which initially is empty or whatever you set the initial content to.

    StringBuilders mutable. The StringBuilder uses less processing power, but more memory. If you create two StringBuilders with the same content you use twice the memory. On the other hand if you make two Strings with the same content you only use memory once.
     
  9. Offline

    halley

    To expand on Brain's note: you should be using a StringBuilder to concatenate Strings... because it's not the memory but the number of individual objects you make to do it.

    If you code:

    Code:
    String apple = "apple";
    String banana = "banana";
    String joined = apple + banana;
    
    Java really does this:

    Code:
    String apple = "apple";
    String banana = "banana";
    StringBuilder temporary = new StringBuilder();
    temporary.append(apple);
    temporary.append(banana);
    String joined = temporary.toString();
    
    That gets even worse in a loop, because each iteration of the loop makes a whole new StringBuilder. Use one StringBuilder yourself, and it gets quite a bit more efficient in terms of created objects.
     
  10. Offline

    Brain

    The number of objects is not a problem for String. The problem is that internally String runs a pattern-matching algorithm on the master string. This is not efficient in a scenario where you have to deal with big strings, resulting big master string and a helluva lot of defined strings.

    If you're just adding up a few short strings typed into chat it's permissible to use String. It might cost more processing power and memory to create a StringBuilder and also to increase its internal array. I don't know which which performs in this particular scenario. If you're interested you could run loops over each solution, a million times each for example, and use System.currentTimeMillis() to measure the performance. Faster solution wins. :)
     
  11. Offline

    halley

Thread Status:
Not open for further replies.

Share This Page