Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Need good books on generics

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need good books on generics

    hi,

    I am iStore developer,mostly working on jsp pages.
    I do have knowledge on core java.But new to generics.
    Can anyone provide book references on generics.Or any links relatated to generics can be helpful for me.

    Thanks,
    Sabitha


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Need good books on generics

    I read a tutorial about generics,but it was in greek.So i can only post you the examples of it,not the link(if you wish the link too ask for it )
     
    package generics;
     
     
    public class Main{
     
     
        public static void main(String[] args) {
            Integer[] int_array = {1,2,3,5,5}; //in Generic basic types such as int,double,char
            //can not be used,so we use instead Inetger,Double,Character.The point is that at Gene
            //rics we can play only with Classes ,not with basic types(e.g. int).Notice that 
            //Integer,etc. are classes
            Character[] char_array = {'s','a','b','h'};
     
            int s1=12;
            char s2='a';
     
            /*Purpose of function contains is to see if a symbol is part of a sequence(e.g. an 
             * array)
             */
            if(contains(int_array,s1))
            {
                System.out.println("Found" + s1);
            }
            else
                System.out.println(  s1 + " Not found...\n");
     
            if(contains(char_array,s2))
            {
                System.out.println("Found " + s2);
            }
            else
                System.out.println(  s2 + " Not found...\n");
     
        }
     
        //The generic type is placed before the return type of the function
        public static <E> boolean contains(E[] a,E x)
        {
            for(E element : a)//traverse all the elements of array a
            {
                if(x.equals(element))//equals is a function that is for use to us by JAVA
                {
                    return true;
                }
            }
            return false;
        }
    }

    and about safety of Generics

     
    package genericsafety;
     
    import java.util.*;
     
    public class Main{
     
     
        public static void main(String[] args) {
            List<Integer> l =new ArrayList();
     
            //insert some random numbers at that list
            l.add(5);
            l.add(100);
     
            //Error because the list is for int and we insert String
            //l.add("this will cause error");
        }
    }

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Need good books on generics

    Lesson: Generics (Updated) (The Java™ Tutorials > Learning the Java Language)

Similar Threads

  1. about books
    By deependeroracle in forum Java Theory & Questions
    Replies: 0
    Last Post: January 31st, 2012, 10:54 AM
  2. Any good books for java game development?
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 1
    Last Post: September 23rd, 2011, 05:30 AM
  3. JAVA books- help
    By the_marija in forum Java Theory & Questions
    Replies: 2
    Last Post: September 19th, 2010, 05:59 AM
  4. books
    By gcsekhar in forum Java Networking
    Replies: 1
    Last Post: September 16th, 2010, 07:37 AM
  5. java books
    By bbr201 in forum Java Theory & Questions
    Replies: 0
    Last Post: July 20th, 2010, 09:51 AM

Tags for this Thread