Results 1 to 6 of 6
  1. #1
    PascalS is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Basic operations with selfmade datatypes

    Hello everyone

    In my actual project I made a new datatype called Vec3, that is basically just 3 double variables. (x, y and z)
    Now when I want to add, let's say Var2 to Var1 I always have to write this:

    Var1 = Vec3.Add(Var1, Var2); //Vec3.Add is a short function I wrote that adds 2 Vec3s together and returns the result

    Is there a way to make this shorter, so I can write it like this?
    Var1 += Var2;

    Where do I code the behavior for operations with new datatypes?

    Would be great if someone could help me! :)

  2. #2
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Basic operations with selfmade datatypes

    What I've been seeing on the internet is that operator overloading is not supported at all in java:
    coding style - Java operator overloading - Stack Overflow
    Why Java Doesn't Need Operator Overloading (and Very Few Languages Do, Really) | Javalobby

    So, you would not be able to do Var1 += Var2.

    Maybe someone can tell me if I wrong.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Basic operations with selfmade datatypes

    Moved from Advanced Java.

    Please learn to follow Java coding conventions. Variable and method names should start with a lowercase letter.

    @shall: I don't believe this is a question about operator overloading. Whether you need an add(...) method or you can write code like doubleVar1 += doubleVar2 depends on the visibility and scope of the variables themselves.

    Or maybe you believe that there isn't a += operator in Java?
    Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default Re: Basic operations with selfmade datatypes

    User defined operator overloading may seem handy or convenient for a few algebraic types such as vectors, matrixes or complex numbers but they're the source of a lot of evil and confusion for other data types, e.g. what is myTree/= myNode supposed to mean or mySpaceShip<<myBomb? On top of that, most people don't really understand what the return value of such operator application is supposed to be.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Basic operations with selfmade datatypes

    Quote Originally Posted by DarrylBurke View Post
    Moved from Advanced Java.

    Please learn to follow Java coding conventions. Variable and method names should start with a lowercase letter.

    @shall: I don't believe this is a question about operator overloading. Whether you need an add(...) method or you can write code like doubleVar1 += doubleVar2 depends on the visibility and scope of the variables themselves.

    Or maybe you believe that there isn't a += operator in Java?
    Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)

    db
    I knew there was a +=, but I wasn't sure whether or not operators could be overloaded in Java. It seems not to allow it.

  6. #6
    PascalS is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Re: Basic operations with selfmade datatypes

    Thank you for the fast help! :)

    @db: sorry for putting it in the wrong folder and also, I'll have a look at the conventions.

    so if += doesn't work, is there at least a way to write it like this:
    var1.add(var2);
    instead of this:
    var1 = Vec3.add(var1, var2);

    -Pascal-

    edit: oh wait, never mind, dammit I'm stupid..
    Last edited by PascalS; 04-16-2012 at 08:38 AM.

Similar Threads

  1. Array Operations
    By knoxb in forum New To Java
    Replies: 2
    Last Post: 02-10-2012, 07:06 AM
  2. Operations on Nodes
    By Eleeist in forum New To Java
    Replies: 7
    Last Post: 02-05-2012, 04:43 PM
  3. binary search...multiple datatypes?
    By TopNFalvors in forum New To Java
    Replies: 8
    Last Post: 03-15-2011, 08:03 PM
  4. Replies: 2
    Last Post: 11-09-2008, 10:49 PM
  5. Coercing DataTypes
    By barryc in forum New To Java
    Replies: 1
    Last Post: 07-09-2008, 05:59 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
  •