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

好久不见. 提交于 2020-03-18 13:48:48

问题


I have this question that has completely stumped me. I have to create a variable that equals Integer.MAX_VALUE... (in 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).


回答1:


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.

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



回答2:


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.




回答3:


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 ...)




回答4:


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

I do like that bitshift one though...




回答5:


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

There are other things in Java which can be represented as an Integer, for example 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.




回答6:


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

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.

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.

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

public static void main(String [] arg) {

    int i = -1

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


来源:https://stackoverflow.com/questions/22888961/how-to-get-the-value-of-integer-max-value-in-java-without-using-the-integer-clas

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!