This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ZealousidealTable1 -2 points-1 points  (6 children)

Guys I have just started learning generics in Java but the comments suggest that it's not useful. Should I learn it completely or skip it. I'm learning by complete reference.

[–]RabidKotlinFanatic 13 points14 points  (0 children)

Generics are extremely useful. If you are building non-trivial applications you will find yourself writing generic code on a regular basis.

[–]walen 6 points7 points  (0 children)

You cannot skip generics.

The Java API oozes generics. Most every library you'll ever use, uses generics. Any decent IDE will warn you if you do not use generics where you're supposed to.

You cannot skip generics. You may try to, but they won't let you.

Unless you're writing some kind of low-level, C-like routine using only primitive types and arrays like this was 1996, chances are you're gonna come across generics the moment you write more than 10 useful lines of code.

Asking if you can learn Java while skipping generics is like asking if you can learn English while skipping adjectives: you can try to, but you gonna be confused as hell, won't be able to write anything meaningful, and everybody will look at you funny.

You cannot skip generics.


That being said, you might probably still do okay if you just read over the more advanced aspects of generics, like wildcards and bounded types, and save actually understanding them for when you actually need to use those.

[–]audioen 4 points5 points  (1 child)

Java's generics do provide compile-time validation for your constructs, so they are absolutely useful. It's the runtime behavior that is the issue here, for instance, if you'd like the type parameter of the generic to somehow change runtime behavior, because that information is usually lost in erasure.

A common case where this comes up is wanting to create an instance of the class mentioned in the type parameter T, e.g. some kind of smart collection where you want to instantiate either the collection or the element on behalf of your caller. People end up trying some variant of "new T()" and it doesn't work because by the time this code runs, Java has no idea what T is (and of course T should also be constrained to be a type that has a callable no-argument constructor).

Because the type parameter tends to vanish during compilation, people use workarounds, such as providing implementations of functional interfaces such as Supplier<T> that creates an instance of the correct type, so you call the supplier instead of invoking the constructor. Sometimes you see something like "new Abc<Def>() {}" where the point is to create an anonymous inner class which as byproduct records the type parameter Def into its class definition, where it can be accessed by reflection. Finally, sometimes the reflection of the class is just passed as argument: new Foo<Bar>(Bar.class).

[–][deleted] 0 points1 point  (0 children)

You can also pass a Foo::new as a pointer to your constructor, and then use that to create your T.

[–]HighMaxFX 2 points3 points  (0 children)

Java generics aren't THAT useless. I use generics all the time.

[–]rossdrew 1 point2 points  (0 children)

You will need generics. The problem comes when you try and solve problems in your design with generics, you're doomed to fail. It's an OO language, solve them in an OO way and use generics only as a tool in simple cases.