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?
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.
I'm not sure what line continuation means, but here's your code with no errors.
Joined: Aug 15, 2008
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.
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