generic Vs regular java classes

Hi,

how to create and use generic interfaces, classes, methods(with one and more type parameters). what are the uses of them. How they are differen and similar to regular interface, class, methods.please advis
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dpearsonCommented:
Covering all of that is a huge topic, but the basic idea is simple.

List untypedList = new List() ;
untypedList.add(10) ;

// Need to cast the value when getting it out of the list - because it could be a list of anything
Integer value = (Integer)untypedList.get(0) ;

List<Integer> typedList = new List<Integer>() ;   // Or now in Java 7 can type new List<>() ;
typedList.add(10) ;

// Now need to cast - the compiler knows this only stores integers
Integer value2 = typedList.get(0) ;

Hope that explains the main difference - one has compile time type checking and the other does not.
And compile time type checking is better - so you should always be using the Generic version of these classes now.

Doug
gudii9Author Commented:
List<Integer> typedList = new List<Integer>() ;   // Or now in Java 7 can type new List<>() ;

i wonder why 7 relaxed the rule.

I like as below
List<Integer> typedList = new List<Integer>() ;

So that immediately after List we type <Integer> without forgetting both places.
List<Integer> typedList = new List<Integer>() ;
where as below shows bit inconsistent
List<Integer> typedList = new List() ;

Hope that explains the main difference - one has compile time type checking and the other does not.
And compile time type checking is better - so you should always be using the Generic version of these classes now.

what is difference between compile time checking and non compile time checking? which category generic version falls under. Can you please elaborate more on this
dpearsonCommented:
i wonder why 7 relaxed the rule.
It's just an enhancement - the type on the right hand side isn't needed, so they made it optional.  So you can save some typing.

what is difference between compile time checking and non compile time checking?
Compile time checking means you get an error when you compile your program.  The other type of error is "run time" which means you only get it when you run your program and get it to execute the incorrect lines of code.

Compile time is always better because you find the problems sooner and generics are all compile time checks.

Doug
gudii9Author Commented:
generics are all compile time checks.

can you please elaborate on this may be with simple example?
dpearsonCommented:
Sure.  If you do this:

List untypedList = new List() ;
untypedList.add(10) ;
String value = untypedList.get(0) ;    // This won't work - you put an integer in here, not a string

It'll compile - but when you try to run it you'll get a runtime error - a class cast exception when it tries to convert the first item in the list from an integer into a string.

If you do this:
List<String> typedList = new List<String>() ;
typedList.add(10) ;            // This line will not compile
String value = typedList.get(0) ;

You'll get a compile time error - can't add an integer to a list of strings.
Because the compiler knows (for the generic list) that it's a list of strings - so you can only add strings to it.

Much better to find this error at compile time than waiting for the error at runtime.

Doug

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.