DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Java
  4. How Java Executes and Performs Better

How Java Executes and Performs Better

A N M Bazlur Rahman user avatar by
A N M Bazlur Rahman
CORE ·
Sep. 22, 14 · Interview
Like (5)
Save
Tweet
Share
15.35K Views

Join the DZone community and get the full member experience.

Join For Free

 Please bear with my attempt to inform you a little regarding the execution of Java code.

Java’s code is compiled into byte code, which Java’s virtual machine  executes.  This output is an intermediate language (IL) that is neither  human readable nor machine-executable. Only the virtual machine  understands it.  To execute this output, the virtual machine uses a Just  in Time (JIT) compiler to interpret the byte code – so is Java an  interpreted language as some people describe it, or is Java a compiled  language?  The answer: Java is both.  Its source code is compiled with a  static compiler into byte code – and then, the JIT interprets the  compiler’s output.

From the previous description, one might be tempted to conclude that  the JIT hurts performance, as it compiles during execution, a design  that would seem to cause Java to run slowly. This conclusion, while  intuitive, is incorrect.  The JIT actually performs quite well. At  run time, it compiles byte code into machine code, which is later run by  the CPU.  When Java executes, it executes machine code just as native  code does – so it should run – discounting the JIT’s compilation costs –  as fast as native code does.

However, C/C++ is generally regarded as providing better performance  than Java does. The on-the-fly nature of Java’s execution lends  credence to this thinking.  Compiling a piece of code into machine code  takes a non-trivial amount of time.  If a language executes similar  machine instructions but does not need compilation prior to the  execution of them, then one can assume that the language in question  runs faster than Java does – even when Java’s machine code output is  maximally optimized.  However, Java isn’t maximally optimized, but C++  is not maximally optimized either.  No compiler can match the  performance of a top-notch assembly programmer who is an expert at  manipulating the targeted hardware – so both Java and C++ can never be  maximally optimized.  Making matters worse, both languages have inherent  issues. Java must bear the reality that at least some of its code has  to be compiled every time that it runs, and C++ is, when it must be  generic, often not fully tailored to its hardware, as Java always is.  Even with the aforementioned handicap, one still expects Java’s  compilation cost to give C++ an edge in every scenario outside of very  poorly coded or compiled C++.

However, appearances are deceiving; the JVM is a very intelligent  environment and the JIT an intelligence compiler. For example, the JVM  knows which methods are called frequently, as it maintains a count of  each method’s calls.  When this count exceeds a certain threshold, the  method’s machine code is kept, so that the JIT does not have to compile  the method when it is called again – a decision that greatly reduces the  cost of translating byte code into machine code.  Furthering this  performance improvement, the JIT optimizes the most frequently used  code. The JIT collects statistics to determine which parts of code fit  this mold. Intelligent optimization of this ilk can, as Research  indicates, yield big gains. Most research states that 80% of execution  time is spent executing 20% of the code (“hot code”) – so optimizing  these sections of code can yield sizeable performance benefits. The JIT  optimizes “hot code.” It executes these sections in a highly optimized  way directly on the operating system.  To achieve this goal, the JIT  uses statistics to identify the “hot code.” In fact, Java performs even  more optimization than efficiently executing “hot code.” It identifies  the code that it executes the most and recompiles to run more  efficiently than it previously did. These optimizations allow Java to  achieve, in certain situations, better performance than C++ gives,  without the need to resort to dangerous compiler settings or to  dangerous optimization techniques.

Java (programming language)

Published at DZone with permission of A N M Bazlur Rahman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Steel Threads Are a Technique That Will Make You a Better Engineer
  • Required Knowledge To Pass AWS Certified Solutions Architect — Professional Exam
  • Front-End Troubleshooting Using OpenTelemetry
  • Create a REST API in C# Using ChatGPT

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: