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

Thread: how to do dynamic casting for all datatypes

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to do dynamic casting for all datatypes

    ublic class MethodsExample {
     
    	public void add(int a, int b) {
    		int add = a + b;
    		System.out.println("Addtion value=" + add);
    	}
     
    	public void sub(int a, int b) {
    		int sub = a - b;
    		System.out.println("Substraction value=" + sub);
    	}
     
    	public void mul(int a, int b) {
    		int mul = a * b;
    		System.out.println("Multiplication value=" + mul);
    	}
     
    	public void div(int a, int b) {
    		int div = a / b;
    		System.out.println("Division value=" + div);
    	}
     
    	public static void main(String[] args) {
    		MethodsExample obj = new MethodsExample();
    		obj.add(5, 6);
    		obj.sub(5, 6);//here if i want any other data types to pass as a parameter what i ve to do for that please help me
    		obj.mul(5, 6);
    		obj.div(5, 6);
     
    		obj.add(100, 500);
    		obj.add(800, 900);
     
    	}
     
    }


    --- Update ---

    if i sent by different data types i ve to get convertes in that method 'm not getting how to do that please help me
    Last edited by Norm; December 11th, 2013 at 06:28 AM. Reason: added end code tag


  2. #2
    Member
    Join Date
    Nov 2013
    Location
    Bangalore, India
    Posts
    69
    My Mood
    Cool
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: how to do dynamic casting for all datatypes

    you can send a subclass of a particular type using generics or mark the argument as object to pass any datatype but however not sure what kind of converter you expect?

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,459
    My Mood
    Mellow
    Thanks
    148
    Thanked 607 Times in 594 Posts

    Default Re: how to do dynamic casting for all datatypes

    Welcome to the Forum! Please read this topic to learn how to post your code correctly along with other useful info for newcomers.

    You can overload the method with others that accept different type parameters:

    public void sub(int a, int b) {}

    public void sub( double a, double b ) {}

    Etc. . . . Is that what you mean?

  4. #4
    Junior Member
    Join Date
    Dec 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to do dynamic casting for all datatypes

    no i want in main method if i send any other datatypes exbj(2.0,3.0) i want it convert in the method as generic if i want to put any type of data type parameters when i called t have to convert to that type for that what i have to do is there any method like type cast of generally or i have to use switch for every data types?????/ please help me

    --- Update ---

    9
    if i want any of method called in different data types like obj.add('a','b') or obj.add(2.0,5.0) i want to convert in method parameters when i called in main method for that what i have to do generically please help me its urgent
    public static void main(String[] args) {
    MethodsExample obj = new MethodsExample();
    obj.add(5, 6);
    obj.sub(5, 6);//here if i want any other data types to pass as a parameter what i ve to do for that please help me
    obj.mul(5, 6);
    obj.div(5, 6);
    Last edited by Norm; December 11th, 2013 at 06:29 AM. Reason: added [/code]

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: how to do dynamic casting for all datatypes

    Are you talking about method overloading? Having multiple methods with the same name but different parameters?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Dec 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to do dynamic casting for all datatypes

    no i posted one prog above in that if i called obj.add(anydatatype variable,anydatatype variable) i want to do that operation in that method like if i calle obj.add(2.0,3.0) i want answere in float parameter want to get know the particular type for that what have to do if i send obj.add(int i,int b) ,obj.add(boolean i,short b) ,obj.add(float i,float b) ,obj.add(double i,int b) like any data type if i sent to add method there i have to do converstion i written like public void add(int a, int b) {
    int add = a + b;
    System.out.println("Addtion value=" + add);
    }
    this is for only one datatype operation is integer if i want obj.add(2.0,3) how to do in methods i showed below as anydatatype in that place what i have to do for all data type convertion
    public void add(anydatatype a, anydatatype b) {
    anydatatype add = a + b;
    System.out.println("Addtion value=" + add);
    }

    --- Update ---

    is there any generic converstion or else how shall i do please explain t with any example i hope you understands my problem

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: how to do dynamic casting for all datatypes

    Have you tried writing the code using generics? The code in post#1 does not use generics. Try rewriting the code to use generics.
    For example:
     public class MethodsExample<T> {
     
    ...
      MethodsExample<Double> me = new MethodsExample<Double>();
      me.add(1.2, 2.3);
    //  and
     MethodsExample<Integer> me2 = new MethodsExample<Integer>();
     me2.add(12, 23);
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Dec 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to do dynamic casting for all datatypes

    oki i will try this thank you

  9. #9
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,607
    Thanks
    24
    Thanked 314 Times in 294 Posts

    Default Re: how to do dynamic casting for all datatypes

    Continuing on Norm's suggestion and going along with your current question, consider restricting your generic arguments to numeric values only (if that is what you want).

    For example (stealing Norm's code):
     public class MethodsExample<T extends java.lang.Number> {
     
    ...
      MethodsExample<Double> me = new MethodsExample<Double>();
      me.add(1.2, 2.3);
    //  and
     MethodsExample<Integer> me2 = new MethodsExample<Integer>();
     me2.add(12, 23);
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  10. #10
    Junior Member
    Join Date
    Dec 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to do dynamic casting for all datatypes

    i just want when i call the parameters with different datatypes in that particular method want to do that particular type of operation m just a beginner of java so dont know how to do that currently 'm learning methods with those concepts 'm trying to solve

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: how to do dynamic casting for all datatypes

    To call the methods with the same names but different data types for its args,
    either define overloaded methods each with the desired data type
    or use generics and define a class to handle the desired data type
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Dec 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to do dynamic casting for all datatypes

    i tried that with same name and different args what happened is for add method i have to create different data types args and next for sub i have to create different args with sub method name so its going lengthy prog for all add,sub,mul,div

  13. #13
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,607
    Thanks
    24
    Thanked 314 Times in 294 Posts

    Default Re: how to do dynamic casting for all datatypes

    Actually, this is far more complex than I assumed because you cannot do arithmetic on a Number object...
    Hmm... There is certainly a messy way of doing this, but I do not know if there is a generic way of doing this.
    The messy way of doing it would be something like:
    public <T extends Number> T add(T val1, T val2) {
            if(val1 instanceof Integer) {
                return (T)new Integer((Integer)val1 + (Integer)val2);
            }
            else if(val1 instanceof Double) {
                return (T)new Double((Double)val1 + (Double)val2);
            }
            // And follow this pattern for every Number type
        }
    It is not ideal. Maybe someone else has a better idea.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. About casting
    By CheukKwan in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 18th, 2013, 11:03 AM
  2. Explicit Casting?
    By syregnar86 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 14th, 2013, 06:44 PM
  3. Casting Theory
    By BigDru in forum Java Theory & Questions
    Replies: 1
    Last Post: October 25th, 2012, 08:21 AM
  4. Dymaic Casting of List
    By jaypee81 in forum Collections and Generics
    Replies: 1
    Last Post: February 23rd, 2012, 03:46 PM
  5. Class Casting
    By Sana1990 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 9th, 2012, 07:13 AM