5

The semantics of constructs in Java language are defined by the Java language specification, but they're not in terms of JVM instructions. So, is there possiblity that the specification is not strictly defined to prevent mis-implementation of Java compilers? Or the mapping between Java language and JVM instructions is pretty straightforward, there's no need to worry about this?

Dagang
  • 24,586
  • 26
  • 88
  • 133
  • At one time IBM had a java compiler that not only was fast as hell, but it produced better code that Sun's `javac`. So my guess would be no, there is no strict mapping of java code to JVM instructions, otherwise no compiler could do a better job than any other. – Paul Tomblin Dec 05 '13 at 16:29
  • @PaulTomblin, it's reasonable that a compiler can do optimization about the generated instructions, but I would like to know if there's some STRICT constraints in terms of JVM instructions. – Dagang Dec 05 '13 at 16:33
  • There are almost infinite ways which any given Java program can be converted to equivalent JVM instructions (the same would be true if you were compiling to assembly language). As long as the resulting bytecode does what the program is supposed to do (according to the Java language spec), that is all that matters. – Alex D Dec 05 '13 at 16:34
  • 1
    correction, there *is* an infinite number of ways :-) – Kristopher Micinski Dec 05 '13 at 16:36
  • @Todd, it seems to me that "a compiler can do optimization about the generated instructions" and "strict constraints in terms of JVM instructions" are contradictory. Either the language defines "for java statement `foo` the compiler will generate jvm instructions `bar`" or it doesn't, and if it does, there is no scope for compiler optimization. – Paul Tomblin Dec 08 '13 at 21:14

2 Answers2

6

It would defeat much of the point of separating the language from the bytecode for there to be a clear mapping of Java code to JVM instructions. Instead the compiler has the freedom to make determinations of which instructions best represent the provided source, in order to optimize the efficiency of what's actually run.

This does mean it's possible for a compiler to do the wrong thing, however generally speaking you can trust major compilers like Oracle's are doing the right thing; they get tested a lot.

dimo414
  • 47,227
  • 18
  • 148
  • 244
2

The semantics of constructs in Java language are defined by the Java language specification, but they're not in terms of JVM instructions. So, is there possiblity that the specification is not strictly defined to prevent mis-implementation of Java compilers?

Yes, definitely. Later editions of the Java Language Specification use more formal notation than previous versions. This should help to reduce the possibility of ambiguity and misinterpretation, but any language specification primarily written in English is prone to misinterpretation.

Or the mapping between Java language and JVM instructions is pretty straightforward, there's no need to worry about this?

I think 'straightforward' is a subjective measure. :o) It's more straightforward than I first expected, certainly.

The compiler (javac) itself doesn't appear to do much in the way of optimisation, see this question for example. Most optimisations are actually done by the JVM at runtime (by HotSpot).

Other compiler implementations might do more optimisations. Also, there are some bytecode processors (like ProGuard) that perform optimisations on the bytecode.

Community
  • 1
  • 1
Martin Ellis
  • 9,603
  • 42
  • 53