Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Re: = operator should automatically perform appropriate casts

Reply
Thread Tools

Re: = operator should automatically perform appropriate casts

 
 
cgbusch
Guest
Posts: n/a
 
      07-07-2003
>
> As for automatic downcasting in an assignment statement, I think
> it's a very bad idea.

<deleted>
> A fundamental design goal of the Java language is to find as
> many potential coding errors at compile time as possible, rather
> than at runtime. That's goodness.


The compiler would still check for 'inconvertible types', since the
object on the left must be able to be a subclass or superclass of the
object on the right.

List list=new ArrayList();
list.add("oops");
Map m=(Map)list.get(0);

What Java compiler will find a bug in the above code? They won't. So
(Map) is useless additional information since we know it will need to
be a Map assigned anyways, so (Map) for all practical purposes you are
trusting the compiler's arm.

Map m=(implicit)list.get(0) would accomplish the same thing and it
stills check for 'inconvertible types' at compile-time.

--
CB
 
Reply With Quote
 
 
 
 
cgbusch
Guest
Posts: n/a
 
      07-08-2003
> > Map m=(Map)list.get(0);
> Right now compiler complains here because it tries to tell you that you're
> trying to do something potentially dangerous and wants you to make clear
> that you *really* want to do it

Then I could do it via (implicit), like so:
Map m=(implicit)list.get(0);
But if it was so potentially dangerous, then why are we forced to do it so often?
 
Reply With Quote
 
 
 
 
Sudsy
Guest
Posts: n/a
 
      07-08-2003
cgbusch wrote:
>>>Map m=(Map)list.get(0);

>>
>>Right now compiler complains here because it tries to tell you that you're
>>trying to do something potentially dangerous and wants you to make clear
>>that you *really* want to do it

>
> Then I could do it via (implicit), like so:
> Map m=(implicit)list.get(0);
> But if it was so potentially dangerous, then why are we forced to do it so often?


Because a lot of these methods are defined as returning Object in order
to be as general as possible. It's up to the programmer to know what
kind of objects got stuffed into the collection and cast accordingly.
The JVM will very helpfully throw a runtime ClassCastException if you
get it wrong. Then again, you could always use instanceof in order to
determine whether what you were expecting is what you actually have.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Should you perform complex tasks in the constructor? Chicken McNuggets C++ 19 01-28-2013 09:43 PM
Re: Should I remove unnecessary casts? Roedy Green Java 5 08-25-2008 11:36 AM
unsigned right shift casts to int automatically!!! Ehsan Khoddam mohammadi Java 2 01-04-2007 01:30 PM
TB 1.0.6 - How do you add files types and actions to the "Automatically perform" section? Z@no.spam Firefox 4 01-02-2006 04:38 PM
asp.net automatically perform action based on a date CoX ASP .Net 3 12-30-2003 04:36 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57