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

Thread: How to perform operations on arguments with type OBJECT?

  1. #1
    Member hexwind's Avatar
    Join Date
    May 2011
    Location
    Tunisia
    Posts
    48
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default How to perform operations on arguments with type OBJECT?

    Hello guys
    I'm a Java beginner. I just learned class access controls and whatnot, and I wanted to make a class that I called "Calculus" that has different methods for calculus (addition, multiplication etc). I want the arguments of my methods to handle any type, so I have to use Object (no?). With this, i cannot perform any operation on the arguments. here is an example :
    public class calculus {
     
       public Object add(Object x, Object y)
       {
           return x+y;
     
       }
    }

    I get this error message : Bad Operand types for binary operator '+'

    can anybody help?

    P.S, i don't know if there is already a class in the standard java package that has the methods that i'm writing.
    My website : MediDev


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to perform operations on arguments with type OBJECT?

    If it is just a mathematical operation, define the method parameters as primitives which can be used directly for mathematical operations. Alternatively, you could pass something like a Number object, using the get* methods to retrieve a primitive value
    See http://download.oracle.com/javase/tu...datatypes.html

  3. #3
    Member hexwind's Avatar
    Join Date
    May 2011
    Location
    Tunisia
    Posts
    48
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: How to perform operations on arguments with type OBJECT?

    Quote Originally Posted by copeg View Post
    If it is just a mathematical operation, define the method parameters as primitives which can be used directly for mathematical operations. Alternatively, you could pass something like a Number object, using the get* methods to retrieve a primitive value
    See Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
    From what I understand, I have to import the Number library using "import java.lang.Number;" and then 'convert' the arguments of my method to primitive values?
    How would I know if it's an integer that is passed or it's a float? Is there any solution that works regardless of the argument type?
    My website : MediDev

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to perform operations on arguments with type OBJECT?

    java.lang is a default import so there is not need to explicitly import (give it a try and see). Your methods should define what they accept and the precision they require. If you need floating point math, then define the method as floating point - don't rely on clients (the caller) specifying that behavior - it should be specified in the class you posted.
    public double add(double a, double b){
        return a + b;
    }
    public Number add(Number a, Number b){
        return new Double(a.doubleValue() + b.doubleValue());
    }

  5. #5
    Member hexwind's Avatar
    Join Date
    May 2011
    Location
    Tunisia
    Posts
    48
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: How to perform operations on arguments with type OBJECT?

    So I should figure out the type of the argument first and change it accordingly and work on it? because, for example, if the client passes it with an integer, i don't want to change it to anything else. i want to perform the operation without changing the arugment's type.
    I'm sorry for my slow understanding, i'm just trying to get a clearer picture :p
    My website : MediDev

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to perform operations on arguments with type OBJECT?

    Look at overloading your add method. Have a method for each type of data. See post #4

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to perform operations on arguments with type OBJECT?

    A good example of the overloading Norm suggests is demonstrate in Math (Java Platform SE 6)
    For example, the abs/min/max methods have several methods which accept different primitives but accomplish the same task

Similar Threads

  1. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM
  2. Help with OS operations!
    By benglish in forum Java Theory & Questions
    Replies: 8
    Last Post: April 1st, 2011, 06:32 AM
  3. [SOLVED] Servlet cant perform sql after second button click
    By Stefan_Lam in forum What's Wrong With My Code?
    Replies: 10
    Last Post: March 31st, 2011, 05:18 AM
  4. Operations with lists
    By datreta in forum Collections and Generics
    Replies: 8
    Last Post: October 29th, 2010, 08:54 AM
  5. [SOLVED] my code didnt perform its function
    By jamal in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 21st, 2010, 04:17 PM