Question

I was just looking at the source code for the Integer class and I came across these lines:

public static final int MAX_VALUE = Integer.MAX_VALUE;
public static final int MIN_VALUE = Integer.MIN_VALUE;

These are the only places where MAX_VALUE or MIN_VALUE is declared in the Integer class, so it seems that the values are assigned to themselves...

But when I try to print the values, I get:

2147483647 -> 0x7fffffff
-2147483648 -> 0x80000000

So it still produces the correct value, but where is it assigned? I am using the latest java (8) on eclipse

I couldn't find the code online, so I will just paste the relevant code here:

package java.lang;

import sun.misc.VM;

public final class Integer
  extends Number
  implements Comparable<Integer>
{
  public static final int MIN_VALUE = Integer.MIN_VALUE;
  public static final int MAX_VALUE = Integer.MAX_VALUE;
  ...
}

After solving this, it seems that eclipse modifies the source code for some jar files. Best way to view

Était-ce utile?

La solution

Are you sure you're looking at the right source, and not just a compiled class in your IDE. I have the following:

/**
 * A constant holding the minimum value an {@code int} can
 * have, -2<sup>31</sup>.
 */
public static final int   MIN_VALUE = 0x80000000;

/**
 * A constant holding the maximum value an {@code int} can
 * have, 2<sup>31</sup>-1.
 */
public static final int   MAX_VALUE = 0x7fffffff;

Grep code agrees: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/Integer.java

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top