File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Number class doubt? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Number class doubt?" Watch "Number class doubt?" New topic
Author

Number class doubt?

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9622
    
    2

Guys,

Consider the code below,

Integer i1 = 5000;
Number n1 = 5000;

How does this compile and also when I tried n1.equals(i1) returns true??

Confused. I thought Number class is abstract and cannot be instantiated. Then when we say Number n1 = 5000;, what actually happens?? and of what type is that?


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
A variable of type Number can be assigned a reference of type Number or a subclass thereof. You are assigning 5000, What's that? It is an int literal. So using the concept of autoboxing the compiler converts the int to an Integer wrapper object which is a subclass of Number. Cool, what?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
To explain about the equals being true, think about what you have already learned about polymorphism and overriding.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9622
    
    2

Barry, thanks for the post and how does the above situation behaves in a java 1.4 compiler where there is no auto boxing?
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Jothi Shankar Kumar Sankararaj posted December 10, 2006 05:30 AM
Barry, thanks for the post and how does the above situation behaves in a java 1.4 compiler where there is no auto boxing?

When you have the jdk 1.5 you can also make it compile the old fashioned way:
javac -source 1.4 YourFile.java

Just try it!


Yours,
Bu.


all events occur in real time
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Number class doubt?