-1

Assume that d is a double variable. Write an if statement that assigns d to the int variable i if the value in d is not larger than the maximum value for an int.

The method below is my attempt at this problem:

public static void assignInt(double d)
{
  int i = 0;
  if(d < Integer.MAX_VALUE)
    i =  (int)d;
  System.out.print(i);
}

Integer.MAX_VALUE holds 2147483647. I am assuming this is the maximum value an int can hold? With this assumption in mind, I attempt to call assingDoubleToInt() three times:

public static void main(String[] args)
{
  assignDoubleToInt(2147483646); //One less than the maximum value an int can hold
  assignDoubleToInt(2147483647); //The maximum value an int can hold
  assignDoubleToInt(2147483648);//One greater than the maximum value an int can hold. Causes error and does not run.
}

The first two calls output:

2147483646
0

And the third call, assignDoubleToInt(2147483648);, throws "The literal 2147483648 of type int is out of range." Isn't the comparison 2147483648 < 2147483647 here? Why is i being assigned the value if the comparison should evaluate to false?

Using the comparison d < Integer.MAX_VALUE is not the proper way to do this. How can I test whether a double variable can fit in an int variable?

justin
  • 553
  • 1
  • 7
  • 18
  • 2
    Use `2147483648.0` – mjwills Mar 25 '19 at 22:24
  • 4
    Possible duplicate of [Literal Assignment in Java](https://stackoverflow.com/questions/39777836/literal-assignment-in-java) – mjwills Mar 25 '19 at 22:25
  • 1
    *"if the value in `d` is **not larger than** the maximum value for an `int`"* Note that "not larger than" is not the same as "less than". What if the value is **equal to** the maximum value for an `int`? The code should be `if (d <= Integer.MAX_VALUE)` – Andreas Mar 25 '19 at 22:32
  • 2
    I don't think it actually *throws* anything, I think that's a compiler error, yes? Could also use `2147483648L`. – markspace Mar 25 '19 at 22:35

2 Answers2

1

The int range issue is because you have an int literal. Use a double literal by postfixing 'd':

public static void main(String[] args) {
    assignDoubleToInt(2147483646); // One less than the maximum value an int can hold
    assignDoubleToInt(2147483647); // The maximum value an int can hold
    assignDoubleToInt(2147483648d);// One greater than the maximum value an int can hold. Causes error and does not
                                  // run.
}

I believe your equality test should be <=, also: "if the value in d is not larger than the maximum value for an int" - so if it is EQUAL to the maximum value for an int, it's valid:

public static void assignDoubleToInt(double d) {
    int i = 0;
    if (d <= Integer.MAX_VALUE)
        i = (int) d;
    System.out.println(i);
}
Not a JD
  • 1,864
  • 6
  • 14
0

Integer.MAX_VALUE holds 2147483647. I am assuming this is the maximum value an int can hold?

Yes.

throws "The literal 2147483648 of type int is out of range."

You can't create an int literal representing a number outside of the int range, so 2147483647 is legal but 2147483648 is not. If you want a larger integer literal, use a long literal, with the letter L appended: 2147483648L, or a double literal with a decimal point (or D appended): 2147483648.0 (or 2147483648D).

is not the proper way to do this.

The comparison d < Integer.MAX_VALUE is legal and correct, because Integer.MAX_VALUE will be widened to a double for the comparison, and a double can be (much) larger in value than Integer.MAX_VALUE. You just need to make sure you can pass the proper value in as detailed above.

(As noted in a comment above, it should be d <= Integer.MAX_VALUE, with <=.)

rgettman
  • 176,041
  • 30
  • 275
  • 357