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

  • Home
  • SEARCH
  • 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 9182405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:33:11+00:00 2026-06-17T18:33:11+00:00

Consider following code as totally simplified example of the idea: public class Main {

  • 0

Consider following code as totally simplified example of the idea:

public class Main {
    public static void foreach(int[] x, intVoid action) {
        for (int i = 0; i < x.length; ++i) {
            action.apply(x[i]);
        }
    }

    public static void main (String[] args) {
        int[] h = new int[] {2, 3, 5, 7, 11, 13};
        for (int i = 0; i < 10; ++i) {
            foreach(h, new intVoid() {
                public void apply(int x) {
                    System.out.println(x * 2);
                }
            });
        }
    }
}

interface intVoid {public void apply(int x);}

In the for loop we call foreach with absolutely identical arguments from logical point of view, since the anonymous implementation of our interface doesn’t depend of anything in the context. The question is – will it be instantinated 10 times or only once? Or, similar, will it be equivalent for the following code:

public class Main {
    public static void foreach(int[] x, intVoid action) {
        for (int i = 0; i < x.length; ++i) {
            action.apply(x[i]);
        }
    }

    public static void main (String[] args) {
        int[] h = new int[] {2, 3, 5, 7, 11, 13};
        intVoid action = new intVoid() {
            public void apply(int x) {
                System.out.println(x * 2);
            }
        };
        for (int i = 0; i < 10; ++i) {
            foreach(h, action);
        }
    }
}

interface intVoid {public void apply(int x);}

I sometimes had been in such situations, where it’s very convenient to define an implementation exactly in place where it’s needed, but sometimes I also need to be sure that there will be no attempt to create the same object multiple times.
If there is an optimization in the runtime, then I’m interested in how this is handled in different implementations, especially what will happen if I convert such code to be run in Android’s Dalvik VM.
I know that I can test this all myself, but I’m wondering if someone has already shed some light on this question.

  • 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-17T18:33:13+00:00Added an answer on June 17, 2026 at 6:33 pm

    The question is – will it be instantinated 10 times or only once?

    10 times, because that’s what you’ve told it to do. I’d be surprised to see that the JVM (or Dalvik) noticed that apply had no side-effects and that the object had no state, and therefore you didn’t need to create separate instances. It’s possible – Hotspot’s pretty impressive – but it seems an unlikely optimization to me.

    Of course, creating each object is probably cheap – but it would still be better to create one object as per your second code snippet.

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

Sidebar

Related Questions

Consider following code: public sealed class Program { public static void Main() { System.Console.WriteLine(Hi);
Consider the following code, public class StartUp { public StartUp(String[] test){} public static void
Consider the following code in C: void main() { int a=0; for(printf(\nA); a; printf(\nB));
Consider the following code : #include <iostream> #include <type_traits> class Base { public: static
Consider the following code: class A { A(const A&); public: A() {} }; int
Consider the following code: template <class x1, class x2 = int*> struct CoreTemplate {
Consider the following code: public abstract class RepositoryBase<T> where T : class { #region
Consider the following code: enum E { A { public int get() { return
Consider you have the following code: public abstract class MenuItem { protected string m_Title;
Consider the following code: void f(byte x) {print(byte);} void f(short x) {print(short);} void f(int

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.