2

Currently I am learning Generic Programming. I didn't understand why Generic Born. when we have Object Class(data Type). Below Code, The functioF1 and function21 in both I have passed the HashCodeclass1 as parameter. Both need to cast The passed parameter to particular Class and the output of operation will be same. Then why Generic Comes into picture?

package know.generic; import knowl.collection.hashcodeInternal.HashCodeclass1; public class ClassGeneric { public <T> void functioF1(T arg) { System.out.println(arg.getClass().getName()); System.out.println("val1 " + arg); HashCodeclass1 hCC = (HashCodeclass1) arg; } public void functioF21(Object arg) { System.out.println(arg.getClass().getName()); System.out.println("val2 " + arg); HashCodeclass1 hCC = (HashCodeclass1) arg; } } public class TestGeneric { public static void main(String arg[]) { ClassGeneric cg = new ClassGeneric(); cg.functioF1(new HashCodeclass1()); cg.functioF21(new HashCodeclass1()); } } 
3
  • You will something here Generics usage Commented Aug 14, 2015 at 5:45
  • First of all its elimination of casting Commented Aug 14, 2015 at 5:46
  • 1
    You've managed to create a broken example with bad non-generic and bad (barely) generic code. I'd start by removing the code you're displaying, it can only give you wrong ideas. Commented Aug 14, 2015 at 6:45

2 Answers 2

1

Refer javadocs for same. Some of relevant text from site :-

Code that uses generics has many benefits over non-generic code:

1.Stronger type checks at compile time. A Java compiler applies strong type checking to generic code and issues errors if the code violates type safety. Fixing compile-time errors is easier than fixing runtime errors, which can be difficult to find.

2.Elimination of casts. The following code snippet without generics requires casting: List list = new ArrayList(); list.add("hello"); String s = (String) list.get(0); When re-written to use generics, the code does not require casting: List list = new ArrayList(); list.add("hello"); String s = list.get(0); // no cast

3.Enabling programmers to implement generic algorithms. By using generics, programmers can implement generic algorithms that work on collections of different types, can be customized, and are type safe and easier to read.

Elimination of cast and type is really import if your collection is being passed through multiple classes. It avoids unexpected class cast issues, and provide better maintainability.

Sign up to request clarification or add additional context in comments.

Comments

0

You can refer to javadocs and @Panther also pointed out the same.

You can also refer source code of HashMap. And also think if you had to rewrite this code without using generics, what all challenges you will face which you have overcome using generics?

I am just rewriting your code. You can check the advantage.

public class ClassGeneric { public <T> T functioF1(T arg) { // changed return type from void System.out.println(arg.getClass().getName()); System.out.println("val1 " + arg); // HashCodeclass1 hCC = (HashCodeclass1) arg; return arg; } public Object functioF21(Object arg) { // changed return type from void System.out.println(arg.getClass().getName()); System.out.println("val2 " + arg); // HashCodeclass1 hCC = (HashCodeclass1) arg; return arg; } } public class TestGeneric { public static void main(String arg[]) { ClassGeneric cg = new ClassGeneric(); //no casting required and compile time check of type HashCodeclass1 f1OutputObj = cg.functioF1(new HashCodeclass1()); // you need to use casting HashCodeclass1 f2OutputObj = (HashCodeclass1) cg.functioF21(new HashCodeclass1()); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.