Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9095053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:30:12+00:00 2026-06-16T23:30:12+00:00

Let’s imagine we have the following classes: public class Message extends Object {} public

  • 0

Let’s imagine we have the following classes:

public class Message extends Object {}

public class Logger implements ILogger {
 public void log(Message m) {/*empty*/}
}

and the following program:

public static void main(String args[]) {
  ILogger l = new Logger();
  l.log((Message)null); // a)
  l.log(new Message()); // b)
}

Will the Java compiler strip out statements a and b ? In both cases (stripping or not stripping), what is the rationale behind the Java compiler’s decision ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T23:30:16+00:00Added an answer on June 16, 2026 at 11:30 pm

    Will the Java compiler strip out statements a and b ?

    The javac (source to bytecode) compiler won’t strip either call. (It is easy to check this by examining the bytecodes; e.g. looking at the javap -c output.)

    In both cases (stripping or not stripping), what is the rationale behind the Java compiler’s decision ?

    Conformance with the JLS :-).

    From a pragmatic perspective:

    • If the javac compiler optimized the calls away, a Java debugger wouldn’t be able to see them at all … which would be rather confusing for the developer.
    • Early optimization (by javac) would result in breakage if the Message class and the main class were compiled / modified independently. For example, consider this sequence:

      • Message is compiled,
      • the main class is compiled,
      • Message is edited so that log does something … and recompiled.

      Now we have an incorrectly compiled main class that doesn’t do the right thing at a and b because the prematurely inlined code is out of date.


    However, the JIT compiler might optimize the code at runtime in a variety of ways. For instance:

    • The method calls in a and b may be inlined if the JIT compiler can deduce that no virtual method dispatching is required. (If Logger is the only class used by the application that implements ILogger this a no-brainer for a good JIT compiler.)

    • After inlining the first method call, the JIT compiler may determine that the body is a noop and optimize the call away.

    • In the case of the second method call, the JIT compiler could further deduce (by escape analysis) that the Message object doesn’t need to be allocated on the heap … or indeed at all.

    (If you want to know what the JIT compiler (on your platform) actually does, Hotspot JVMs have a JVM option that dumps out the JIT-compiled native code for selected methods.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following structure: abstract class Hand {} class Rock extends
Let's say that I have classes like this: public class Parent { public int
Let's say for a moment that I have the following module in python: class
Let's say I have a domain object with the following field: private Map<StatType, Double>
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress
Let's assume I have the following typedef: typedef void (^myBlock)(id); And I have some
Let me explain best with an example. Say you have node class that can
Let's say I have a custom class CustomClass, and I have a collection deriving
Let's say I have the following data frame: > myvec name order_no 1 Amy
Let's say I have one class User, and it has a property of type

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.