aspose file tools*
The moose likes Beginning Java and the fly likes line continuation characters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "line continuation characters" Watch "line continuation characters" New topic
Author

line continuation characters

Dave Kairys
Ranch Hand

Joined: Sep 09, 2005
Posts: 33
Are there any line continuation characters in Java? For instance, if a developer wants a line to move to the next carriage return in .NET, they would code an underscore. Anything similar in Java?

Thanks,
Dave
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Dave Kairys:
Are there any line continuation characters in Java? For instance, if a developer wants a line to move to the next carriage return in .NET, they would code an underscore. Anything similar in Java?

Thanks,
Dave


Do you mean Visual Basic .NET, C# .NET, or Visual C++ .NET? (Okay, I'm not familiar with any of these, really, but from the little knowledge, I do have, they are all very different languages.)

Anyway...I'm not entirely sure what you want. By "carriage return" do you mean that the next character is printed one line below where it would be without the cariage return? Typically, '\r' represents the carriage return character (or "\r" if you want it as a String), but it's exact behavior depends a lot on the underlying operating system. Unfortunately, that's the best suggestion I can come up with at the moment.

Layne


Java API Documentation
The Java Tutorial
Dave Kairys
Ranch Hand

Joined: Sep 09, 2005
Posts: 33
In .NET, the line continuation character is _(underscore). For instance, consider the following comment in VB .NET:
'process loop instructions until end of loop is encountered or _
'no more text to read
The underscore will continue the comment onto the next line and the compiler will not complain. Anything similar in Java?

Thanks,
Dave
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Okay, that makes sense. My comments above were for output from a Java program, not for the code itself. It sounds like that will work in VB.NET, but I'm certain that it won't work in Visual C++ .NET ;-) As I said, these are different languages.

Anyways, for comments you can use the block comments if you need multiple lines in your comment. A typical block comment starts with "/*" and ends with "*/". So for example, you can do this:



The "*" at the beginning of each line is not required, but it is a common convention to visually set off a block comment like this.

If you start the block comment with "/**", you can use the javadoc tool to generate documentation in HTML format for you. This is a separate topic however, so I won't talk about it further.

I can't think of any other contexts where you need a line continuation since Java does not limit the code for a single statement to one line. Instead, you have to type a semi-colon to end the statement.

Does this answer your question?

Layne
[ September 11, 2005: Message edited by: Layne Lund ]
Dave Kairys
Ranch Hand

Joined: Sep 09, 2005
Posts: 33
To some degree. In .NET, it also does not limit the developer in terms of line length. However, when one wants to print a hard copy of the code, the continuation character is helpful; it is noticeable for readability only, really. I posted the comments snippet as how to use the continuation character in .NET. It is similar with code as well, as follows:
Me.BindingContext(Me.DstEmployee1, "tblEmploy").Position = _
Me.BindingContext(Me.DstEmployee1, "tblEmploy").Position - 1
(code for accessing a database)

The underscore will continue the code onto the next line without any problems and the program will execute as written. Anything similar in Java?

Thanks,
Dave
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I wasn't talking about the line length. Java doesn't need a "line continuation character" since the end of the line has absolutely NO special significance. It acts just as any other whitespace character. You MUST put a semi-colon to end a statement. In other words, you can end the line immediately after an = and continue on the next line without any problems. For example:



Will compile just fine, assuming it's in a class. Note that the spaces at the beginning of the second line are unnecessary. However, it is a common convention to indent when you continue a statement on the next line.

Layne
[ September 11, 2005: Message edited by: Layne Lund ]
Dave Kairys
Ranch Hand

Joined: Sep 09, 2005
Posts: 33
Thank you. That does answer my question. I was referring to line length for .NET, just to keep everything on the page. According to your answer, Java doesn't care, just so long as the semicolon is there.

Thank you,
Dave
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Exactly. You can even do something like this if you want:



But you shouldn't because it is more difficult to read, imo.

Layne
[ September 11, 2005: Message edited by: Layne Lund ]
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

The one noticable exceptions is String literals. You cannot place a line break in the middle of a String literal.


is not permitted. But what you can do is concatenate two string literals,
and the (Sun) compiler will optimize this out so that only one String literal is created. (Of course, you should'nt really care about such things as whether one, two, or three literals are created, except to know that different comilers could do different things.)



is the way to get this to work. Note that there is no new line in the String itself; it just spans multiple lines in code. (Use the "\n" to get a new line is a String, of course.)


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: line continuation characters