5

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

brandizzi
  • 26,083
  • 8
  • 103
  • 158
madsword19
  • 57
  • 1
  • 1
  • 3

7 Answers7

11

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)
Bohemian
  • 412,405
  • 93
  • 575
  • 722
7

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.

Reuben Tanner
  • 5,229
  • 3
  • 31
  • 46
2

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

Bohemian
  • 412,405
  • 93
  • 575
  • 722
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Sorry, but I have to do this for bragging rights... see [my version](http://stackoverflow.com/a/22889389/256196) :) – Bohemian Apr 06 '14 at 02:44
2

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

slyaer
  • 241
  • 4
  • 10
1

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.

MrLore
  • 3,759
  • 2
  • 28
  • 36
0

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);
}
0

this also works....

int i = Short.MIN_VALUE 
       *Short.MIN_VALUE 
       *(Short.MAX_VALUE/Short.MAX_VALUE + Short.MAX_VALUE/Short.MAX_VALUE) 
       -(Short.MAX_VALUE/Short.MAX_VALUE);
i = Math.abs(i);
  • Answer needs supporting information Your answer could be improved with additional supporting information. Please [edit](https://stackoverflow.com/posts/76261993/edit) to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center.](https://stackoverflow.com/help/how-to-answer) – moken May 18 '23 at 01:56