• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

SCJP problem with boxing

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you check the API documentation?

This will tell you that Number is an abstract superclass of the numeric wrapper classes.
 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Anand Shrivastava
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Anand Shrivastava
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
And then we all jump out and yell "surprise! we got you this tiny ad!"
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic