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 4 of 4

Thread: How to create a Java generic method, similar to a C++ template method?

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    24
    My Mood
    Angelic
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default How to create a Java generic method, similar to a C++ template method?

    Hi all,
    I'm new to Java and I used to program in C++. Now I know that there is a way in C++ to use the templates to create generic methods that can take different types of variable as input. Is this applicable in Java??
    I have written this method:

     static boolean inject1=true;
    	  public static boolean check_equality(int v11, int v21){
    		  if(v11==v21)
    			  if(inject1){
    				  inject1=false;
    				  return false;
    				  }
    				  else 
    			  return true;
    		 if(inject1)
    			 return true;
     
    		return false;
     
    	  }

    Now I want this method to take int, double and float for example as inputs.
    Any suggestions are appreciated.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to create a Java generic method, similar to a C++ template method?

    Suggested reading: Generic Methods in Java

    Java generics can't be primitive types (int, float, double, etc.), but luckily Java has auto-boxing. You can somewhat restrict the type you can pass in by using extends for the generic type:

    public static <T extends Number> boolean check_equality(T v11, T v21)

    However, there are a few caveats:

    1. I could call check_equality with any class which extends Number, such as: check_equality(Float, Integer) or check_equality(BigInteger, Long).

    2. Because you are now dealing with objects you should use the .equals() method instead of the == operator.

    The alternative is if you want to only allow check_equality(int, int), check_equality(float, float), and check_equality(double, double) (or any other combination) is to copy/paste the function and change the parameter types.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    Sharmeen (October 18th, 2012)

  4. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    24
    My Mood
    Angelic
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to create a Java generic method, similar to a C++ template method?

    This is really appreciated. I will give it a try and will let you know about the results.
    Thanks.

  5. #4
    Junior Member
    Join Date
    Oct 2012
    Posts
    24
    My Mood
    Angelic
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to create a Java generic method, similar to a C++ template method?

    I think I will go to the traditional way by Copy/paste the method as I only need to check the equality of integers and doubles.
    Thanx

Similar Threads

  1. create a test class (main method) to start(run) the class in Java
    By curious725 in forum Java Theory & Questions
    Replies: 5
    Last Post: August 1st, 2012, 03:21 AM
  2. Using a Generic Insertion Sort method
    By dubois.ford in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 6th, 2011, 06:08 PM
  3. hello.,i would like to create a method that accepts ...
    By amr in forum Java Theory & Questions
    Replies: 2
    Last Post: November 20th, 2010, 04:13 PM
  4. Generic method using an int[] argument
    By dubois.ford in forum Collections and Generics
    Replies: 2
    Last Post: March 18th, 2010, 07:18 AM
  5. [SOLVED] How to dynamically create ArrayLists (or something similar)?
    By igniteflow in forum Collections and Generics
    Replies: 4
    Last Post: August 6th, 2009, 06:00 AM