What does (angle brackets) mean in Java?











up vote
100
down vote

favorite
31












I am currently studying Java and have recently been stumped by angle brackets(<>). What exactly do they mean?



public class Pool<T>{
public interface PoolFactory<T>{
public T createObject();
}
this.freeObjects= new ArrayList<T>(maxsize)
}


What does the <T> mean? Does it means that I can create an object of type T?










share|improve this question




















  • 11




    google java generics
    – jcomeau_ictx
    Jul 7 '11 at 8:04






  • 4




    I'm finding it hard to parse your code. There's no indentation for a start, and there are two open braces and only one close. Is T createObject supposed to be inside Pool or PoolFactory? Where is this.freeObjects = ... supposed to be? Is that a separate example? It is illegal to have it there; it has to be inside a method.
    – mgiuca
    Jul 7 '11 at 8:17










  • If someone is looking for information about <> (diamond operator) which could be used like List<Integer> list = new ArrayList<>(); visit docs.oracle.com/javase/7/docs/technotes/guides/language/….
    – Pshemo
    Mar 1 at 17:21















up vote
100
down vote

favorite
31












I am currently studying Java and have recently been stumped by angle brackets(<>). What exactly do they mean?



public class Pool<T>{
public interface PoolFactory<T>{
public T createObject();
}
this.freeObjects= new ArrayList<T>(maxsize)
}


What does the <T> mean? Does it means that I can create an object of type T?










share|improve this question




















  • 11




    google java generics
    – jcomeau_ictx
    Jul 7 '11 at 8:04






  • 4




    I'm finding it hard to parse your code. There's no indentation for a start, and there are two open braces and only one close. Is T createObject supposed to be inside Pool or PoolFactory? Where is this.freeObjects = ... supposed to be? Is that a separate example? It is illegal to have it there; it has to be inside a method.
    – mgiuca
    Jul 7 '11 at 8:17










  • If someone is looking for information about <> (diamond operator) which could be used like List<Integer> list = new ArrayList<>(); visit docs.oracle.com/javase/7/docs/technotes/guides/language/….
    – Pshemo
    Mar 1 at 17:21













up vote
100
down vote

favorite
31









up vote
100
down vote

favorite
31






31





I am currently studying Java and have recently been stumped by angle brackets(<>). What exactly do they mean?



public class Pool<T>{
public interface PoolFactory<T>{
public T createObject();
}
this.freeObjects= new ArrayList<T>(maxsize)
}


What does the <T> mean? Does it means that I can create an object of type T?










share|improve this question















I am currently studying Java and have recently been stumped by angle brackets(<>). What exactly do they mean?



public class Pool<T>{
public interface PoolFactory<T>{
public T createObject();
}
this.freeObjects= new ArrayList<T>(maxsize)
}


What does the <T> mean? Does it means that I can create an object of type T?







java generics






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 1 at 17:18









Pshemo

93.2k14129183




93.2k14129183










asked Jul 7 '11 at 8:03









Laughy

66951320




66951320








  • 11




    google java generics
    – jcomeau_ictx
    Jul 7 '11 at 8:04






  • 4




    I'm finding it hard to parse your code. There's no indentation for a start, and there are two open braces and only one close. Is T createObject supposed to be inside Pool or PoolFactory? Where is this.freeObjects = ... supposed to be? Is that a separate example? It is illegal to have it there; it has to be inside a method.
    – mgiuca
    Jul 7 '11 at 8:17










  • If someone is looking for information about <> (diamond operator) which could be used like List<Integer> list = new ArrayList<>(); visit docs.oracle.com/javase/7/docs/technotes/guides/language/….
    – Pshemo
    Mar 1 at 17:21














  • 11




    google java generics
    – jcomeau_ictx
    Jul 7 '11 at 8:04






  • 4




    I'm finding it hard to parse your code. There's no indentation for a start, and there are two open braces and only one close. Is T createObject supposed to be inside Pool or PoolFactory? Where is this.freeObjects = ... supposed to be? Is that a separate example? It is illegal to have it there; it has to be inside a method.
    – mgiuca
    Jul 7 '11 at 8:17










  • If someone is looking for information about <> (diamond operator) which could be used like List<Integer> list = new ArrayList<>(); visit docs.oracle.com/javase/7/docs/technotes/guides/language/….
    – Pshemo
    Mar 1 at 17:21








11




11




google java generics
– jcomeau_ictx
Jul 7 '11 at 8:04




google java generics
– jcomeau_ictx
Jul 7 '11 at 8:04




4




4




I'm finding it hard to parse your code. There's no indentation for a start, and there are two open braces and only one close. Is T createObject supposed to be inside Pool or PoolFactory? Where is this.freeObjects = ... supposed to be? Is that a separate example? It is illegal to have it there; it has to be inside a method.
– mgiuca
Jul 7 '11 at 8:17




I'm finding it hard to parse your code. There's no indentation for a start, and there are two open braces and only one close. Is T createObject supposed to be inside Pool or PoolFactory? Where is this.freeObjects = ... supposed to be? Is that a separate example? It is illegal to have it there; it has to be inside a method.
– mgiuca
Jul 7 '11 at 8:17












If someone is looking for information about <> (diamond operator) which could be used like List<Integer> list = new ArrayList<>(); visit docs.oracle.com/javase/7/docs/technotes/guides/language/….
– Pshemo
Mar 1 at 17:21




If someone is looking for information about <> (diamond operator) which could be used like List<Integer> list = new ArrayList<>(); visit docs.oracle.com/javase/7/docs/technotes/guides/language/….
– Pshemo
Mar 1 at 17:21












6 Answers
6






active

oldest

votes

















up vote
115
down vote













<T> is a generic and can usually be read as "of type T". It depends on the type to the left of the <> what it actually means.



I don't know what a Pool or PoolFactory is, but you also mention ArrayList<T>, which is a standard Java class, so I'll talk to that.



Usually, you won't see "T" in there, you'll see another type. So if you see ArrayList<Integer> for example, that means "An ArrayList of Integers." Many classes use generics to constrain the type of the elements in a container, for example. Another example is HashMap<String, Integer>, which means "a map with String keys and Integer values."



Your Pool example is a bit different, because there you are defining a class. So in that case, you are creating a class that somebody else could instantiate with a particular type in place of T. For example, I could create an object of type Pool<String> using your class definition. That would mean two things:




  • My Pool<String> would have an interface PoolFactory<String> with a createObject method that returns Strings.

  • Internally, the Pool<String> would contain an ArrayList of Strings.


This is great news, because at another time, I could come along and create a Pool<Integer> which would use the same code, but have Integer wherever you see T in the source.






