如何在不使用 Integer 类的情况下在 Java 中获取 Integer.MAX_VALUE 的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22888961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 18:42:14  来源:igfitidea点击:

How to get the value of Integer.MAX_VALUE in Java without using the Integer class

javaintegerintmax

提问by madsword19

I have this question that has completely stumped me. I have to create a variable that equals Integer.MAX_VALUE... (in Java)

我有这个问题,完全难倒了我。我必须创建一个等于 Integer.MAX_VALUE... 的变量(在 Java 中)

// The answer must contain balanced parentesis
public class Exercise{

  public static void main(String [] arg){
    [???]
    assert (Integer.MAX_VALUE==i);
  }
}

The challenge is that the source code cannot contain the words "Integer", "Float", "Double" or any digits (0 - 9).

挑战在于源代码不能包含单词“Integer”、“Float”、“Double”或任何数字(0 - 9)。

回答by Hypothetical inthe Clavicle

As others have said.

正如其他人所说。

int i = Integer.MAX_VALUE;

is what you want.

是你想要的。

Integer.MAX_VALUE, is a "static constant" inside of the "wrapper class" Integer that is simply the max value. Many classes have static constants in them that are helpful.

Integer.MAX_VALUE,是“包装类”Integer 中的“静态常量”,它只是最大值。许多类中都有有用的静态常量。

回答by MrLore

The issue is that the answer cannot contain: "Integer", "Float", "Double", and digits (0 - 9)

问题是答案不能包含:“Integer”、“Float”、“Double”和数字 (0 - 9)

There are other things in Java which can be represented as an Integer, for example a char:

Java 中还有其他东西可以表示为 a Integer,例如 a char

char aCharacter = 'a';
int asInt = (int) aCharacter;
System.out.println(asInt); //Output: 97

You can also add chars together in this manner:

您还可以通过这种方式将字符添加在一起:

char aCharacter = 'a';
char anotherCharacter = 'b';
int sumOfCharacters = aCharacter + anotherCharacter;
System.out.println(sumOfCharacters); //Output: 195

With this information, you should be able to work out how to get to 2147483647on your own.

有了这些信息,您应该能够弄清楚如何2147483647自己去。

回答by Críostóir ó Catháin

OK, so an Integer can only take certain values. This is from MIN_VALUE to MAX_VALUE where the minimum value is negative.

好的,所以 Integer 只能取某些值。这是从 MIN_VALUE 到 MAX_VALUE,其中最小值为负。

If you increase an integer past this upper bound the value will wrap around and become the lowest value possible. e.g. MAX_VALUE+1 = MIN_VALUE.

如果您增加一个整数超过此上限,该值将环绕并成为可能的最低值。例如 MAX_VALUE+1 = MIN_VALUE。

Equally, if you decrease an integer past the lower bound it will wrap around and become the largest possible value. e.g. MIN_VALUE-1 = MAX_VALUE.

同样,如果您将一个整数减小到超过下限,它将环绕并成为可能的最大值。例如 MIN_VALUE-1 = MAX_VALUE。

Therefore a simple program that instantiates an int, decrements it until it wraps around and returns that value should give you the same value as Integer.MAX_VALUE

因此,一个简单的程序实例化一个 int,递减它直到它环绕并返回该值应该给你相同的值 Integer.MAX_VALUE

public static void main(String [] arg) {

    int i = -1

    while (i<0) {
        i--;
    }
    System.out.println(i);
}

回答by Stephen C

Here's a solution:

这是一个解决方案:

int ONE = "X".length();
int max = ONE;
while (max < max + ONE) {
   max = max + ONE;
}

or lots of variants.

或很多变种。

(The trick you were missing is how to "create" an integer value without using a numeric literal or a number wrapper class. Once you have created ONE, the rest is simple ...)

(您缺少的技巧是如何在不使用数字文字或数字包装类的情况下“创建”一个整数值。一旦您创建了ONE,剩下的就很简单了……)

回答by Bohemian

Here's a succinct method:

这是一个简洁的方法:

int ONE = "x".length();
int i = -ONE >>> ONE; //unsigned shift

This works because the max integer value in binary is all ones, except the top (sign) bit, which is zero. But -1 in twos compliment binary is all ones, so by bit shifting -1 one bit to the right, you get the max value.

这是有效的,因为二进制中的最大整数值都是 1,除了最高(符号)位为零。但是 -1 在二进制补码二进制中都是 1,因此通过将 -1 向右移动一位,您可以获得最大值。

11111111111111111111111111111111 // -1 in twos compliment
01111111111111111111111111111111 // max int (2147483647)

回答by slyaer

A bit late, but here goes:

有点晚了,但这里是:

int two = "xx".length();
int thirtyone = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx".length();
System.out.println(Math.pow(two, thirtyone)-1);

How did I go? :p

我怎么去了?:p

I do like that bitshift one though...

不过我确实喜欢那个位移位...