my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Line continuation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Line continuation" Watch "Line continuation" New topic
Author

Line continuation

Paolo Dina
Ranch Hand

Joined: Aug 15, 2008
Posts: 63
Hi, does Java provide some sort of syntax for line continuation? I'd like to write something like the following, but I get an error at compile-time. Beginner's FAQ on Javaranch didn't help, any suggestion?



Thanks


SCJP 5
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Java can ignore whitespace. Change your code to this:The compiler will simply ignore the line ends and join all that code to form a single statement. The only place you need syntax for line breaks is inside Strings, where you usually use "+<line break>".

By the way: don't write if (someBoolean) return true; else return false. You simply write return someBoolean; There are at least two possible errors in that code. You are using the == when you should probably use the equals method. You can get nasty errors with instanceof if your class is extended.

I would suggest you enhance your equals method by preceding the method header with the @Override annotation, and inserting

o == this ||

immediately after return. That will short-circuit the process if you test whether an object is equal to itself. To find out more about equals, look in Bloch's book, or Google for "Angelika Langer Java equals."

[Additional:] By the way: What is YEAR? Have you got a Calendar instance? If so, then YEAR is a constant which tells the get() method you are looking for the year, not the value recorded as year. You may need to alter that to pubDate.get(Calendar.YEAR). You may simply be able to get a long value from the Calendar instance and check that for equality, or use the equals method of Calendar.
Ryan Beckett
Ranch Hand

Joined: Feb 22, 2009
Posts: 192
I'm not sure what line continuation means, but here's your code with no errors.

Paolo Dina
Ranch Hand

Joined: Aug 15, 2008
Posts: 63
Useful tips to know. About the API, you said well, YEAR is a field of Calendar and I was wrong not using get(Calendar.YEAR). That was my fault, though I think it's dangerous to have access to static fields using instances.




Google for "Angelika Langer Java equals."


Secrets of equals() - Part 1
Secrets of equals() - Part 2


Thank you very much

[EDIT]: added the link to the second part of Secrets of equals() for future reference.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Paolo Dina wrote:

Google for "Angelika Langer Java equals."


Secrets of equals() - Part 1

Thank you very much
Wait till you see part 2

And you're welcome
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Paolo Dina wrote:though I think it's dangerous to have access to static fields using instances.

That's why most IDEs can report that as a warning.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11411
    
  16

for what it's worth...

in some shells (Unix ones in particular), you could write some programs directly on the command line. The problem is, sometimes you need to hit 'enter' before you are done writing your program. As soon as you hit 'enter', the shell takes that as a sign you are done and want it to DO something.

enter the line continuation character. you could type something (often a slash or backslash - i forget which) which tells the shell "hey, I'm not REALLY done entering my program, so please ignore this newline character here."

Java, however, is not run directly on the shell. You write a file with your java statements, and it is read by a program (called the compiler). The language is defined in a way that all white-space characters are ignored - except to determine the language tokens. so it doesn't matter if you have one space, 50 spaces, some tabs, or newlines. Java knows that everything up to a semi-colon (with some exceptions) is considered one 'line', so it will continue reading multiple lines looking for that character to figure out what goes together.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Line continuation