Java IoT Authors: Yeshim Deniz, Pat Romanski, Liz McMillan, Elizabeth White, Paul Simmons

Related Topics: Java IoT

Java IoT: Article

Java Code Compilation

Java Code Compilation

In the November JDJ (Vol. 3, Issue 11) we peered into the Cosmic Cup to look at some of the Java Virtual Machines on the market. We also discussed how a VM enables Java to promote its "write once, run anywhere" (WORA) cause. To recapitulate, the Java programming environment may be categorized into two computing environments. The compile-time environment provides the translation of Java source code to bytecodes (.class files). The runtime environment provides the interpretation of the bytecodes into native, platform-specific, executable instructions. A Java Virtual Machine's purpose is to load class files and execute the bytecodes they contain.

The speed of execution in the runtime environment is crucial for the success of the Java platform. After all, it targets several facets of the computing industry, but the crux of the platform is still the Java programming language itself. Several IT managers are putting off a wholehearted commitment to pure Java solutions because they still aren't convinced that it will meet the performance requirements for their applications. Happy programmers and cool languages don't put bread on the table.

This month we'll take a closer look at the two stages of compilation that lead to executable Java code. We'll also examine the available Java code compilation alternatives. Java is an interpreted language. An executed Java program typically consists of a virtual machine that interprets bytecodes. Interpreted languages can never be as fast as compiled languages because the process of interpretation consists of converting high-level programming instructions into machine instructions, one line at a time. On the other hand, natively compiled code is machine code - that is, the program is already in the form of machine instructions when it's ready to be executed. Obviously, the latter will execute faster.

Do we really need a "write once, run anywhere" solution? WORA essentially means that the deployed version of code is bytecode. The application is developed in the Java programming language and then compiled down to bytecode. This bytecode is shipped to the machine where the application actually needs to run. "Anywhere" requires that the same code should be capable of running on different hardware and operating systems.

What's the alternative to code that runs in a JVM? To achieve true native speeds for a particular platform, the application should execute native code (which runs as a process spawned by the operating system as opposed to code that's interpreted by a VM). This should be a no-brainer. Platform-specific optimizations can be performed most efficiently on native code. So how do we get native code from Java source code that runs on every platform? This is a bit of a dilemma.

The answer boils down to the responsibilities of the players in the application development process - the developer and the compiler vendor. If the developers end up using platform-specific compilers for individual platforms, they'll run into the very porting headache that Java has tried to eliminate. The responsibility of providing platform-independent executable code falls on the guys who write the compiler. If the same code could be compiled to different platform executables, we'd get the best of both worlds. This would be the "WOCA" - "write once, compile anywhere" - solution. The approaches taken by WORA and WOCA are illustrated in Figure 1.

Note: Neither WORA nor WOCA is feasible in a language like C or C++. The reason is that there are many platform-specific extensions of the programming language itself, which means that the source code written by developers for one platform will differ from the source code written for another platform.

One of the greatest things Sun Microsystems has done for the computing world is to keep a tight rein on what goes into the Java programming language and to make sure that it has no platform-specific extensions. The result is that source code written in the Java programming language looks the same on any platform.

Compiled Just in Time!
Just-in-time (JIT) compilers translate Java bytecode into instructions that can be sent directly to the processor. A JIT compiler provides a second stage of compilation at the platform where code compiles the bytecode. Once recompiled by the JIT compiler, the code will usually run more quickly in the computer. Typically, JIT compilers come with the VM and their use is optional. The JVM passes on the .class file to the JIT compiler; the JIT compiler compiles them into native code for the machine that the application runs on. The JIT is an integral part of the Java Virtual Machine implementation. Since Java is a dynamic language, the bytecodes are "dynamically" compiled into machine code only when they're loaded.

JITs make the executable code faster only after it's called for the first time. In fact, the first time the class is used, it may actually be slower since it goes through an extra compilation step. Therefore, JITs are effective only when the code is used repeatedly. Going by the 80-20 rule (the program spends 80% of the time in 20% of the code), this happens most of the time and therefore JIT compilers speed up the execution considerably. In fact, when Java JIT compilers first came out, traditional Java interpreters were resulting in execution speeds that were 20 to 30 times slower than comparable programs written in C. JIT compilers brought the speeds of Java code from 40% to 60% of C/C++ execution speeds. Figure 2 illustrates the approach taken by JIT compilers.

JIT is currently the predominant technology for speeding up Java applets and applications. Almost all the compiler and IDE vendors - including Symantec's Visual Cafe, Borland's JBuilder, IBM's VisualAge, Asymetrix's Supercede and Microsoft's Visual J++ - provide Java compilers that have the JIT compilation option. Find the "Hot Spots"!

While JIT compilers are a step up, they're still unable to reach the raw speeds of native code. Sophisticated optimization on JIT-compiled code is hard to achieve. Also, applying these optimizations slows the process of JIT compilation. For applications that require a larger performance gain, another viable approach is "adaptive optimization."

Adaptive optimization further leverages the 80-20 rule. The logic that drives adaptive compilers is that since the majority of a program's executable time is spent in a small fraction of the code, it makes sense to concentrate on making that fraction as fast as possible. Adaptive compilation uses a more "dynamic" and "intelligent" form of compilation. The runtime system identifies the sections of code that are performance bottlenecks by continuously monitoring the executing code. It then applies sophisticated optimization techniques to speed up execution in these critical sections of code.

This technique is used by Sun's much-awaited HotSpot Virtual Machine. Fundamentally, HotSpot is based on compiler technology that is an extension of the JIT compiler technology. The HotSpot VM constantly monitors the performance of the executing bytecode. It "inlines" (remember the C++ "inline" keyword?) methods in the critical region for maximum performance. Inlined methods are like static methods in the class and are replaced by actual machine instructions (instead of method calls) in compiled code. Optimization in the HotSpot compiler is achieved with the help of a very advanced garbage collector and a new thread synchronization mechanism.

Sun claims that HotSpot will make Java execution speeds comparable to C/C++ speeds. The compiler is expected to be released early this year and is more than a year late.

Native Compilers
JIT compilers and adaptive compilers are very important for improving performance on the client side, where the program may be deployed on various platforms. Applets execute by downloading bytecodes (.class files) from specific URLs. Thus the Java source code is compiled into bytecodes at the machine corresponding to the URL. These bytecodes may be downloaded to a multitude of clients - that is, to several platforms. Hence, native optimizations can't be performed before the code is deployed. JIT compilers apply optimizations that aren't really platform specific (as they target several platforms). They're also limited by the amount of time sacrificed in performing the compilations on the fly. An additional disadvantage of compiling bytecodes at the deployment machine is that decompiling them isn't easy. Therefore, companies can't prevent software piracy of their code.

The same restriction doesn't apply to code deployed at a single machine. In a client/server architecture this would be the server. Hence, code deployed at the server can be compiled down to native code, and global native optimizations can be applied to it. The trade-off is that this code won't run on other platforms. The reality is that it won't need to.

Native compilers compile Java source code down to platform-specific executables. In that sense they're no different from the traditional C/C++ compilers. As mentioned earlier, the unique feature that Java brings to the table is that the source code is always the same. Most IDEs provide the option of compiling Java source to native code. However, most IDEs support only one platform. For example, JBuilder, Supercede, Visual Cafe, Visual J++ and so on run only on NT. As a result, although you develop platform-neutral source code in Java, you end up deploying on a single platform when you use a traditional native compiler.

This points toward the need for a cross-platform Java compiler, as illustrated earlier in Figure 1. In other words, we need a compiler from a single vendor that can compile Java source code down to native code, and that can take advantage of the specific platform it runs on. Such a compiler should also support compilation of bytecodes to support Java's dynamic nature. Currently, TowerJ from Tower Technologies provides these features. It provides support for several platforms, including NT and Solaris. Another cross-platform native compiler under development is JOVE from Instantiations.

What About Legacy Code?
Contrary to popular belief, the programming world will never be all Java. C/C++, Pascal, BASIC, Fortran, COBOL, Smalltalk and others will continue doing what they do best. Sun Microsystems recognizes this and that's why the Java Native Interface (JNI) is a part of the Java VM specification. Any vendor that provides a Java VM needs to support JNI (Microsoft thought they were the exception, but were recently proved wrong). JNI allows Java applications to interact with legacy applications written in other languages. Using JNI, the application or module is linked with Java code as a kind of shared library. If your Java application needs to interact with legacy code, you need to make sure that the compiler you choose supports JNI.

Cosmic Reflections
Application development in Java is eventually going to require a mix of the various compiler technologies available. If speed of execution isn't a major concern, a traditional Java compiler may be used. If the performance boost provided by JIT compilers suffices, that option can always be turned on. If HotSpot lives up to its expectations, developers and managers for realtime applications can breathe easy. And if it doesn't, native compilers will grab a larger market share. On the server side, native compilers will always find a home. Whatever compiler technology is chosen for development, one significant service that Java has performed for the developers is that it has shifted a large part of the burden of supporting cross-platform applications to the compiler vendors.

More Stories By Ajit Sagar

Ajit Sagar is Associate VP, Digital Transformation Practice at Infosys Limited. A seasoned IT executive with 20+ years experience across various facts of the industry including consulting, business development, architecture and design he is architecture consulting and delivery lead for Infosys's Digital Transformation practice. He was also the Founding Editor of XML Journal and Chief Editor of Java Developer's Journal.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.

