Page 1 of 3 123 LastLast
Results 1 to 20 of 44
  1. #1
    docesam is offline Member
    Join Date
    Apr 2009
    Posts
    5
    Rep Power
    0

    Exclamation why java does not compile to native code?

    java is multi platform i.e the same source code will run on all supported operating systems ,this is definitely an advantage. java run on the web ( multi platform) this is also an advantage.

    however , on the desktop java compile to byte code rather than a native excitable. i.e the same byte code binary file runs on all supported platforms. this is a trivial advantage. furthermore it is a DISADVANTAGE from performance point of view.

    for example : skype is multi platform program (i guess it is written in delphi) navigating the skype website and downloading the native binary that runs on my operating system is very easy task.for the developer compiling his product multiple times foe various OSs is not a major problem.

    so why java accept the performance hindrance for the sake of trivial benefit ?
    Last edited by docesam; 11-21-2010 at 05:47 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Skype has separate packages for different OSs. You cannot the same for Windows and Linux. Why?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Years ago I wrote a little article on source code compilation, JIT (Just In Time) compilation and the hotspot mechanism.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Thank for sharing Jos. Why don't you added those links to your forum signature. It could be useful for other members.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by Eranga View Post
    Thank for sharing Jos. Why don't you added those links to your forum signature. It could be useful for other members.
    No, I want my signature (if I have one) to be totally useless ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    docesam is offline Member
    Join Date
    Apr 2009
    Posts
    5
    Rep Power
    0

    Default

    so yo are telling me that i can compile java to native code ? if so shall you guide me how to do that in netbeans ?

    thank you very much

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by docesam View Post
    so yo are telling me that i can compile java to native code ? if so shall you guide me how to do that in netbeans ?

    thank you very much
    Java doesn't work that way: javac compiles your java code to byte code; in the early days that byte code was the food for its interpreter (the old jvm); nowadays its food for the jit compiler that further compiles it to machine code and that machine code can not be stored in a file for reuse. It just runs as fast as possible and it is generally better and faster code than the code produced by traditional compilers that go from source code to machine code in one pass.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by JosAH View Post
    No, I want my signature (if I have one) to be totally useless ;-)

    kind regards,

    Jos
    Could be mine... :eek:

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by JosAH View Post
    Java doesn't work that way: javac compiles your java code to byte code; in the early days that byte code was the food for its interpreter (the old jvm); nowadays its food for the jit compiler that further compiles it to machine code and that machine code can not be stored in a file for reuse. It just runs as fast as possible and it is generally better and faster code than the code produced by traditional compilers that go from source code to machine code in one pass.

    kind regards,

    Jos
    As far as I know that there is a performance problem with javac, but with new technologies like jit, HotSpot optimizer allow Java to run as fast, or faster than Java byte code.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by Eranga View Post
    As far as I know that there is a performance problem with javac, but with new technologies like jit, HotSpot optimizer allow Java to run as fast, or faster than Java byte code.
    Javac isn't a performance bottleneck at all, it is amazingly fast (it does almost no optimization at all). The resulting byte code, if interpreted is slow but nowadays further compilation to machine code is done by the jvm (by the jit compiler to be exact); the resulting machine code is way faster than byte code interpretation and even faster than one pass compilation (i.e. source code compilation to machine code). The hotspot mechanism just tries to figure out which parts are to be compiled to machine code and which parts are supposed to be interpreted. Nowadays the hotspot mechanism is more 'aggressive' and compiles almost everything.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    docesam is offline Member
    Join Date
    Apr 2009
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Javac isn't a performance bottleneck at all, it is amazingly fast (it does almost no optimization at all). The resulting byte code, if interpreted is slow but nowadays further compilation to machine code is done by the jvm (by the jit compiler to be exact); the resulting machine code is way faster than byte code interpretation and even faster than one pass compilation (i.e. source code compilation to machine code). The hotspot mechanism just tries to figure out which parts are to be compiled to machine code and which parts are supposed to be interpreted. Nowadays the hotspot mechanism is more 'aggressive' and compiles almost everything.

    kind regards,

    Jos
    thanks for all who contributed positively to the post.

    the question now why don't the JIT compiler compile everything to "native code" and store it in the disk at the first time the program runs ,or even when the program get installed .that will make java faster i guess. something similar to delphi .
    any technical reason why not to do that?

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by docesam View Post
    thanks for all who contributed positively to the post.

    the question now why don't the JIT compiler compile everything to "native code" and store it in the disk at the first time the program runs ,or even when the program get installed .that will make java faster i guess. something similar to delphi .
    any technical reason why not to do that?
    The technical reason is that the JIT compiler may compile byte code to native machine code more than once; e.g. if it can be sure that no boundaries violations can ever occur it can remove their checks, speeding up the code even more. Storing the stuff in a file is no option.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by JosAH View Post
    Javac isn't a performance bottleneck at all, it is amazingly fast (it does almost no optimization at all). The resulting byte code, if interpreted is slow but nowadays further compilation to machine code is done by the jvm (by the jit compiler to be exact); the resulting machine code is way faster than byte code interpretation and even faster than one pass compilation (i.e. source code compilation to machine code). The hotspot mechanism just tries to figure out which parts are to be compiled to machine code and which parts are supposed to be interpreted. Nowadays the hotspot mechanism is more 'aggressive' and compiles almost everything.

    kind regards,

    Jos
    Performance sense I mean the byte code, it take more time than in jit and all, isn't it?

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by Eranga View Post
    Performance sense I mean the byte code, it take more time than in jit and all, isn't it?
    Yep, interpretation of byte code is slow compared to running machine code. That's why you have to make the JVM 'warm up' if you want to perform speed/performance tests: first the byte code is interpreted (slow), then the hotspot mechanism hits and decides to fire up the JIT compiler and the machine code is produced; at the end the machine code is running.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    why don't the JIT compiler compile everything to "native code" and store it in the disk at the first time the program runs ,or even when the program get installed

    The best performing compilation of some code will depend on what that code is actually doing when it is run on a particular occasion and you can't know that ahead of time. See the Wikipedia Adaptive Optimization article.

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by JosAH View Post
    Yep, interpretation of byte code is slow compared to running machine code. That's why you have to make the JVM 'warm up' if you want to perform speed/performance tests: first the byte code is interpreted (slow), then the hotspot mechanism hits and decides to fire up the JIT compiler and the machine code is produced; at the end the machine code is running.

    kind regards,

    Jos
    Cool, that's what I want to pointed.

    Anyway from the beginning I'm curious too, why there is intermediate interpretation of byte code, as OP worried.

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by Eranga View Post
    Anyway from the beginning I'm curious too, why there is intermediate interpretation of byte code, as OP worried.
    Because in the end, the final machine code is often better than the code generated by 'classic' compilers that have to go from source code straight to the target machine code.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  19. #19
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  20. #20
    couling is offline Member
    Join Date
    Nov 2010
    Posts
    54
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Because in the end, the final machine code is often better than the code generated by 'classic' compilers that have to go from source code straight to the target machine code.

    kind regards,

    Jos
    I find that very hard to believe. Even "classic" compilers have their own intermediate object code. Serialising that intermediate object code out to a file and shiping off the second half of compiling to another machine isn't going to speed things up. All compilers are a multi stage process. Classic compilers have at least 5 stages if memory serves.... but there's nothing to stop them having 6 or 7.

    I would be more inclined to believe that efficiency benafits of java are more to do with the specifics of the language itself and the way that programmers use it.



    But back to the origional question. Java byte code collects benifits of both source code and object code:
    • It's hard to read (needs to be decompiled first)
    • It's system indipendant
    • It takes very little work to get it running (far less than raw source at least)
    • It requires no further configuration on the part of the end user


    But why isnt compiled and installed?
    I dont know.
    Possibly because too few end users can be bothered going through the hastle it takes to compile and install every java program they come across, when there's a perfectly good JVM to do all that on the fly for you.

    But why not cache?
    Good question. That sounds like a good addition to a JVM if it hasn't been done already.

Page 1 of 3 123 LastLast

Similar Threads

  1. Java Code Won't Compile
    By JavaStudent1990 in forum New To Java
    Replies: 4
    Last Post: 07-29-2010, 09:34 AM
  2. New to JAVA and code cant compile
    By Gayethiri_86 in forum New To Java
    Replies: 2
    Last Post: 03-05-2010, 06:43 AM
  3. Replies: 6
    Last Post: 02-06-2009, 08:05 PM
  4. Compile/Execute code in Java app
    By Doctor Cactus in forum New To Java
    Replies: 5
    Last Post: 12-16-2008, 09:58 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •