Results 1 to 5 of 5
  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    116
    Rep Power
    0

    Default Generic types in java - basic concept

    I am trying to understand the concept of Generics in java. In the introduction to Generic Types, this example is given:

    Java Code:
    public class Box {
        private Object object;
    
        public void set(Object object) { this.object = object; }
        public Object get() { return object; }
    }
    "Since Since its methods accept or return an Object, you are free to pass in whatever you want, provided that it is not one of the primitive types." - I understand this.

    But then it has been changed to a generic class:

    Java Code:
    /**
     * Generic version of the Box class.
     * @param <T> the type of the value being boxed
     */
    public class Box<T> {
        // T stands for "Type"
        private T t;
    
        public void set(T t) { this.t = t; }
        public T get() { return t; }
    }
    "As you can see, all occurrences of Object are replaced by T. A type variable can be any non-primitive type you specify: any class type, any interface type, any array type, or even another type variable."

    We can use any type in place of an Object, because Object is a superclass of all classes. But T (or any other class) is not a superclass of all classes. So how do we justify any class being used in place of a random T or any other class?

    Thank you in advance.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: Generic types in java - basic concept

    ... by filling in any 'real' type for 'T' (as in Box<String>, Box<Double> etc.)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    116
    Rep Power
    0

    Default Re: Generic types in java - basic concept

    Quote Originally Posted by JosAH View Post
    as in Box<String>
    "One part of the code may place an Integer in the box and expect to get Integers out of it, while another part of the code may mistakenly pass in a String, resulting in a runtime error."

    Reference: Generic Types (The Java™ Tutorials > Learning the Java Language > Generics (Updated))

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,614
    Rep Power
    5

    Default Re: Generic types in java - basic concept

    That is the primary reason for generics. So your potential run time errors will be caught at compile time. In most cases it also frees you from having to use instanceof and then casting objects to the proper type. So the code becomes more readable.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    116
    Rep Power
    0

    Default Re: Generic types in java - basic concept

    @jim829, Thank you very much!

Similar Threads

  1. Can you help me understand this basic GUI concept?
    By EscSequenceAlpha in forum New To Java
    Replies: 1
    Last Post: 04-08-2012, 08:33 PM
  2. Arithmetic with Generic Types
    By azalea in forum New To Java
    Replies: 3
    Last Post: 10-24-2011, 08:55 PM
  3. generic and different enum types
    By vojtab in forum New To Java
    Replies: 0
    Last Post: 11-08-2010, 05:11 PM
  4. Help with comparing generic types
    By repairmanjack in forum New To Java
    Replies: 4
    Last Post: 09-22-2009, 06:41 AM
  5. generic types
    By jon80 in forum New To Java
    Replies: 6
    Last Post: 06-12-2009, 10:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •