Reputation: 671
I've just stumbled across the following ( http://docs.oracle.com/javase/8/docs/technotes/guides/language/assert.html#compatibility ):
Unless you specifically request source mode 1.4 with the -source 1.4 flag, the compiler operates in source mode 1.3. If you forget to use this flag, programs that use the new assert statement will not compile. Having the compiler use the old semantics as its default behavior (that is, allowing assert to be used as an identifier) was done for maximal source compatibility. Source mode 1.3 is likely to be phased out over time.
Please note that this is from the official Oracle documentation for Java 8 --- but I have never run into this particular compile-time error before. More than that,
class Test{
public static void main(String[] args){
int a = 0;
assert a < 0: "a is not negative!";
}
}
compiles without a hitch not only under Java 8 but also by 'flagless' invocation of
javac Test.class
with JDK 1.6.0_24 (the oldest I happen to have; ~ March, 2011).
Apparently, the transition to assert
as a new keyword is well over.
So these are my questions: when did it happen, specifically? and why on earth Oracle keeps adding this admonishment ("If you forget to use this flag, programs won't compile"; look, it's italicized, even! ) to the docs for each new version?
It's not just idle curiosity that makes me wonder 'when' and 'why': in four days I'm going to write my OCP exam for Java 8 (1Z0-809), and I expect all sorts of nasties jump at me from dark corners...
Upvotes: 2
Views: 187
Reputation: 131346
This official Java guide : http://docs.oracle.com/javase/8/docs/technotes/guides/language/assert.html#compatibility was written for the Java 1.4 release that has introduced the assert statement that was not previously available (in the 1.3 version).
In fact, this guide comes from this page that refers some enhancements of each released Java version : http://docs.oracle.com/javase/8/docs/technotes/guides/language/enhancements.html
It comes from this particular section :
Enhancements in J2SE 1.4
Assertion Facility - Assertions are boolean expressions that the programmer believes to be true concerning the state of a computer program. For example, after sorting a list, the programmer might assert that the list is in ascending order. Evaluating assertions at runtime to confirm their validity is one of the most powerful tools for improving code quality, as it quickly uncovers the programmer's misconceptions concerning a program's behavior.
So, in fact it is not really a problem of not updated documentation.
You have to consider this information as a guide for the specified version : J2SE 1.4
.
Having this kind of history is useful to have an overview of the Java language evolution through the versions.
But your tests are accurate and you can also get a validation from the javac
official documentation that is updated at each release that javac
with a JDK 8 doesn't use the 1.3 source by default.
When you compile a class, the javac
command may take as argument a source
that specifies the major JDK release to accept in the source code.
From the official Java 8 documentation :
-source release
Specifies the version of source code accepted. The following values for release are allowed :
1.3 The compiler does not support assertions, generics, or other language features introduced after Java SE 1.3.
1.4 The compiler accepts code containing assertions, which were introduced in Java SE 1.4.
1.5 The compiler accepts code containing generics and other language features introduced in Java SE 5.
5 Synonym for 1.5.
1.6 No language changes were introduced in Java SE 6. However, encoding errors in source files are now reported as errors instead of warnings as in earlier releases of Java Platform, Standard Edition.
6 Synonym for 1.6.
1.7 The compiler accepts code with features introduced in Java SE 7.
7 Synonym for 1.7.
1.8 This is the default value. The compiler accepts code with features introduced in Java SE 8.
8 Synonym for 1.8.
If this argument is not specified, it has a default value.
And you can read that 1.8
is the default value.
Consequently, the compiler accepts source code with features introduced in Java SE 8
(assert
from 1.4 included of course).
Upvotes: 1