@ThingsExpo Stories
DXWorldEXPO LLC announced today that All in Mobile, a mobile app development company from Poland, will exhibit at the 22nd International CloudEXPO | DXWorldEXPO. All In Mobile is a mobile app development company from Poland. Since 2014, they maintain passion for developing mobile applications for enterprises and startups worldwide.
Bill Schmarzo, author of "Big Data: Understanding How Data Powers Big Business" and "Big Data MBA: Driving Business Strategies with Data Science," is responsible for setting the strategy and defining the Big Data service offerings and capabilities for EMC Global Services Big Data Practice. As the CTO for the Big Data Practice, he is responsible for working with organizations to help them identify where and how to start their big data journeys. He's written several white papers, is an avid blogge...
I think DevOps is now a rambunctious teenager - it's starting to get a mind of its own, wanting to get its own things but it still needs some adult supervision," explained Thomas Hooker, VP of marketing at CollabNet, in this SYS-CON.tv interview at DevOps Summit at 20th Cloud Expo, held June 6-8, 2017, at the Javits Center in New York City, NY.
"MobiDev is a software development company and we do complex, custom software development for everybody from entrepreneurs to large enterprises," explained Alan Winters, U.S. Head of Business Development at MobiDev, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
Major trends and emerging technologies – from virtual reality and IoT, to Big Data and algorithms – are helping organizations innovate in the digital era. However, to create real business value, IT must think beyond the ‘what’ of digital transformation to the ‘how’ to harness emerging trends, innovation and disruption. Architecture is the key that underpins and ties all these efforts together. In the digital age, it’s important to invest in architecture, extend the enterprise footprint to the cl...
Data is the fuel that drives the machine learning algorithmic engines and ultimately provides the business value. In his session at Cloud Expo, Ed Featherston, a director and senior enterprise architect at Collaborative Consulting, discussed the key considerations around quality, volume, timeliness, and pedigree that must be dealt with in order to properly fuel that engine.
Two weeks ago (November 3-5), I attended the Cloud Expo Silicon Valley as a speaker, where I presented on the security and privacy due diligence requirements for cloud solutions. Cloud security is a topical issue for every CIO, CISO, and technology buyer. Decision-makers are always looking for insights on how to mitigate the security risks of implementing and using cloud solutions. Based on the presentation topics covered at the conference, as well as the general discussions heard between sessio...
IoT is rapidly becoming mainstream as more and more investments are made into the platforms and technology. As this movement continues to expand and gain momentum it creates a massive wall of noise that can be difficult to sift through. Unfortunately, this inevitably makes IoT less approachable for people to get started with and can hamper efforts to integrate this key technology into your own portfolio. There are so many connected products already in place today with many hundreds more on the h...
No hype cycles or predictions of zillions of things here. IoT is big. You get it. You know your business and have great ideas for a business transformation strategy. What comes next? Time to make it happen. In his session at @ThingsExpo, Jay Mason, Associate Partner at M&S Consulting, presented a step-by-step plan to develop your technology implementation strategy. He discussed the evaluation of communication standards and IoT messaging protocols, data analytics considerations, edge-to-cloud tec...
DXWorldEXPO LLC announced today that the upcoming DXWorldEXPO | CloudEXPO New York event will feature 10 companies from Poland to participate at the "Poland Digital Transformation Pavilion" on November 12-13, 2018.
Digital Transformation is much more than a buzzword. The radical shift to digital mechanisms for almost every process is evident across all industries and verticals. This is often especially true in financial services, where the legacy environment is many times unable to keep up with the rapidly shifting demands of the consumer. The constant pressure to provide complete, omnichannel delivery of customer-facing solutions to meet both regulatory and customer demands is putting enormous pressure on...
CloudEXPO | DXWorldEXPO are the world's most influential, independent events where Cloud Computing was coined and where technology buyers and vendors meet to experience and discuss the big picture of Digital Transformation and all of the strategies, tactics, and tools they need to realize their goals. Sponsors of DXWorldEXPO | CloudEXPO benefit from unmatched branding, profile building and lead generation opportunities.
The best way to leverage your CloudEXPO | DXWorldEXPO presence as a sponsor and exhibitor is to plan your news announcements around our events. The press covering CloudEXPO | DXWorldEXPO will have access to these releases and will amplify your news announcements. More than two dozen Cloud companies either set deals at our shows or have announced their mergers and acquisitions at CloudEXPO. Product announcements during our show provide your company with the most reach through our targeted audienc...
Everything run by electricity will eventually be connected to the Internet. Get ahead of the Internet of Things revolution. In his session at @ThingsExpo, Akvelon expert and IoT industry leader Sergey Grebnov provided an educational dive into the world of managing your home, workplace and all the devices they contain with the power of machine-based AI and intelligent Bot services for a completely streamlined experience.
@DevOpsSummit at Cloud Expo, taking place November 12-13 in New York City, NY, is co-located with 22nd international CloudEXPO | first international DXWorldEXPO and will feature technical sessions from a rock star conference faculty and the leading industry players in the world.
DXWorldEXPO | CloudEXPO are the world's most influential, independent events where Cloud Computing was coined and where technology buyers and vendors meet to experience and discuss the big picture of Digital Transformation and all of the strategies, tactics, and tools they need to realize their goals. Sponsors of DXWorldEXPO | CloudEXPO benefit from unmatched branding, profile building and lead generation opportunities.
22nd International Cloud Expo, taking place June 5-7, 2018, at the Javits Center in New York City, NY, and co-located with the 1st DXWorld Expo will feature technical sessions from a rock star conference faculty and the leading industry players in the world. Cloud computing is now being embraced by a majority of enterprises of all sizes. Yesterday's debate about public vs. private has transformed into the reality of hybrid cloud: a recent survey shows that 74% of enterprises have a hybrid cloud ...
In his keynote at 19th Cloud Expo, Sheng Liang, co-founder and CEO of Rancher Labs, discussed the technological advances and new business opportunities created by the rapid adoption of containers. With the success of Amazon Web Services (AWS) and various open source technologies used to build private clouds, cloud computing has become an essential component of IT strategy. However, users continue to face challenges in implementing clouds, as older technologies evolve and newer ones like Docker c...
JETRO showcased Japan Digital Transformation Pavilion at SYS-CON's 21st International Cloud Expo® at the Santa Clara Convention Center in Santa Clara, CA. The Japan External Trade Organization (JETRO) is a non-profit organization that provides business support services to companies expanding to Japan. With the support of JETRO's dedicated staff, clients can incorporate their business; receive visa, immigration, and HR support; find dedicated office space; identify local government subsidies; get...
Dion Hinchcliffe is an internationally recognized digital expert, bestselling book author, frequent keynote speaker, analyst, futurist, and transformation expert based in Washington, DC. He is currently Chief Strategy Officer at the industry-leading digital strategy and online community solutions firm, 7Summits.