Earlier today, a coworker asked what exactly Apex was. My lazy answer for those unfamiliar with Salesforce development is usually "it's like Java, but for Salesforce.com," to which he replied "is it built on Java?"

I realized that I truly do not know - a few searches turn up a lot of answers such as "Apex itself is based on Java" but I realized "based on" might be referring to the syntax and not the actual language that it compiles to. The syntax and conventions are obviously very similar, but was this just for familiarity's sake?

tl;dr - what does Apex compile into on the Salesforce provider side?

  • 7
    good archeological dig here Apr 6 '15 at 18:57
  • Funny - I was just about to post the exact same link... Apr 6 '15 at 19:01
  • Probably should go ahead and post that as an answer...
    – Adrian Larson
    Apr 6 '15 at 19:13
  • I was under assumption that vf page controllers are built on Java struts and so is the apex, Sorry if I am making irrelevant comment. Even after going through the link given in the comment still looking for a simplified answer.
    – Suresh
    Apr 6 '15 at 19:45
  • 1
    As a research project. Try searching for "common.apex.runtime.bytecode" in your favourite search engine for hints at the internals. All sorts of exceptions show up. Like common.apex.runtime.bytecode.BytecodeExecutionContext cannot be cast to common.apex.runtime.impl.Execution. Apr 6 '15 at 22:57

@bigassforce showed me this, which explains that prior to January 2012, Apex would be compiled into an Abstract Syntax Tree (AST) node graph which would then be traversed for every request to that class! The AST object would be serialized and cached in memcached, and then be deserialized and re-traversed upon subsequent requests. This was obviously quite expensive!

Starting January 2012, the new (and current - April 2015) Apex runtime compiles directly to Java bytecode, eliminating the AST completely. While it must still be serialized and deserialized, the cost of this for bytecode is trivial when compared to the Apex AST.

The more you know ;)

  • Thanks the that info @Brian. You might like to see this answer to a linked question "Does Apex transpile into Java?" salesforce.stackexchange.com/a/69344/54736 Turns out the compiled code runs on a layered implementation of JVM.
    – Rob Wells
    Apr 3 '18 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.