I know that Integer is final and can't be extended, that is why the second example won't compile. But how is possible that the first example compiles and actually returns 35?
In Brazilian forums, there are some guys saying that could even be a Compiler Error, I doubt that but I haven't found any explanation.
Thanks for the Help.
Duc Vo
Ranch Hand
Joined: Nov 20, 2008
Posts: 254
posted
0
I would guess it works because of autoboxing feature. Autoboxing only means for the compiler to treat value type as reference type, properly in your case it will be treated as T.
Also, with the second code block if you cast the return result to T then it can be compiled.
“Everything should be as simple as it is, but not simpler.” Albert Einstein
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
posted
0
I am compiling from the command line and your both examples are not compiling.
Try to run them from command line.
SCJP 6
Sunny Mattas
Ranch Hand
Joined: Apr 22, 2008
Posts: 45
posted
0
hi
For me also both examples are not compiling.
And i think it has nothing to do with Integer class because following code is also not compiling.
But this is compiling with a warning
Regards
Sunny Mattas
SCJP5
Duc Vo
Ranch Hand
Joined: Nov 20, 2008
Posts: 254
posted
0
Well I compile it with eclipse 3.4. Not sure what compiler options that eclipse is using.
David Lopes
Greenhorn
Joined: Oct 24, 2008
Posts: 3
posted
0
Punit Singh wrote:I am compiling from the command line and your both examples are not compiling.
Try to run them from command line.
In fact, when compiling from command line (javac) the first example won't compile as well.
The strange is that my Eclipse uses the jdk 1.6 located at the same directory but of course it uses the javaw.exe to compile, not the javac.
Is there anything wrong with the javaw.exe ? oO
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
posted
0
David Lopes wrote:
Punit Singh wrote:I am compiling from the command line and your both examples are not compiling.
Try to run them from command line.
In fact, when compiling from command line (javac) the first example won't compile as well.
The strange is that my Eclipse uses the jdk 1.6 located at the same directory but of course it uses the javaw.exe to compile, not the javac.
Is there anything wrong with the javaw.exe ? oO
This is not the first issue with eclipse, you will lots of these type of issues in future as I have got too many, so be careful, do not depend on eclipse.
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
posted
0
One more example I remember is this one:
Here this is allowed in eclipse, but it is compile time error on command line.
David Lopes
Greenhorn
Joined: Oct 24, 2008
Posts: 3
posted
0
Contacted Eclipse Foundation,
They confirmed the bug and it will be corrected at the next patch.
I thought Eclipse didn't change core java and javac behavior. I thought of it as a giant GUI wrapper with added functionality. This is interesting, is Eclipse preparsing the code somehow? And for what reason?
All code in my posts, unless a source is explicitly mentioned, is my own.