Results 1 to 7 of 7

Thread: overloading

  1. #1
    Bhuvan is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default overloading

    why does it select the function with float instead of double?????

    public class Test
    {
    void show(double a)
    {
    System.out.println("Inside double");
    }
    void show(float f)
    {
    System.out.println("Inside float");
    }
    public static void main(String[] args)
    {
    Test t=new Test();
    t.show(4); //4.0 is an int value
    }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Because the most specific method is chosen. If that link and the related discussion of method invocation conversion and widening primitive conversion is difficult to follow, avoid overloading.

    Basically it boils down to the fact that a float may be converted to a double by means of a widening primitive conversion, but not vice versa. A method wit a float argument is said to be more specific because of that.
    Last edited by pbrockway2; 12-28-2010 at 04:13 PM.

  3. #3
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    You can tell Java to use the double parametered method
    my casting to double, this is normally only used when the
    compiler generates an error due to ambiguousy when using null.

    t.show((double)4);

    Or if you used null

    method((ClassName)null);


    But otherwise, if you do not cast, pbrockway2 had the answer.

  4. #4
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Post

    This is known as tricky overloading

  5. #5
    cherylfoster is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    overloading is a feature found in different programming languages such as Ada, C # in, VB.Net, C + + in, D, and Java, allows you to create a variety of methods with the same name, each with the other provisions of Types of input and output types. I am intrested in Java programming.

  6. #6
    madnaelo is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default

    If you want a number to be a "float" then you have to put an "f" at the end

    e.g. show(4f);

    now void show(float); function will be called.

  7. #7
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Bhuvan View Post
    t.show(4); //4.0 is an int value

    the comment is wrong. in java decimal values are interpreted as a double by default.

Similar Threads

  1. Overloading qn
    By diskhub in forum New To Java
    Replies: 18
    Last Post: 06-07-2010, 06:19 PM
  2. method overloading and arrays.
    By glopez09 in forum New To Java
    Replies: 5
    Last Post: 12-08-2009, 04:02 PM
  3. Overloading?
    By padutch2 in forum New To Java
    Replies: 2
    Last Post: 12-31-2007, 04:26 AM
  4. Method/Operator Overloading
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-01-2007, 09:33 PM
  5. Overloading methods dynamically
    By Syed.muddasir in forum New To Java
    Replies: 6
    Last Post: 07-31-2007, 03:34 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •