Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How can that possible compile??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How can that possible compile???" Watch "How can that possible compile???" New topic
Author

How can that possible compile???

David Lopes
Greenhorn

Joined: Oct 24, 2008
Posts: 3
This compiles:


This one does not compile:


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
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
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
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
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
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
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
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
Contacted Eclipse Foundation,

They confirmed the bug and it will be corrected at the next patch.

https://bugs.eclipse.org/bugs/show_bug.cgi?id=264843
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Eclipse does not use javac - it has its own built-in Java compiler, based on IBM's Jikes Java compiler. (Eclipse was originally created by IBM).

Eclipse is ofcourse supposed to work the same way as Sun's Java compiler. If it doesn't, it's a bug in Eclipse.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
That explains things, thanks for clarifying that, Jesper.
 
wood burning stoves
 
subject: How can that possible compile???
 
Similar Threads
how to determine the real type of a generic property?
Netbeans Generics Bug
Generics in class declaration
generics
Generics question