13 Replies Latest reply: Jul 27, 2007 10:16 AM by 807605 RSS

    Doubt in Collections

    807605
      Hi,

      Now I'm studying collections package. In that package I studied Collections not support to store the primitive data types. But while using the following code the didn't throw any error message or run exceptions

      import java.util.*;

      public class ArrayListDemo{

           public static void main(String args[])
           {
                int i=100;
                ArrayList arr=new ArrayList();
                arr.add(10);
                arr.add(i);
                arr.add(1);
                System.out.println(arr);
           }
      }

      After compilation run the program It will prints the following output

      output:
      [10,100,1]

      so I couldn't realize what actually made. please explain me about this.

      Thanks in Advance,
      Maheshwaran Devaraj
        • 1. Re: Doubt in Collections
          807605
          waht's there to explain ???

          U added elements to ArrayList and u r displaying them one by one
          • 2. Re: Doubt in Collections
            791266
            @Op. That because autoboxing was added to Java in Java 5.

            Kaj
            • 3. Re: Doubt in Collections
              807605
              wait how did u compile it ,???? , that add method require one more parameter ,,
              • 4. Re: Doubt in Collections
                791266
                wait how did u compile it ,???? , that add method
                require one more parameter ,,
                No, why?
                • 5. Re: Doubt in Collections
                  807605
                  wait how did u compile it ,???? , that add method
                  require one more parameter ,,
                  No it doesn't. Please stop giving people duff answers!
                  • 6. Re: Doubt in Collections
                    807605
                    ter compilation run the program It will prints the
                    following output

                    output:
                    [10,100,1]
                    so I couldn't realize what actually made.
                    please explain me about this.
                    If I understand you correctly,
                    System.out.println(arr); 
                    calls the ArrayList's toString() method, which is inherited from AbstractCollection.
                    If you look at the java API for that class, it explains why you're getting the ouput you're getting, i.e. the list of elements in square brackets.
                    • 7. Re: Doubt in Collections
                      791266
                      calls the ArrayList's toString() method, which is
                      inherited from AbstractCollection.
                      If you look at the java API for that class, it
                      explains why you're getting the ouput you're getting,
                      i.e. the list of elements in square brackets.
                      I don't think what he was asking about :)

                      He said that the collections didn't support primitives, and then tested that and saw that the code compiled, and could execute. He could even see that he had numbers in the collection.

                      @Op. The reason that it is working is that the primitives that you are using are autoboxed, so the collection does not contain primitives, it contains instances of the wrapper classes. The same code will not compile in e.g. JDK 1.4
                      • 8. Re: Doubt in Collections
                        807605
                        calls the ArrayList's toString() method, which is
                        inherited from AbstractCollection.
                        If you look at the java API for that class, it
                        explains why you're getting the ouput you're
                        getting,
                        i.e. the list of elements in square brackets.
                        I don't think what he was asking about :)
                        Looking again at what the OP posted, I'll have to agree.

                        My speed-reading technique is still alpha-quality.
                        • 9. Re: Doubt in Collections
                          791266
                          I don't think what he was asking about :)
                          Looking again at what the OP posted, I'll have to
                          agree.

                          My speed-reading technique is still alpha-quality.
                          ..and it looks like I was typing to fast. Even I have a hard time understanding the quoted sentence :)
                          • 10. Re: Doubt in Collections
                            807605
                            Hi kajbj,

                            Thank u for ur response. I want to know what is autoboxing .
                            Is there relation between autoboxing and typecasting. Could U please explain what is autoboxing. In java from which places autoboxing occured.

                            Thanks in Advance,
                            Maheshwaran Devaraj
                            • 11. Re: Doubt in Collections
                              791266
                              Thank u for ur response. I want to know
                              what is autoboxing .
                              You should have googled now when you had something to google on:

                              http://www.google.com/search?q=java+autoboxing
                              is there relation between autoboxing and typecasting.
                              No

                              Kaj
                              • 12. Re: Doubt in Collections
                                807605
                                http://en.wikipedia.org/wiki/Autoboxing#Autoboxing
                                • 13. Re: Doubt in Collections
                                  807605
                                  return col.contains(doubt);