Reputation: 4940
From the docs
-source release Specifies the version of source code accepted.
If I have a JDK version , say , 1.8, and I mention -source=1.6
, what does it mean ? Does this only mean that whatever code I have written can be compiled by javac of JDK 1.6 or above ?
If that be case , why pass -source=1.6
during javac
command ? As this will generate .class
files and hence there is no source code left to mark (the source code compatibility to 1.6 or above) ? After javac command, all we get is the bytecode and no .java
files.
Upvotes: 1
Views: 443
Reputation: 663
By specifying the source
argument on the compiler, you are telling the compiler that you want the source code you are submitting to comply with that version of Java and check against specific language features for the version you selected (The default is the newest version typically, even if you don't specify source version yourself). The docs are clear on what values are acceptable and what value is the default. This does not change your source code or transform your code to older versions, it merely alerts you if you are using features that are in later versions of Java. If you are not using newer features of the java language then this will simply compile your code and generate class files as usual.
javac MyProgram.java -source 1.6
The command above will tell the compiler to treat the source code as it was compatible with Java version 1.6.
Below are the allowable values for JDK 1.8 and the description from the docs.
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.
Upvotes: 0
Reputation: 1261
If I have a JDK version , say , 1.8, and I mention -source=1.6 , what does it mean ? Does this only mean that whatever code I have written can be compiled by javac of JDK 1.6 or above ?
If your code uses Java 8 features, it won't even compile with -source=1.6. Otherwise, not necessarily true, but generally, yes, it should work with Java 6 and above.
If that be case , why pass -source=1.6 during javac command ? As this will generate .class files and hence there is no source code left to mark (the source code compatibility to 1.6 or above) ? After javac command, all we get is the bytecode and no .java files.
Javac is the Java Compiler. Of course it will generate .class files, as that is the compiled form of a java program. Why would you pass it? Let's say you want to target a specific version, this is the easiest way to keep support at that level.
You can test this by using a Java 8 feature ( lambdas, streams, datetimeformatter ), then try to compile. Your compilation will fail.
Upvotes: 1
Reputation: 718886
Does this only mean that whatever code I have written can be compiled by javac of JDK 1.6 or above ?
Nope.
The -source=1.6
option means that your code can only use Java language constructs that are part of the Java 6 and earlier versions of the Java language.
For example, any Java 8 lambdas, or Java 9 var
declarations would be flagged as compilation errors.
Upvotes: 2
Reputation: 792
Java8 introduced lambda expressions. If you compile your application with -source=1.6
the compiler will not allow lambda expressions despite it being supported with JDK8.
Upvotes: 1