1

If I want to write a program that deals almost exclusively with, say, base 8 math, is there a way to change the source code or JVM to perform all calculations with this radix without having to explicitly change it on every integer reference?

For example, instead of...

private static final int RADIX = 8;

// ... then, elsewhere ...

System.out.println(Integer.toString(3 + 7, RADIX));

... I could just do ...

System.out.println(3 + 7);

... and have it print the same result of 12? Is there some environment variable or in-code setting I can apply? Or is this simply not possible?

This may seem arcane or a "why in the world would you want to do this" scenario, but if you can imagine having to perform a large number of non-trivial calculations under a different base, then you can see how it would become extremely tedious extremely fast to have to keep manually converting numbers to the appropriate radix.

4

3 回答 3

1

不。

还有,你为什么要这样?想象一下,在同一个 JVM 中运行时您会破坏多少段代码——没有一个编码库会期望默认基数会突然改变。

您使用常量是正确的方法。

于 2012-10-23T18:02:34.833 回答
1

不,没有这样的功能。

无论您谈论的是什么基数,数字都是一个数字,只有在转换为/从字符串时才会出现基数。如果您必须一直这样做,那么创建一些实用方法来完成这项工作,并始终调用它们。或者,编写您自己的类整数类来处理 fromString/toString 位。

public final class OctalInteger extends Number implements Comparable<OctalInteger> {
    // Basically a copy of Integer.java, but changes the methods dealing with Strings

}
于 2012-10-23T18:07:46.647 回答
0

您不能更改默认基数。但是您可以轻松编写自己的程序printprintln以八进制打印整数的程序。

于 2012-10-23T18:04:28.363 回答