Results 1 to 6 of 6
  1. #1
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default [SOLVED] Java operator overloading

    Hello everyone.

    I have an interesting question to ask. I have seen on a few forums that operator overloading is not possible in Java. So then, why can we use the following?
    Java Code:
        Integer a = new Integer(1);
        Integer b = new Integer(2);
        Integer c = a + b;
    I've tested this and it seems to work. Could this be due to Java's auto-boxing? In this case a, b and c are all instances of Integer. And the + means the addition of two Integer objects, which then returns an Integer object. Could it be that the objects, a and b, are converted to their primitive type equivalents, added and then converted back to an Integer object?

    Just a strange thing I picked up. Thank you. ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  2. #2
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    I believe but could be wrong, but operator overloading refers like in C++ where you change what an operator does. In C++ I could make the + really mean multiply or concatenate two strings. Java has no such function other that what is already defined in java.
    My IP address is 127.0.0.1

  3. #3
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    Hello Zosden

    Can you explain why my code snippet compiles and runs successfully?

    Thank you. :D
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  4. #4
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    Because I do believe correct me if I'm wrong but Integer can be made into int form in java. They are almost the same to a programmer in many aspects.
    My IP address is 127.0.0.1

  5. #5
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Based on the JavaDoc's first line:
    The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.
    I would then deduce that a resolves to 1, b resolves to 2 and c resolves to the value of 1 + 2, or 3.

    Java Code:
        Integer a = new Integer(1);
        Integer b = new Integer(2);
        Integer c = a + b;
      
        System.out.println(c);
    Output:
    3

    :)
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  6. #6
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    Thank you CaptainMorgan and Zosden ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

Similar Threads

  1. Replies: 2
    Last Post: 11-26-2010, 03:12 AM
  2. problem with operator in case
    By jimJohnson in forum New To Java
    Replies: 2
    Last Post: 03-21-2008, 09:22 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

Posting Permissions

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