Results 1 to 14 of 14
  1. #1
    psx2514 is offline Member
    Join Date
    Oct 2012
    Posts
    50
    Rep Power
    0

    Default Which format is better for strings that are too long to fit on one line (in your cod?

    Of these two options, which is the preferred format?

    A)
    System.out.print("blah blah blah blah blah blah blah" +
    " blah blah blah blah blah blah blah.");

    or B)
    System.out.print("blah blah blah blah blah blah blah");
    System.out.print(" blah blah blah blah blah blah blah");

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Which format is better for strings that are too long to fit on one line (in your

    The second is clearer to me. Although I might assign the output string to a variable and print that. Depends on the circumstances.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Join Date
    Jan 2013
    Posts
    24
    Rep Power
    0

    Default Re: Which format is better for strings that are too long to fit on one line (in your

    Weak, it ate my post. Is killign me today.

    So better is usually all about making it clearer for yourself and others if you come back to it in ten years or something.
    Commenting is good, though I am admittedly lazy. That laziness has bit my behind more than once. i.e. An old PHP menu I wrote, looking at it the other day, required MUCH pain to decrypt! I should have written for clarity and commented.

    I know strings are immutable, so in the long run, each time one appends a string they are creating new ones, but on a small scale, this might be easier to read!


    Java Code:
    //--Create String--//
    sString = " Something to say?";
    //--Append String--//
    sString += " Put it here!";
    //--Print it with a single call--//
    System.out.println(sString);
    Last edited by LaughingSeraph; 03-14-2013 at 05:34 PM.

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

    Default Re: Which format is better for strings that are too long to fit on one line (in your

    Quote Originally Posted by LaughingSeraph View Post
    Weak, it ate my post. Is killign me today.

    So better is usually all about making it clearer for yourself and others if you come back to it in ten years or something.
    Commenting is good, though I am admittedly lazy. That laziness has bit my behind more than once. i.e. An old PHP menu I wrote, looking at it the other day, required MUCH pain to decrypt! I should have written for clarity and commented.

    I know strings are immutable, so in the long run, each time one appends a string they are creating new ones, but on a small scale, this might be easier to read!


    Java Code:
    //--Create String--//
    sString = " Something to say?";
    //--Append String--//
    sString += " Put it here!";
    //--Print it with a single call--//
    System.out.println(sString);
    Option A) in the OP can be optimized by the compiler (i.e. remove the String concatenation by constant folding) while your version leaves a concatenation to be done at runtime.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Join Date
    Jan 2013
    Posts
    24
    Rep Power
    0

    Default Re: Which format is better for strings that are too long to fit on one line (in your

    Just out of curiosity, if one were working with a ton of strings, how much of a hit does the program take for performance in each case?
    I honestly don't know what all Java does when it compiles, or how lean strings are.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Which format is better for strings that are too long to fit on one line (in your

    I can tell you if you have a lot of strings you should use StringBuilder. At least when appending it is about an order of magnitude faster.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    Join Date
    Jan 2013
    Posts
    24
    Rep Power
    0

    Default Re: Which format is better for strings that are too long to fit on one line (in your

    OMG, ty jim.

    It's a fast, fully functional MUTABLe string object...muahahahha

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default Re: Which format is better for strings that are too long to fit on one line (in your

    Logging is usually (we're talking production stuff here) A.
    It's also how an IDE will break up a long String onto multiple lines.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: Which format is better for strings that are too long to fit on one line (in your

    Quote Originally Posted by LaughingSeraph View Post
    OMG, ty jim.

    It's a fast, fully functional MUTABLe string object...muahahahha
    but adding (concatenating) two literal Strings is done by the compiler and so doesn't involve a StringBuilder (or StringBuffer) whatsoever at runtime; it is very cheap.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0

    Default Re: Which format is better for strings that are too long to fit on one line (in your

    you could use a for loop to produce the desired amount of "BLAH"s

    Java Code:
    public class NestedLoop3 {
        public static void main(String[] args) {
            for(int i=1; i<=6; i++) {
         for(int j=1; j<=i;  j++) {
      System.out.print("blah");
         }
         
         System.out.println();
     }
     
        }
    }
    this could give you a set amount of blah's per line depending on your preference.

  11. #11
    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0

    Default Re: Which format is better for strings that are too long to fit on one line (in your

    Java Code:
    public class NestedLoop3 {
        public static void main(String[] args) {
            for(int i=1; i<=6; i++) {
         for(int j=1; j<=i;  j++) {
      System.out.print("blah" + " ");
         }
         
         System.out.println();
     }
     
        }
    }
    you can even concatenate it with a space

  12. #12
    psx2514 is offline Member
    Join Date
    Oct 2012
    Posts
    50
    Rep Power
    0

    Default Re: Which format is better for strings that are too long to fit on one line (in your

    SMH. Why can't you guys answer my question directly as it was asked? It was pretty straight forward.

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default Re: Which format is better for strings that are too long to fit on one line (in your

    Because different places will have different preferences?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Which format is better for strings that are too long to fit on one line (in your

    Quote Originally Posted by psx2514 View Post
    SMH. Why can't you guys answer my question directly as it was asked? It was pretty straight forward.
    Well

    Quote Originally Posted by JosAH
    Option A) in the OP can be optimized by the compiler (i.e. remove the String concatenation by constant folding) while your version leaves a concatenation to be done at runtime.

    kind regards,

    Jos
    Sounds pretty direct to me! Others gave similar direct answers (with suggestions). You are here to learn, correct?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 12
    Last Post: 10-07-2011, 07:32 AM
  2. Number Format Exception while parsing long
    By Aamir in forum Networking
    Replies: 2
    Last Post: 05-15-2011, 02:57 AM
  3. Replies: 1
    Last Post: 02-02-2011, 05:39 AM
  4. Manifest file: line too long error
    By nn12 in forum New To Java
    Replies: 10
    Last Post: 02-01-2011, 03:01 PM
  5. Handling long strings - Advice needed
    By Codercode in forum Forum Lobby
    Replies: 0
    Last Post: 01-02-2011, 12:43 AM

Tags for this Thread

Posting Permissions

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