Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

generics  problemis to avoid type cast problem is i correct

Posted on 2012-09-09
6
Medium Priority
?
442 Views
Last Modified: 2012-09-10
generics  problemis to avoid type cast problem is it correct

any way explain advanages of generics
0
Comment
6 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 38381807
Generics allow you to  provide the same logic across multiple classes/types without having to duplicate that logic for each class. A crude example would be having a class which performs arithmetic against types. For example:

public class Arithmetic<T> {
    public T Add(T input1, T input2) {
        return intput1 + input2;
    }
}

Open in new window


Now if we were going to add two int objects, we might traditionally write the function with hard-coded types:

...
    public int Add(int input1, int input2) {
        return input1 + input2;
    }
...

Open in new window


Can't you add decimal types as well? I'm sure you'd agree that we can use the function defined immediately above because it is designed to take in only ints. So traditionally we'd have to create a new function to deal with decimals:

...
    public decimal Add(decimal input1, decimal input2) {
        return input1 + input2;
    }
...

Open in new window


The logic above is exactly the same:  perform the addition of two things and return the result. The power of generics is that we do not need to duplicate the logic to account for multiple types. We essentially make the type variable, and we write the class only once, inserting placeholders for the types. Then, we can simply pass the type when we want to work with different types:

Arithmetic<int> adder1 = new Arithmetic<int>();
int input1 = 1;
int input2 = 2;
int result1 = adder1.Add(input1, input2);

Arithmetic<decimal> adder2 = new Arithmetic<decimal>();
decimal input3 = 1.0;
decimal input4 = 2.0;
decimal result2 = adder2.Add(input3, input4);

Open in new window

0
 
LVL 12

Expert Comment

by:Sharon Seth
ID: 38382039
Generics' intention is to avoid type cast problems during runtime . By using generics , you can strictly specify with which types you want your routine to work . The compiler restricts clients(callers) to use the routine by passing only those types  and nothing else . Thus , compiler makes that type checking instead of the runtime . So ,if you have any bad callers , then it is detected at compile time instead of ending up with a surprise at runtime.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 9

Author Comment

by:Manikandan Thiagarajan
ID: 38382174
is it jdkk1.5 feature or jdk1.6 features
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38382735
I'm sure you'd agree that we can use the function defined immediately above because it is designed to take in only ints.
That was supposed to say "can not" in my comment.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38382739
is it jdkk1.5 feature or jdk1.6 features
According to Wiki, they came with Java 5.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month15 days, 18 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question