Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

generics
compiles, but netbeans underlines most of it in red; what do these statement
s
do, please?  shooting in the dark..

code borrowed/inspired from/by
<http://www.javaworld.com/javaworld/...607-tiger2.html>

public class DriverCatDog {

public static void main(String[] args){
System.out.println("DriverCatDog main..");

java.util.Vector<Dog> dogs = new java.util.Vector<Dog>();
//a Vector of type Dog named dogs composed of Dog ?
//why underlined in red?

java.util.Vector<Cat> cats = new java.util.Vector<Cat>();
//same as dogs

java.util.Vector<? extends Mammal> pets = dogs;
//a Vector of type...anything which extends Mammal?
//the Vector referenced by pets?
//composed of Vector dogs?
//there aren't any Cat objects in this, yes?

System.out.println("..DriverCatDog main..done.");
}//main

}//DriverCatDog



Thufir Hawat
<http://www.shaw.ca/members/hawat/source/> for java code

Report this thread to moderator Post Follow-up to this message
Old Post
thufir.hawat@mail.com
07-29-04 02:09 AM


Re: generics
thufir.hawat@mail.com wrote:
> compiles, but netbeans underlines most of it in red; what do these stateme
nts
> do, please?  shooting in the dark..

First of all, most likely the reason NetBeans underlines in red is that
it thinks there are compile errors there.  This code only compiles with
a 1.5-compliant compiler, and such a thing hasn't been released yet
(we're at beta 2).  So NetBeans is probably comparing with the 1.4
language and trying to tell you that there are errors in your code.

As for meaning.

>          java.util.Vector<Dog> dogs = new java.util.Vector<Dog>();

This declares a reference called dogs.  The reference can only point to
a Vector<Dog> object.  That is, it would be an error for it to point to
a Vector<Cat> object, or a Vector<Mammal> object, or a Vector<Poodle>
object.  (Those last three are important to recognize, and sometimes
surprising).

It then creates a new Vector<Dog> object.  The object will be a Vector
that holds Dog references; note that it CAN hold a Poodle reference,
because a Poodle is a Dog.  Nevertheless, its class (the kind of object
it is) is Vector<Dog>, even if you only intend to add Poodle references
to it in practice).

It then assigns 'dogs' to point to that object.

>          java.util.Vector<Cat> cats = new java.util.Vector<Cat>();

Same thing.

>          java.util.Vector<? extends Mammal> pets = dogs;

This declares a new reference called pets.  This reference can point to
any Vector object whose type parameter is Mammal or a subclass.  For
example, you previously created objects of class Vector<Dog> and
Vector<Cat>, and either of those objects could be pointed to by this
reference.  This is a wildcard.

This is different from declaring a reference of type Vector<Mammal>.  A
reference of type Vector<Mammal> could ONLY point to objects whose class
is Vector<Mammal>, so you couldn't assign 'dogs' to it, since 'dogs'
points to a Vector<Dog>; and Vector<Dog> is different from
Vector<Mammal>.  The wildcards get you around this problem.

This code then assigns the value of 'dogs' (which is a reference to
Vector<Dog> ) to 'pets'.  This is a legal assignment, because Dog extends
Mammal.

>  	//there aren't any Cat objects in this, yes?

There aren't any Cat objects in the actual object that 'pets' ends up
pointing to.  However, there's nothing to prevent 'pets' from pointing
to an object that does have Cat objects in it, at some point.  It could
point to a Vector<Cat>, or it could point to a Vector<Mammal> that
contains some cats and some dogs (or it could point to a Vector<Tabby>
if Tabby is a subclass of Cat).

Hope that helps.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Report this thread to moderator Post Follow-up to this message
Old Post
Chris Smith
07-29-04 02:09 AM


Re: generics
On Mon, 26 Jul 2004, Chris Smith wrote:
[..]
> Hope that helps.
[..]

absolutely; I'll have to read it repeatedly, though!

thanks,

Thufir Hawat

Report this thread to moderator Post Follow-up to this message
Old Post
thufir.hawat@mail.com
07-29-04 02:09 AM


Re: generics
On Mon, 26 Jul 2004 23:36:07 GMT, thufir.hawat@mail.com wrote or
quoted :

>compiles, but netbeans underlines most of it in red; what do these statemen
ts
>do, please?  shooting in the dark..

see http://mindprod.com/jgloss/generics.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Report this thread to moderator Post Follow-up to this message
Old Post
Roedy Green
07-29-04 02:09 AM


Re: generics
On Tue, 27 Jul 2004, Roedy Green wrote:
[..]
> see http://mindprod.com/jgloss/generics.html
[..]

I did, it doesn't address wildcards, that I see :(
If that's a bit combative pls excuse, a bad day!

Thufir Hawat
<http://www.shaw.ca/members/hawat/source/>

Report this thread to moderator Post Follow-up to this message
Old Post
thufir.hawat@mail.com
07-29-04 02:09 AM


Re: generics
On Tue, 27 Jul 2004 04:24:22 GMT, thufir.hawat@mail.com wrote or
quoted :

>I did, it doesn't address wildcards, that I see :(
>If that's a bit combative pls excuse, a bad day!


I take it the question you want answered is

What does this mean?

java.util.Vector<? extends Mammal> pets = dogs;

The elements of the Vector are constrained to be classes that either
are Mammals or are subclasses of Mammal or that implement Mammal. It
is unclear from you example if Mammal is a class or interface.

How is it different from:

java.util.Vector<Mammal> pets = dogs;

I don't know. Perhaps you could perform some experiments and report
back. The key may be the that extends does not mean "extends" in this
context. It means "extends or implements".

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Report this thread to moderator Post Follow-up to this message
Old Post
Roedy Green
07-29-04 02:09 AM


Re: generics
Roedy Green wrote:
>  java.util.Vector<? extends Mammal> pets = dogs;

[...]

> How is it different from:
>
>  java.util.Vector<Mammal> pets = dogs;

Here's the answer:

Vector<Dog> dogs = new Vector<Dog>();
Vector<? extends Mammal> pets = dogs;

works fine.  On the other hand,

Vector<Dog> dogs = new Vector<Dog>();
Vector<Mammal> pets = dogs;

will fail with a compile error.  Why?  Because Vector<Mammal> and
Vector<Dog> are different classes, with no inheritance relationship
between them.  Vector<Mammal> references may *not* point to instances of
Vector<Dog>, because they allow operations (such as adding a Cat) that
are not possible on Vector<Dog>.

Note that parameterized types with wilcards are reference types, but are
*not* classes.  That is, you can't say:

Vector<? extends Mammal> pets = new Vector<? extends Mammal>();

In the latter case, you need to pick a type.  Note also that the
reference of type Vector<? extends Dog> will not allow you to call add,
because it doesn't know what the actual parameterized type is and so
can't type-check the parameter.  It will allow you to call get, though,
because it can guarantee that whatever the type is, it extends Mammal...
so it will return a reference statically typed as Mammal.

The converse type is Vector<? super Dog>, which can point to a Vector
<Mammal>, but can't point to a Vector<CockerSpaniel>.  It allows you to
call add and pass a Dog (which it knows is perfectly valid), but if you
call get, it returns a reference statically typed as Object, since you
haven't told it anything more specific.  IIRC, you can also declare a
type like Vector<? extends Mammal super CockerSpaniel>, in which case
add will only accept a reference statically typed as CockerSpaniel, and
get will return a reference statically typed as Mammal.

This is the right solution to the problem of generic collections, which
Sun earlier got wrong with arrays (see ArrayStoreException in the API
docs).

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Report this thread to moderator Post Follow-up to this message
Old Post
Chris Smith
07-29-04 02:09 AM


Re: generics
On Mon, 26 Jul 2004 23:36:07 GMT, thufir.hawat@mail.com wrote or
quoted :

>   java.util.Vector<Dog> dogs = new java.util.Vector<Dog>();

I presume this is legal:

java.util.Vector<Dog> dogs = new java.util.Vector<Dalmatian>();

Now what happens when I type:

dogs.add( pitbull );

I presume the error cannot be detected at compile time.
Do you get a ClassCastException at runtime?

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Report this thread to moderator Post Follow-up to this message
Old Post
Roedy Green
07-29-04 02:09 AM


Re: generics
On Tue, 27 Jul 2004 07:07:22 GMT, Roedy Green
<look-on@mindprod.com.invalid> wrote or quoted :

>On Mon, 26 Jul 2004 23:36:07 GMT, thufir.hawat@mail.com wrote or
>quoted :
> 
>
>I presume this is legal:
>
>    java.util.Vector<Dog> dogs = new java.util.Vector<Dalmatian>();
>
>Now what happens when I type:
>
>    dogs.add( pitbull );
>
>    I presume the error cannot be detected at compile time.
>    Do you get a ClassCastException at runtime?


In other words, I presume it works like this:

// this is strange, but legal
java.util.Vector<Dog> dogs = new java.util.Vector<Dalmatian>();

// this is legal
dogs.add( aDalmatian );

// this is illegal and will be detected at compile time
dogs.add( aSiameseCat );

// this is illegal but will not be detected until run time where
// it will show up as a ClassCastException
dogs.add ( aPitbull );
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Report this thread to moderator Post Follow-up to this message
Old Post
Roedy Green
07-29-04 02:09 AM


Re: generics
Roedy Green wrote:
> I presume this is legal:
>
>     java.util.Vector<Dog> dogs = new java.util.Vector<Dalmatian>();

You're presumption is wrong.  That's not legal at all.  The type
Vector<Dog> is not related to Vector<Dalmation>, and even casting
between them is not possible, much less an implicit conversion.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Report this thread to moderator Post Follow-up to this message
Old Post
Chris Smith
07-29-04 02:09 AM


Sponsored Links




Last Thread Next Thread Next
Pages (2): [1] 2 »
Search this forum -> 
Post New Thread

Java Help archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:26 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.