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

7

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

Cobnut answered 6/4, 2014 at 1:31 Comment(4)
What? I don't understand... int i = Integer.MAX_VALUE;? Or just look at the source: @Native public static final int MAX_VALUE = 0x7fffffff;Cone
Just do int i = Integer.MAX_VALUE.Feltner
@JoshM @Christian The issue is that the answer cannot contain: "Integer", "Float", "Double", and digits (0 - 9)Dex
I guess it can contain int (instead of Integer) then.Lindeberg
I
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)
Internationale answered 6/4, 2014 at 2:43 Comment(0)
C
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.

Copycat answered 6/4, 2014 at 1:37 Comment(2)
Bzzzt ... your answer contains Integer! But thanks for playing :-)Girt
Whoops! I completely missed that (only and most important) constraintCopycat
G
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 ...)

Girt answered 6/4, 2014 at 2:33 Comment(1)
Sorry, but I have to do this for bragging rights... see my version :)Internationale
G
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...

Guglielma answered 22/5, 2015 at 10:26 Comment(0)
D
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.

Dex answered 6/4, 2014 at 1:43 Comment(0)
D
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);
}
Dermato answered 6/4, 2014 at 1:45 Comment(0)
S
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);
Sonia answered 16/5, 2023 at 10:32 Comment(1)
Answer needs supporting information Your answer could be improved with additional supporting information. Please 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.Invalidism

© 2022 - 2024 — McMap. All rights reserved.