aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes SCJP problem with boxing 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 "SCJP problem with boxing" Watch "SCJP problem with boxing" New topic
Author

SCJP problem with boxing

Anand Shrivastava
Ranch Hand

Joined: Jul 22, 2007
Posts: 125
Dear friends,

I have started preparing for SCJP1.5 with the k&B book, and in the assignment chapter got struck down with the following question:


The correct output is ofcourse 4,3.
The problem is that in the book, prior to this example, nowhere it has been explained that what is Number. In the wrapper class discussion also there is no reference to Number. The explanation reads that the short is boxed in Short and widened to Number. My difficulty is that if Number is a wrapper class then widening between wrapper classes is not allowed. Number is definitely not a primitive. If Number is a superclass then the short should have been boxed in Number as Boxing and widening is allowed, instead of being boxed in Short. Secondly, why the compiler will automatically box the short to Short without any explicit statement for that. Can someone throw some light on this.


Anand Shrivastava
SCJA
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Did you check the API documentation?

This will tell you that Number is an abstract superclass of the numeric wrapper classes.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Originally posted by Anand Shrivastava:
[QB]

If Number is a superclass then the short should have been boxed in Number as Boxing and widening is allowed, instead of being boxed in Short. [QB]



What exactly do you mean by "short should have been boxed in Number as Boxing and widening is allowed, instead of being boxed in Short. "

Boxing means converting a Primtive to a Wrapper/Object

So in this case the compiler will obviously give the first preference to Widening then Boxing and then Var Arg.

Here short is being boxed to Short and then being widened to Number.
so what we are doing here is Boxing and Widening.
short cannot be directly boxed to Number if that's what you were trying to say.

Originally posted by Anand Shrivastava:
[QB]
Secondly, why the compiler will automatically box the short to Short without any explicit statement for that. Can someone throw some light on this.
QB]


That is the whole point of Java 1.5 - implicit boxing.
It was not possible to do that in the earlier versions.



This is because in the first case it implicitly unboxes the Integer to int and increements it.
You donot have to expilicitly do it .
However in the second , with Java 4, this was not possible and you would have to do it explicitly.


I hope this helps.
[ July 23, 2008: Message edited by: Nabila Mohammad ]

The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Anand Shrivastava
Ranch Hand

Joined: Jul 22, 2007
Posts: 125
Nabila Thanks,

Just it came to my mind, what we are upto,
ultimately we are doing,

Number = Short

Number is a superclass.

or in otherwords,
Superclasstype = Subclass object,

this is upcasting,

infact we are narrowing it (so the term boxing and narrowing should have been used
Keith Nagle
Ranch Hand

Joined: May 06, 2008
Posts: 65
I think the golden rules to remember here is
that an int CANNOT become a LONG (widen & box) and an
Integer cannot be widened to a Long as they
are peer classes!If you remember those you will
be at your grannies.
Best of luck with the studies.


SCJP 5.0
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Originally posted by Anand Shrivastava:
Nabila Thanks,

Just it came to my mind, what we are upto,
ultimately we are doing,

Number = Short

Number is a superclass.

or in otherwords,
Superclasstype = Subclass object,

this is upcasting,

infact we are narrowing it (so the term boxing and narrowing should have been used


I am not sure how you are coming to the conclusion that we are narrowing it.
We are not narrowing the supertype Number to Short.
If you can be more clear may be I can help you.




The first statement that is executed is from the main.
s is assigned the value 7
short s=7
The next statement is System.out.print(dox(s,s) + " ");

It has 4 options.
1. static int dox(Long x, Long y) { return 1;}
Widening and boxing is not possible - short cannot be wident to long and boxed to Long. so this option is discarded.

2.static int dox(long...x) {return 2;}This option will be the last one to be considered as it is var arg.

3.static int dox(Integer x, Integer y) { return 3;}This has the same case as the first one.
Widening and boxing is not possible - short cannot be wident to int and boxed to Integer. so this option is discarded.

4.static int dox(Number n, Number m) {return 4;}This statement is possible .

The short s= 7 is boxed to Short s=7
And this Short is then widend to Number which is a parent class of the wrapper classes.

so the first out put is 4


In teh second case
System.out.println(dox(7,7));
7 is implicitly an int and so the best option is simply boxing it to Integer.And thus the output is 3.

I hope you are clear with it now.
[ July 24, 2008: Message edited by: Nabila Mohammad ]
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

2.static int dox(long...x) {return 2;}


if you had

You would get 2 as the output not the 4. Because it chooses widening first.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Anand Shrivastava
Ranch Hand

Joined: Jul 22, 2007
Posts: 125
Okay I will try to explain you why I say it is narrowing.

Consider the follwing case :



Here line1 would not compile because the object base has been upcasted
to sup reference, hence only methods available in the SuperClass can be
invoked, or in other words object base has been narrowed to only those methods that are contained in the superclass (yes ofcourse to avoid runtime exception they must be overridden in the base class (polymorphism)).

Similarly, we know that Number is super class and Short is its base class
so what we are doing here is
Short s
Number num = s
SuperClass reference = subclass object
(upcasting)
followint the same analogy isnt it narrowing.


Yes, i understand how 4 and 3 came.
Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SCJP problem with boxing