开发者

Change a java class

开发者 https://www.devze.com 2023-02-14 01:25 出处:网络
I have found a potentially faster implementation of the BigInteger class. Anyone can tell me how can I change the original BigInteger class found in java.math with this one 开发者_运维问答please.

I have found a potentially faster implementation of the BigInteger class. Anyone can tell me how can I change the original BigInteger class found in java.math with this one 开发者_运维问答please. Thanks in advance.

Actually, it is not my implementation, it is presented in http://www.mail-archive.com/core-libs-dev@openjdk.java.net/msg01746.html,. Every improvement is their work not mine, I am just trying to use it.


Don't. Simply include the jar file (or class file, or source) of the potentially faster implementation, and instantiate it instead.


I think you are asking how to use your "java.math.BigInteger" instead of the JVMs version.

I don't think you want to do this because it would require you to update all the JDK/JRE that use your application. A better solution is to your "YourBigInteger" instead of the BigInteger in your application.

You could also be asking how to get it looked at by the masses to be included in a JDK/JRE. In that case, I would say start with OpenJDK.


There's almost no reason why you would want to exchange BigInteger in Java with yours, unless you're absolutely sure that it's not only faster in ALL relevant cases, but is also safe to use for all programs that would have depended on the original BigInteger, and doesn't conflict with Java internally (at a level even developers may not notice).

If all of the above are true, then you really submit your class to Oracle, because I'm sure they're looking for more efficient implementations constantly and you could really help us all out by releasing your faster BigInteger class in Java 7 or 8, perhaps.

But since you say you've only "potentially" found a faster implementation, what you really want is a subclass:

public class FastBigInteger extends java.math.BigInteger
{

and override all the methods you think can be sped up

    public BigInteger divide(BigInteger val)
    {
        //your faster implementation goes here
    }
}

and then you can start testing this class against java's class, timing the running time of each for many test cases, and presenting the findings to Oracle.


Java gets all of its built-in classes from rt.jar. You could replace the BigInteger class with another implementation within that JAR.

Of course, I wouldn't recommend it. Extending BigInteger would in almost all situations be a much better idea. But it can be done.


You should be able to override classes shipped in the JRE through the Java "endorsed" mechanism:

http://download.oracle.com/javase/6/docs/technotes/guides/standards/

However, I completely agree with the above - if possible you should be simply using your class instead of replacing BigInteger: this method doesn't require changing the JRE, and there's no risk of breaking some other package that depends on BigInteger.

However, if you have a 3rd-party package that uses BigInteger, and you want to switch the implementation under it for some reason (think long about this), the endorsed mechanism may be what you need.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号