| Author |
Number class doubt?
|
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
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, SCBCD Hints - Demnachst, SCDJWS - Auch Demnachst
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: 8795
|
|
|
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
|
 |
 |
|
|
subject: Number class doubt?
|
|
|