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 6999561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:33:27+00:00 2026-05-27T20:33:27+00:00

In java, is there a general way to enforce compile-time compilation? In a programming

  • 0

In java, is there a general way to enforce compile-time compilation? In a programming competition I’m in we are limited by the bytecode our robot can use every round while the program is running, so if I’m able to compute things at compile time I have an advantage.

For a concrete example, lets say I want to define a variable NORTH that will be an array of
MapLocations representing the squares the robot can see if facing North. If I hand code this,
I can write:

public class SightSensor{
  public static MapLocation[] NORTH = {new MapLocation(-2,2),
                    new MapLocation(-1,1),
                        new MapLocation(-1,2),
                        new MapLocation(0,1),
                        new MapLocation(0,2),
                        new MapLocation(0,3),
                        new MapLocation(1,1),                       
                        new MapLocation(1,2),
                        new MapLocation(2,2)};

}

which represents that the robot can see all squares in a 90 degree arc about due north within a distance squared of 9. Now, my robots can see in all cardinal directions, and some can see farther
than others. It would be rather tedious and seemingly bad form to hand code each possible
sight (distance, direction) pair. Hence, I wrote a function

public static MapLocation[] getSensorLocs(int r, Direction dir){ ... bla ... }

that automatically computes the locations, so I can re-write the SightSensor class as

public class SightSensor{
  public static MapLocation[] NORTH = getSensorLocs(3, Direction.NORTH);
}

The only problem is now when by robot tries to use the NORTH variable, it has to spend the
time to run the getSensorLocs method, so it is more expensive than the hand-written version.
Can I force that computation to be run at compile-time, i.e. be “symbolically replaced” in an appropriate sense?

  • 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-05-27T20:33:28+00:00Added an answer on May 27, 2026 at 8:33 pm

    Now that I understand you are optimising for bytecode size, you can use the following approach.

    // 8 bytes per field.
    public static MapLocation[] NORTH = locations("\u006a\u0079\u007a\u0089\u008a\u008b\u0099\u009a\u009b\u00aa");
    
    // 57 bytes.
    public static MapLocation[] locations(String s) {
        final int len = s.length();
        MapLocation[] locations = new MapLocation[len];
        for (int i = 0; i < len; i++) {
            char ch = s.charAt(i);
            locations[i] = new MapLocation((ch >> 4) - 8, (ch & 0xf) - 8);
        }
        return locations;
    }
    
    static class MapLocation {
        public MapLocation(int x, int y) {
            System.out.println("x=" + x + ", y=" + y);
        }
    }
    

    prints

    x=-2, y=2
    x=-1, y=1
    x=-1, y=2
    x=0, y=1
    x=0, y=2
    x=0, y=3
    x=1, y=1
    x=1, y=2
    x=1, y=3
    x=2, y=2
    

    Note: the length of the encoding String doesn’t add any byte code!.


    When you run the first code it actually creates the array and each element at runtime. There is no compile time optimisation. I suggest you check whether and why your getSensorLocs method is slower because it shouldn’t be significantly slower.

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

Sidebar

Related Questions

In Java (And in general) is there a way to make a class so
Is there some way I can find the path to the java.exe that launched
In Java is there a way to check the condition: Does this single character
In Java is there a way to extract the value of an unknown subset
In IE Java-script load slower than Firefox, Safari and Chrome? Is there a way
In Java, I recall there is a way to store objects in a usable
In general on any non-HTTP server. Would there be a way to detect what
I'm fairly new to programming in general but have been working with Java for
Coming from Java programming, I'm used to the general Main<->Test Maven-setup for a project.
Is there any way to read and print the object attribute dynamically(Java) ? for

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.