| Author |
Clarification about Widening
|
James Tharakan
Ranch Hand
Joined: Aug 29, 2008
Posts: 580
|
|
Its about widening. I always think if i am right or wrong. As far as i understood, Widening of primitive datatypes means, moving upwards.i.e byte-> short-> int-> long->float->double. Widening at objects level means, Hope i am right, if not, correct me [ December 16, 2008: Message edited by: James Tharakan ] [ December 17, 2008: Message edited by: James Tharakan ]
|
SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
|
 |
Preethi Dev
Ranch Hand
Joined: Sep 07, 2008
Posts: 265
|
|
Hi James, I think this is correct, byte->short-> int-> long->float->double. Preetha
|
 |
James Tharakan
Ranch Hand
Joined: Aug 29, 2008
Posts: 580
|
|
|
I am still looking for answers ! !
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
Originally posted by James Tharakan: I am still looking for answers ! !
Did you see the answer by Preetha Arun??
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Rajshekhar Paul
Ranch Hand
Joined: Oct 17, 2006
Posts: 140
|
|
Yes, you are right James except when you say int->Integer->Number->Object, for example. In the above case, int is boxed to Integer first and then widening is applied. In general widening means going to class/interface which is more general, on contrary narrowing means moving to more specific classes.
|
When it's obvious that you have to do it, just do it without shattering your thoughts over different directions.
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
James, there is no widening is at object level. If primitive type int is converted to wrapper Integer, it is called boxing. int----> Integer is boxing. Integer---> int is unboxing. Now boxing and unboxing are done by compiler, so it is called autoboxing and autounboxing. If Integer is converted to Number, it is called upcasting or implicit casting, as it happens implicitly. If Number is converted to Integer, it is called downcasting or explicit casting, as we have to do it explicitly. like: Number n; Integer i=(Integer)n;
|
SCJP 6
|
 |
Rajshekhar Paul
Ranch Hand
Joined: Oct 17, 2006
Posts: 140
|
|
@punit When you say 'autounboxing', you are wrong. It's 'unboxing' basically. And I think the usage of word 'widening' in case of object level is also meaningful. [ December 17, 2008: Message edited by: Rajshekhar Paul ]
|
 |
James Tharakan
Ranch Hand
Joined: Aug 29, 2008
Posts: 580
|
|
Originally posted by Ankit Garg: Did you see the answer by Preetha Arun??
Well,Ankit, that was not complete. And thanks Rajshekhar Paul , Now things are clear.
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
Originally posted by James Tharakan: Well,Ankit, that was not complete.
No problems . By the way nice thought in your signature James
|
 |
 |
|
|
subject: Clarification about Widening
|
|
|