0

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

smac89
  • 39,374
  • 15
  • 132
  • 179
  • That's not what I see. I downloaded the source when I installed Java 8, and I'm looking at it in an editor (not in Eclipse).... Update: I don't see it in Eclipse either. – ajb Apr 15 '14 at 00:34
  • Is it possible that Eclipse doesn't have access to the actual sources, and therefore is trying to "reconstruct" the source from other information? That might explain why there aren't any comments in the "relevant code" snippet you posted. – ajb Apr 15 '14 at 00:39
  • maybe but I am using now going to take a look at the actual source using a text editor. I will post what I find – smac89 Apr 15 '14 at 00:42
  • @ajb, you are right, I am viewing it now on a text editor and it actually shows the value. I wonder why eclipse doesn't show this. Also there is no `import sun.misc.VM;` in the source code when viewed with a text editor. I wonder what eclipse is actually doing...this is wierd – smac89 Apr 15 '14 at 00:46
  • Maybe some of the answers to http://stackoverflow.com/questions/426084/how-do-i-view-jres-source-code-in-eclipse might help. – ajb Apr 15 '14 at 00:49
  • That is actually where I got the idea to view java source code. I used the second highest rated answer to view. ctrl + click. However, it seems eclipse is not the best way to view source code, as it modifies the code – smac89 Apr 15 '14 at 00:50
  • I don't think it modifies the source code, if it knows where to find the source code. I'm suggesting that perhaps you need to do something else to tell Eclipse where to find it (assuming you've downloaded it onto your computer). That's what I was hoping some of the other answers in that question would address. – ajb Apr 15 '14 at 00:57
  • I have downloaded it to my computer. In fact the way I was able to locate the actual folder was to hover over the folder in eclipse which showed the jar where the Integer class is located (rt.jar). I will look around and see if I can find an answer to this – smac89 Apr 15 '14 at 01:06

1 Answers1

2

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

sksamuel
  • 16,154
  • 8
  • 60
  • 108