4

I am new to Java and am a bit confused about how this is working/how I should be working. I am using intellij and a project that I am working on its pom.xml has:

<java.version>11</java.version>
<maven.compiler.source>$(java.version)</maven.compiler.source>
<maven.compiler.target>$(java.version)</maven.compiler.target>

When I go into the project structure on intellij the module is using language level 11.

on my computer I just downloaded the newest JDK (17)?

Does this cause issues working like this? Should I only be using a JDK that is associated with the version I am working?

I have not had any issues... but I am afraid my dependencies might be different than the ones I should be using...or the the build will be different if someone else is using another jdk.

Jack Trainer
  • 41
  • 1
  • 2
  • [This](https://stackoverflow.com/a/17714777/3845362) might answer your question. In short: Java is mostly backwards compatible. If you have 17 installed and tell IntelliJ to use/compile against version 11. It will assist you in using Java 11 compatible features only. For example if you try to write a sealed class, it will not compile, even though you have sdk 17. – Edito Mar 30 '22 at 09:29

1 Answers1

7

The JDK version specified in the pom.xml specifies what source and target version is passed to javac. This specifies what system libraries and language features can be used.

The language level from IntelliJ matches this.

The installed JDK is the program (or set of programs) used for compiling and running the application.

Java allows to use a newer JDK to compile programs for older (source/target/release) versions.

The produced class files (bytecode) should be the same no matter what JDK version you use as long as the target/release version (specified in the pom.xml/language level in IntelliJ) is the same.

Furthermore, Java is (almost completely) backwards compatible. When writing code for an old Java version, it will likely also work in newer Java versions.

dan1st
  • 12,568
  • 8
  • 34
  • 67