• [Java] adding basic operators to a class
    23 replies, posted
If i have a class and i want to say, subtract it from another of it's type, how would i add a function that told the class what to do when the subtract symbol was used? Thanks :)
Java does not support operator overloading.
Not gonna happen in Java.
To elaborate on these answers: the behaviour you describe is known as operator overloading, and isn't something Java supports (or will ever support, in all probability). If you want a similar language to Java that supports this feature I (and basically everyone else) recommends C#, which takes the basic idea of Java, and makes a very nice programming language out of it. Other than this, your best bet is methods with names like add, subtract, multiply etc.
This might sound silly, but I don't like the idea of C# being developed, maintained and copyrighted closed source and owned by Microsoft.
It does, because it's not true.
Oh, i see. Thanks. So you all recomend just fucking java and starting w/ C#?
If you have the option then I personally would recommend that.
I actually prefer python to C#, but C# is more widely used and supported. It is a nicer language to work with than Java, certainly.
I use C#, prefer it over Java in every way possible. If you're stuck with java though, perhaps instead of: result = obj1 - obj2; Create a Remove method on the obj1/obj2 class: result = obj1.Remove(obj2); Instead of using operator overloading.
Starts getting really messy when you're subtracting more than two objects though.
[QUOTE=gilly_54;22599178]Starts getting really messy when you're subtracting more than two objects though.[/QUOTE] Why? result = obj1.Remove(obj2).Remove(obj3);
Gets even messier when you're doing stuff with BigNum or something where you use lots of operators. [cpp] var x = new BigNum(3); var y = new BigNum(4); // without operator overloads var hypotenuse = x.Times(x).Add(y.Times(y)).SquareRoot(); // with operator overloads var hypotenuse = Math.Sqrt(x*x + y*y); [/cpp]
Right, since im not doing this for a job, just because i want to learn a useful language (lua doesn't count) ill go see what i can do with C#. Anyone know a good tutorial/project site for it? Also, is the software IDE and crap free? [editline]12:40PM[/editline] With C# do i have to use forms? I'd really rather not, can i do like in java just do like an applet, where it doesnt have the terrible ugly huge top bar and a terrible background and shit like that? Thanks :P
... The fuck? You can use silverlight to make browser embeddables, and there are options for changing the window decorations, but that doesn't really make that much sesne to me...
[QUOTE=TheBoff;22639616]silverlight[/QUOTE] Uh oh.
If you want to embed it in a webpage use java or flash depending on your needs.
Use console. For everything. But no, you obviously do not have to use a form. In fact, you don't have to use a console app either. And of course, you don't need to have the title bar or anything for a form in the first place.
[QUOTE=bobthe2lol;22632949]Right, since im not doing this for a job, just because i want to learn a useful language (lua doesn't count) ill go see what i can do with C#. Anyone know a good tutorial/project site for it? Also, is the software IDE and crap free? [editline]12:40PM[/editline] With C# do i have to use forms? I'd really rather not, can i do like in java just do like an applet, where it doesnt have the terrible ugly huge top bar and a terrible background and shit like that? Thanks :P[/QUOTE] There's Visual Studio Express (the free version of Visual Studio).
[QUOTE=bobthe2lol;22632949]With C# do i have to use forms? I'd really rather not, can i do like in java just do like an applet, where it doesnt have the terrible ugly huge top bar and a terrible background and shit like that?[/QUOTE] By "terrible ugly huge top bar", do you mean the window's titlebar? Windows Forms applications are hardly unique in having titlebars. They look pretty much like any other application, in fact -- there's no special background or anything like that. Maybe you're thinking of some particular application you've seen that has a poor UI? Java applets aren't standalone applications; they live inside a browser. As others have mentioned, you can use Silverlight to make .NET "applets" that live inside a browser, but unless the program is actually part of a web page, that's probably not what you want.
[QUOTE=Wyzard;22674558]By "terrible ugly huge top bar", do you mean the window's titlebar? Windows Forms applications are hardly unique in having titlebars. They look pretty much like any other application, in fact -- there's no special background or anything like that. Maybe you're thinking of some particular application you've seen that has a poor UI? Java applets aren't standalone applications; they live inside a browser. As others have mentioned, you can use Silverlight to make .NET "applets" that live inside a browser, but unless the program is actually part of a web page, that's probably not what you want.[/QUOTE] And even if the programme is part of a web page, Silverlight is probably not what you want. In that case, use something like Javascript with ASP.NET.
[QUOTE=arienh4;22675328]In that case, use something like Javascript with ASP.NET.[/QUOTE] Unless you want to do something that Javascript doesn't let you
[QUOTE=turb_;22676442]Unless you want to do something that Javascript doesn't let you[/QUOTE] Hence I said with ASP.NET. There's not much the combination won't let you do.
[QUOTE=arienh4;22680542]Hence I said with ASP.NET. There's not much the combination won't let you do.[/QUOTE] I was referring to Silverlight when I was talking about stuff Javascript doesn't let you do.
Sorry, you need to Log In to post a reply to this thread.