5 Replies - 275 Views - Last Post: 12 January 2012 - 04:33 AM Rate Topic: -----

#1 TinkerTailor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 10-January 12

String concatenation - basic question

Posted 11 January 2012 - 07:48 PM

I had a lecture today where my professor was discussing the use of concatenation with strings in Java.
This was the sample code he discussed:
public class Roses {
    /**
     * Prints a poem (of sorts) on multiple lines.
     * @param args unused
     */
    public static void main(String[] args) {
        System.out.println("Roses are red,\n\tViolets are blue,\n"            /* line1 */
                + "Sugar is sweet,\n\tBut I have \"commitment issues\",\n\t"  /* line2 */
                + "So I'd rather just be friends\n\tAt this point in our "    /* line3 */
                + "relationship.");
    }
}



When I asked him about concatenating lines 1-3, I wanted to know if the style required you to concatenate them when entering to a new line in the IDE or if the string would carry on without. His answer was a bit cryptic, so I wanted to ask if you can break up a single string onto multiple lines without concatenation in Java...or if you have to do that?

Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: String concatenation - basic question

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: String concatenation - basic question

Posted 11 January 2012 - 07:51 PM

You can always put a long String on a very long line or "aaaa" + "bbbbb"
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10786
  • View blog
  • Posts: 40,169
  • Joined: 27-December 08

Re: String concatenation - basic question

Posted 11 January 2012 - 07:52 PM

When you end a String and begin a new one, you have to concatenate them in the context of something like a println(). I think maybe certain IDEs don't force you to end the String before moving to a new line. Others do.
Was This Post Helpful? 0
  • +
  • -

#4 TinkerTailor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 10-January 12

Re: String concatenation - basic question

Posted 11 January 2012 - 07:56 PM

Thanks for the really quick replies. This is going to be something to keep in mind as I continue down the path to learning Java.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: String concatenation - basic question

Posted 11 January 2012 - 08:07 PM

In Eclipse if you type

String x = "asasasasas<cr>

the IDE displays

String x = "asasasasas"
+ "
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: String concatenation - basic question

Posted 12 January 2012 - 04:33 AM

Some questions are best answered through experimentation, especially when the results may be dependent on the observer's or test subject's environment. Try the various options in your development environment and see what happens.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1