On the positive side, the book clearly states this line does not compile But of course, it's intended to not compile because of mixing class and instance variables, not because of a missing (primitive) data type.
Let me test your knowledge a little bit Assuming total and count are both class variables of type int as inwhich of the following will compile
Roel De Nijs wrote: Let me test your knowledge a little bit Assuming total and count are both class variables of type int as inwhich of the following will compile
Thanks for your question, Roel. It is good that I am obliged to remember previous topic which I have already read At first, I think all lines will compile except line1. Line1 doesn't compile so i have already learned from OCA book (on page 55, Numeric Promotion section) I am a bit confused line5 & line6 and think that these compile for autoboxing. Then I test these codes in Netbeans and saw that line6 doesn't compile, it must be cast to long
We can't declare Long variable as this
We must add "L" after value or cast to long
Mushfiq Mammadov wrote:I am a bit confused line5 & line6 and think that these compile for autoboxing. Then I test these codes in Netbeans and saw that line6 doesn't compile, it must be cast to long
It's one of the most common mistakes with regard to autoboxing people make. You might think autoboxing will turn an int into a Long because you can assign an int to a long. But that's (as you discovered) not true. This topic has a very extensive discussion about wrapper classes, autoboxing and using the == operator. It's definitely worth reading.
Roel De Nijs wrote: Just like to add: besides being an Object, Integer IS-A Number as well.
I see Number at first time although since JDK1, thanks for new information, Roel
Roel De Nijs wrote:
So this code compiles:And now the 1 million dollar question: what's the output of this program?
If Number is Integer play(4); calls play(Number n) and play(4L); calls play(Long l). Similiar example as this is explained very well in the last section of Overloading Method topic, I don't remember page exactly because my book is in the work. At first overloading method find exact type. In this example play(4); find int type at first, then Integer, then Object. So Integer is Number therefore calls play(Number n).