share|improve this answer




























    up vote
    20
    down vote













    It is related to generics in java. If I mentioned ArrayList<String> that means I can add only String type object to that ArrayList.



    The two major benefits of generics in Java are:




    1. Reducing the number of casts in your program, thus reducing the number of potential bugs in your program.

    2. Improving code clarity






    share|improve this answer






























      up vote
      16
      down vote













      It's really simple. It's a new feature introduced in J2SE 5. Specifying angular brackets after the class name means you are creating a temporary data type which can hold any type of data.



      Example:



      class A<T>{
      T obj;
      void add(T obj){
      this.obj=obj;
      }
      T get(){
      return obj;
      }
      }
      public class generics {
      static<E> void print(E elements){
      for(E element:elements){
      System.out.println(element);
      }
      }

      public static void main(String args) {
      A<String> obj=new A<String>();
      A<Integer> obj1=new A<Integer>();
      obj.add("hello");
      obj1.add(6);
      System.out.println(obj.get());
      System.out.println(obj1.get());

      Integer arr={1,3,5,7};
      print(arr);
      }
      }


      Instead of <T>, you can actually write anything and it will work the same way. Try writing <ABC> in place of <T>.



      This is just for convenience:





      • <T> is referred to as any type


      • <E> as element type


      • <N> as number type


      • <V> as value


      • <K> as key


      But you can name it anything you want, it doesn't really matter.



      Moreover, Integer, String, Boolean etc are wrapper classes of Java which help in checking of types during compilation. For example, in the above code, obj is of type String, so you can't add any other type to it (try obj.add(1), it will cast an error). Similarly, obj1 is of the Integer type, you can't add any other type to it (try obj1.add("hello"), error will be there).






      share|improve this answer






























        up vote
        8
        down vote













        is called a generic type. You can instantiate an object Pool like this:



        PoolFactory<Integer> pool = new Pool<Integer>();


        The generic parameter can only be a reference type. So you can't use primitive types like int or double or char or other primitive types.






        share|improve this answer




























          up vote
          6
          down vote













          <> is used to indicate generics in Java.



          T is a type parameter in this example. And no: instantiating is one of the few things that you can't do with T.



          Apart from the tutorial linked above Angelika Langers Generics FAQ is a great resource on the topic.






          share|improve this answer




























            up vote
            4
            down vote













            Generic classes are a type of class that takes in a data type as a parameter when it's created. This type parameter is specified using angle brackets and the type can change each time a new instance of the class is instantiated. For instance, let's create an ArrayList for Employee objects and another for Company objects



            ArrayList<Employee> employees = new ArrayList<Employee>();
            ArrayList<Company> companies = new ArrayList<Company>();


            You'll notice that we're using the same ArrayList class to create both lists and we pass in the Employee or Company type using angle brackets. Having one generic class be able to handle multiple types of data cuts down on having a lot of classes that perform similar tasks.
            Generics also help to cut down on bugs by giving everything a strong type which helps the compiler point out errors. By specifying a type for ArrayList, the compiler will throw an error if you try to add an Employee to the Company list or vice versa.






            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6607550%2fwhat-does-t-angle-brackets-mean-in-java%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              6 Answers
              6






              active

              oldest

              votes








              6 Answers
              6






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              115
              down vote













              <T> is a generic and can usually be read as "of type T". It depends on the type to the left of the <> what it actually means.



              I don't know what a Pool or PoolFactory is, but you also mention ArrayList<T>, which is a standard Java class, so I'll talk to that.



              Usually, you won't see "T" in there, you'll see another type. So if you see ArrayList<Integer> for example, that means "An ArrayList of Integers." Many classes use generics to constrain the type of the elements in a container, for example. Another example is HashMap<String, Integer>, which means "a map with String keys and Integer values."



              Your Pool example is a bit different, because there you are defining a class. So in that case, you are creating a class that somebody else could instantiate with a particular type in place of T. For example, I could create an object of type Pool<String> using your class definition. That would mean two things:




              • My Pool<String> would have an interface PoolFactory<String> with a createObject method that returns Strings.

              • Internally, the Pool<String> would contain an ArrayList of Strings.


              This is great news, because at another time, I could come along and create a Pool<Integer> which would use the same code, but have Integer wherever you see T in the source.






              share|improve this answer

























                up vote
                115
                down vote













                <T> is a generic and can usually be read as "of type T". It depends on the type to the left of the <> what it actually means.



                I don't know what a Pool or PoolFactory is, but you also mention ArrayList<T>, which is a standard Java class, so I'll talk to that.



                Usually, you won't see "T" in there, you'll see another type. So if you see ArrayList<Integer> for example, that means "An ArrayList of Integers." Many classes use generics to constrain the type of the elements in a container, for example. Another example is HashMap<String, Integer>, which means "a map with String keys and Integer values."



                Your Pool example is a bit different, because there you are defining a class. So in that case, you are creating a class that somebody else could instantiate with a particular type in place of T. For example, I could create an object of type Pool<String> using your class definition. That would mean two things:




                • My Pool<String> would have an interface PoolFactory<String> with a createObject method that returns Strings.

                • Internally, the Pool<String> would contain an ArrayList of Strings.


                This is great news, because at another time, I could come along and create a Pool<Integer> which would use the same code, but have Integer wherever you see T in the source.






                share|improve this answer























                  up vote
                  115
                  down vote










                  up vote
                  115
                  down vote









                  <T> is a generic and can usually be read as "of type T". It depends on the type to the left of the <> what it actually means.



                  I don't know what a Pool or PoolFactory is, but you also mention ArrayList<T>, which is a standard Java class, so I'll talk to that.



                  Usually, you won't see "T" in there, you'll see another type. So if you see ArrayList<Integer> for example, that means "An ArrayList of Integers." Many classes use generics to constrain the type of the elements in a container, for example. Another example is HashMap<String, Integer>, which means "a map with String keys and Integer values."



                  Your Pool example is a bit different, because there you are defining a class. So in that case, you are creating a class that somebody else could instantiate with a particular type in place of T. For example, I could create an object of type Pool<String> using your class definition. That would mean two things:




                  • My Pool<String> would have an interface PoolFactory<String> with a createObject method that returns Strings.

                  • Internally, the Pool<String> would contain an ArrayList of Strings.


                  This is great news, because at another time, I could come along and create a Pool<Integer> which would use the same code, but have Integer wherever you see T in the source.






                  share|improve this answer












                  <T> is a generic and can usually be read as "of type T". It depends on the type to the left of the <> what it actually means.



                  I don't know what a Pool or PoolFactory is, but you also mention ArrayList<T>, which is a standard Java class, so I'll talk to that.



                  Usually, you won't see "T" in there, you'll see another type. So if you see ArrayList<Integer> for example, that means "An ArrayList of Integers." Many classes use generics to constrain the type of the elements in a container, for example. Another example is HashMap<String, Integer>, which means "a map with String keys and Integer values."



                  Your Pool example is a bit different, because there you are defining a class. So in that case, you are creating a class that somebody else could instantiate with a particular type in place of T. For example, I could create an object of type Pool<String> using your class definition. That would mean two things:




                  • My Pool<String> would have an interface PoolFactory<String> with a createObject method that returns Strings.

                  • Internally, the Pool<String> would contain an ArrayList of Strings.


                  This is great news, because at another time, I could come along and create a Pool<Integer> which would use the same code, but have Integer wherever you see T in the source.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jul 7 '11 at 8:12









                  mgiuca

                  16.4k54466




                  16.4k54466
























                      up vote
                      20
                      down vote













                      It is related to generics in java. If I mentioned ArrayList<String> that means I can add only String type object to that ArrayList.



                      The two major benefits of generics in Java are:




                      1. Reducing the number of casts in your program, thus reducing the number of potential bugs in your program.

                      2. Improving code clarity






                      share|improve this answer



























                        up vote
                        20
                        down vote













                        It is related to generics in java. If I mentioned ArrayList<String> that means I can add only String type object to that ArrayList.



                        The two major benefits of generics in Java are:




                        1. Reducing the number of casts in your program, thus reducing the number of potential bugs in your program.

                        2. Improving code clarity






                        share|improve this answer

























                          up vote
                          20
                          down vote










                          up vote
                          20
                          down vote









                          It is related to generics in java. If I mentioned ArrayList<String> that means I can add only String type object to that ArrayList.



                          The two major benefits of generics in Java are:




                          1. Reducing the number of casts in your program, thus reducing the number of potential bugs in your program.

                          2. Improving code clarity






                          share|improve this answer














                          It is related to generics in java. If I mentioned ArrayList<String> that means I can add only String type object to that ArrayList.



                          The two major benefits of generics in Java are:




                          1. Reducing the number of casts in your program, thus reducing the number of potential bugs in your program.

                          2. Improving code clarity







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Aug 20 '13 at 1:12









                          Paul Bellora

                          45k15111161




                          45k15111161










                          answered Jul 7 '11 at 9:39







                          user831722





























                              up vote
                              16
                              down vote













                              It's really simple. It's a new feature introduced in J2SE 5. Specifying angular brackets after the class name means you are creating a temporary data type which can hold any type of data.



                              Example:



                              class A<T>{
                              T obj;
                              void add(T obj){
                              this.obj=obj;
                              }
                              T get(){
                              return obj;
                              }
                              }
                              public class generics {
                              static<E> void print(E elements){
                              for(E element:elements){
                              System.out.println(element);
                              }
                              }

                              public static void main(String args) {
                              A<String> obj=new A<String>();
                              A<Integer> obj1=new A<Integer>();
                              obj.add("hello");
                              obj1.add(6);
                              System.out.println(obj.get());
                              System.out.println(obj1.get());

                              Integer arr={1,3,5,7};
                              print(arr);
                              }
                              }


                              Instead of <T>, you can actually write anything and it will work the same way. Try writing <ABC> in place of <T>.



                              This is just for convenience:





                              • <T> is referred to as any type


                              • <E> as element type


                              • <N> as number type


                              • <V> as value


                              • <K> as key


                              But you can name it anything you want, it doesn't really matter.



                              Moreover, Integer, String, Boolean etc are wrapper classes of Java which help in checking of types during compilation. For example, in the above code, obj is of type String, so you can't add any other type to it (try obj.add(1), it will cast an error). Similarly, obj1 is of the Integer type, you can't add any other type to it (try obj1.add("hello"), error will be there).






                              share|improve this answer



























                                up vote
                                16
                                down vote













                                It's really simple. It's a new feature introduced in J2SE 5. Specifying angular brackets after the class name means you are creating a temporary data type which can hold any type of data.



                                Example:



                                class A<T>{
                                T obj;
                                void add(T obj){
                                this.obj=obj;
                                }
                                T get(){
                                return obj;
                                }
                                }
                                public class generics {
                                static<E> void print(E elements){
                                for(E element:elements){
                                System.out.println(element);
                                }
                                }

                                public static void main(String args) {
                                A<String> obj=new A<String>();
                                A<Integer> obj1=new A<Integer>();
                                obj.add("hello");
                                obj1.add(6);
                                System.out.println(obj.get());
                                System.out.println(obj1.get());

                                Integer arr={1,3,5,7};
                                print(arr);
                                }
                                }


                                Instead of <T>, you can actually write anything and it will work the same way. Try writing <ABC> in place of <T>.



                                This is just for convenience:





                                • <T> is referred to as any type


                                • <E> as element type


                                • <N> as number type


                                • <V> as value


                                • <K> as key


                                But you can name it anything you want, it doesn't really matter.



                                Moreover, Integer, String, Boolean etc are wrapper classes of Java which help in checking of types during compilation. For example, in the above code, obj is of type String, so you can't add any other type to it (try obj.add(1), it will cast an error). Similarly, obj1 is of the Integer type, you can't add any other type to it (try obj1.add("hello"), error will be there).






                                share|improve this answer

























                                  up vote
                                  16
                                  down vote










                                  up vote
                                  16
                                  down vote









                                  It's really simple. It's a new feature introduced in J2SE 5. Specifying angular brackets after the class name means you are creating a temporary data type which can hold any type of data.



                                  Example:



                                  class A<T>{
                                  T obj;
                                  void add(T obj){
                                  this.obj=obj;
                                  }
                                  T get(){
                                  return obj;
                                  }
                                  }
                                  public class generics {
                                  static<E> void print(E elements){
                                  for(E element:elements){
                                  System.out.println(element);
                                  }
                                  }

                                  public static void main(String args) {
                                  A<String> obj=new A<String>();
                                  A<Integer> obj1=new A<Integer>();
                                  obj.add("hello");
                                  obj1.add(6);
                                  System.out.println(obj.get());
                                  System.out.println(obj1.get());

                                  Integer arr={1,3,5,7};
                                  print(arr);
                                  }
                                  }


                                  Instead of <T>, you can actually write anything and it will work the same way. Try writing <ABC> in place of <T>.



                                  This is just for convenience:





                                  • <T> is referred to as any type


                                  • <E> as element type


                                  • <N> as number type


                                  • <V> as value


                                  • <K> as key


                                  But you can name it anything you want, it doesn't really matter.



                                  Moreover, Integer, String, Boolean etc are wrapper classes of Java which help in checking of types during compilation. For example, in the above code, obj is of type String, so you can't add any other type to it (try obj.add(1), it will cast an error). Similarly, obj1 is of the Integer type, you can't add any other type to it (try obj1.add("hello"), error will be there).






                                  share|improve this answer














                                  It's really simple. It's a new feature introduced in J2SE 5. Specifying angular brackets after the class name means you are creating a temporary data type which can hold any type of data.



                                  Example:



                                  class A<T>{
                                  T obj;
                                  void add(T obj){
                                  this.obj=obj;
                                  }
                                  T get(){
                                  return obj;
                                  }
                                  }
                                  public class generics {
                                  static<E> void print(E elements){
                                  for(E element:elements){
                                  System.out.println(element);
                                  }
                                  }

                                  public static void main(String args) {
                                  A<String> obj=new A<String>();
                                  A<Integer> obj1=new A<Integer>();
                                  obj.add("hello");
                                  obj1.add(6);
                                  System.out.println(obj.get());
                                  System.out.println(obj1.get());

                                  Integer arr={1,3,5,7};
                                  print(arr);
                                  }
                                  }


                                  Instead of <T>, you can actually write anything and it will work the same way. Try writing <ABC> in place of <T>.



                                  This is just for convenience:





                                  • <T> is referred to as any type


                                  • <E> as element type


                                  • <N> as number type


                                  • <V> as value


                                  • <K> as key


                                  But you can name it anything you want, it doesn't really matter.



                                  Moreover, Integer, String, Boolean etc are wrapper classes of Java which help in checking of types during compilation. For example, in the above code, obj is of type String, so you can't add any other type to it (try obj.add(1), it will cast an error). Similarly, obj1 is of the Integer type, you can't add any other type to it (try obj1.add("hello"), error will be there).







                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Mar 9 '17 at 0:58









                                  Pang

                                  6,8301563101




                                  6,8301563101










                                  answered Jul 21 '16 at 11:25









                                  Ruturaj Mohanty

                                  16114




                                  16114






















                                      up vote
                                      8
                                      down vote













                                      is called a generic type. You can instantiate an object Pool like this:



                                      PoolFactory<Integer> pool = new Pool<Integer>();


                                      The generic parameter can only be a reference type. So you can't use primitive types like int or double or char or other primitive types.






                                      share|improve this answer

























                                        up vote
                                        8
                                        down vote













                                        is called a generic type. You can instantiate an object Pool like this:



                                        PoolFactory<Integer> pool = new Pool<Integer>();


                                        The generic parameter can only be a reference type. So you can't use primitive types like int or double or char or other primitive types.






                                        share|improve this answer























                                          up vote
                                          8
                                          down vote










                                          up vote
                                          8
                                          down vote









                                          is called a generic type. You can instantiate an object Pool like this:



                                          PoolFactory<Integer> pool = new Pool<Integer>();


                                          The generic parameter can only be a reference type. So you can't use primitive types like int or double or char or other primitive types.






                                          share|improve this answer












                                          is called a generic type. You can instantiate an object Pool like this:



                                          PoolFactory<Integer> pool = new Pool<Integer>();


                                          The generic parameter can only be a reference type. So you can't use primitive types like int or double or char or other primitive types.







                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Jul 7 '11 at 8:10









                                          Rafiek

                                          5501515




                                          5501515






















                                              up vote
                                              6
                                              down vote













                                              <> is used to indicate generics in Java.



                                              T is a type parameter in this example. And no: instantiating is one of the few things that you can't do with T.



                                              Apart from the tutorial linked above Angelika Langers Generics FAQ is a great resource on the topic.






                                              share|improve this answer

























                                                up vote
                                                6
                                                down vote













                                                <> is used to indicate generics in Java.



                                                T is a type parameter in this example. And no: instantiating is one of the few things that you can't do with T.



                                                Apart from the tutorial linked above Angelika Langers Generics FAQ is a great resource on the topic.






                                                share|improve this answer























                                                  up vote
                                                  6
                                                  down vote










                                                  up vote
                                                  6
                                                  down vote









                                                  <> is used to indicate generics in Java.



                                                  T is a type parameter in this example. And no: instantiating is one of the few things that you can't do with T.



                                                  Apart from the tutorial linked above Angelika Langers Generics FAQ is a great resource on the topic.






                                                  share|improve this answer












                                                  <> is used to indicate generics in Java.



                                                  T is a type parameter in this example. And no: instantiating is one of the few things that you can't do with T.



                                                  Apart from the tutorial linked above Angelika Langers Generics FAQ is a great resource on the topic.







                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Jul 7 '11 at 8:06









                                                  Joachim Sauer

                                                  232k49480559




                                                  232k49480559






















                                                      up vote
                                                      4
                                                      down vote













                                                      Generic classes are a type of class that takes in a data type as a parameter when it's created. This type parameter is specified using angle brackets and the type can change each time a new instance of the class is instantiated. For instance, let's create an ArrayList for Employee objects and another for Company objects



                                                      ArrayList<Employee> employees = new ArrayList<Employee>();
                                                      ArrayList<Company> companies = new ArrayList<Company>();


                                                      You'll notice that we're using the same ArrayList class to create both lists and we pass in the Employee or Company type using angle brackets. Having one generic class be able to handle multiple types of data cuts down on having a lot of classes that perform similar tasks.
                                                      Generics also help to cut down on bugs by giving everything a strong type which helps the compiler point out errors. By specifying a type for ArrayList, the compiler will throw an error if you try to add an Employee to the Company list or vice versa.






                                                      share|improve this answer

























                                                        up vote
                                                        4
                                                        down vote













                                                        Generic classes are a type of class that takes in a data type as a parameter when it's created. This type parameter is specified using angle brackets and the type can change each time a new instance of the class is instantiated. For instance, let's create an ArrayList for Employee objects and another for Company objects



                                                        ArrayList<Employee> employees = new ArrayList<Employee>();
                                                        ArrayList<Company> companies = new ArrayList<Company>();


                                                        You'll notice that we're using the same ArrayList class to create both lists and we pass in the Employee or Company type using angle brackets. Having one generic class be able to handle multiple types of data cuts down on having a lot of classes that perform similar tasks.
                                                        Generics also help to cut down on bugs by giving everything a strong type which helps the compiler point out errors. By specifying a type for ArrayList, the compiler will throw an error if you try to add an Employee to the Company list or vice versa.






                                                        share|improve this answer























                                                          up vote
                                                          4
                                                          down vote










                                                          up vote
                                                          4
                                                          down vote









                                                          Generic classes are a type of class that takes in a data type as a parameter when it's created. This type parameter is specified using angle brackets and the type can change each time a new instance of the class is instantiated. For instance, let's create an ArrayList for Employee objects and another for Company objects



                                                          ArrayList<Employee> employees = new ArrayList<Employee>();
                                                          ArrayList<Company> companies = new ArrayList<Company>();


                                                          You'll notice that we're using the same ArrayList class to create both lists and we pass in the Employee or Company type using angle brackets. Having one generic class be able to handle multiple types of data cuts down on having a lot of classes that perform similar tasks.
                                                          Generics also help to cut down on bugs by giving everything a strong type which helps the compiler point out errors. By specifying a type for ArrayList, the compiler will throw an error if you try to add an Employee to the Company list or vice versa.






                                                          share|improve this answer












                                                          Generic classes are a type of class that takes in a data type as a parameter when it's created. This type parameter is specified using angle brackets and the type can change each time a new instance of the class is instantiated. For instance, let's create an ArrayList for Employee objects and another for Company objects



                                                          ArrayList<Employee> employees = new ArrayList<Employee>();
                                                          ArrayList<Company> companies = new ArrayList<Company>();


                                                          You'll notice that we're using the same ArrayList class to create both lists and we pass in the Employee or Company type using angle brackets. Having one generic class be able to handle multiple types of data cuts down on having a lot of classes that perform similar tasks.
                                                          Generics also help to cut down on bugs by giving everything a strong type which helps the compiler point out errors. By specifying a type for ArrayList, the compiler will throw an error if you try to add an Employee to the Company list or vice versa.







                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered May 20 '16 at 11:18









                                                          Adiii

                                                          5,4363135




                                                          5,4363135






























                                                              draft saved

                                                              draft discarded




















































                                                              Thanks for contributing an answer to Stack Overflow!


                                                              • Please be sure to answer the question. Provide details and share your research!

                                                              But avoid



                                                              • Asking for help, clarification, or responding to other answers.

                                                              • Making statements based on opinion; back them up with references or personal experience.


                                                              To learn more, see our tips on writing great answers.





                                                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                              Please pay close attention to the following guidance:


                                                              • Please be sure to answer the question. Provide details and share your research!

                                                              But avoid



                                                              • Asking for help, clarification, or responding to other answers.

                                                              • Making statements based on opinion; back them up with references or personal experience.


                                                              To learn more, see our tips on writing great answers.




                                                              draft saved


                                                              draft discarded














                                                              StackExchange.ready(
                                                              function () {
                                                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6607550%2fwhat-does-t-angle-brackets-mean-in-java%23new-answer', 'question_page');
                                                              }
                                                              );

                                                              Post as a guest















                                                              Required, but never shown





















































                                                              Required, but never shown














                                                              Required, but never shown












                                                              Required, but never shown







                                                              Required, but never shown

































                                                              Required, but never shown














                                                              Required, but never shown












                                                              Required, but never shown







                                                              Required, but never shown







                                                              Popular posts from this blog

                                                              A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                                                              Calculate evaluation metrics using cross_val_predict sklearn

                                                              Insert data from modal to MySQL (multiple modal on website)