• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Line continuation

 
Paolo Dina
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what line continuation means, but here's your code with no errors.

 
Paolo Dina
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic