We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have had a few and heard of many errors and mentions to the TRIPLE_DOT character but none of them seem to explain what it means and how it interacts with code. I would greatly appreciate it if someone told me how the TRIPLE_DOT character interacts with code and what it is used for. I thought at first it was used in making default/optional arguments for functions but I think I am incorrect there... <== (hey I used a triple dot I think i get it now loljk) It would be nice to know how to make functions that if one argument is null it assigns a default value to it. I could have used that a lot to simplify my code... <==(there it is again)
Thank's in advance!
Answers
The question isn't clear...
But basically, the triple dot is used when a function accepts a variable number of arguments. The parameter is actually an array of arguments.
Sorry, but Java has no concept of default value. You have to do the test for null and assign the default value yourself.
Sorry if it wasn't clear. That is sort of what i mean by optional arguments like how you can have a PVector class and use either xy or xyz coordinates in all of the functions. Can you show me more on how to use the TRIPLE_DOT character? I have almost no idea on how to use it. I can begin to see how you use String... arguments and such but getting the args in the function is kind of blurry.
A more thorough explanation:
http://www.javabeat.net/qna/645-varargs-in-java-50/
We don't need "varargs" for that! We just need to create methods/constructors using same identifier, but w/ diff. signature:
And I believe latest print() & println() from Processing v2.1+ is using ... for arguments! <:-P
Wait, you can create tow different constructors with two different sets of arguments and instructions for the same class? MIND = BLOWN
Although Java doesn't have default parameter values; creating methods/constructors w/ diff. signatures are as close as we can get! :>
But another question? Is it even fathomable to do this with multiple types of arguments. All the compiler would have to do is the same thing with varargs but this time sort through the types and just say "that's a string put it in that array" and then say "thats an int put it in there" and run code like normal. Right?
Take a look at this post to check a custom Colour class I've made which abuses diff. signatures: ;))
http://forum.processing.org/two/discussion/1220/best-way-to-sort-through-a-6000-element-array-based-on-distance#Item_9
Some nice code there!
When we got methods/constructors w/ same identifier, Java has to determine which 1 to use! =:)
Data-types & # of arguments determine the signature of a method/constructor! :-B
Cool, so can you have a function like this:
According to the explanation link, if ... is used, it must be last argument. And we can't have more than 1 either!
Oh okay, well at least we can use it with other args so long as they precede the vararg. that would have been cool though for a lot of things i have done. My physics engine i'm currently making uses well, a lot of stuff like this and this will remove a lot of lines from the code. SOSOSO sorry that im keeping you here with question after question but i have one more. It might sound stupid but is there a way to have functions with different signatures? I have tried before but I might have missed something.
Methods & constructors are functions! Function is just a generic name, while former 1s are specific! o->
In my Colour class you're gonna find both of them! :D
Perhaps I should try to re-write Colour class constructors and its set() methods using "varargs" :-\"
Thank you so much I learned a ton about programming today! I tested it out with the following code and it worked!
Finally modified Colour to use "varargs" for both constructor & set() method: \m/
"RGB.txt":
Nice! If only i had the ability to write code like that so well. To be completely honest I'm kind of a noob when it comes to programming.
this, having same method name with different argument types / numbers, is called 'polymorphism' btw
Yes, particularly, function overloading, which is a subtype of Polymorphism.
And, println_hello_world, you probably will be able to write such code after some years of experience... :-)
Indeed, there are overloading & overriding polymorphism types. :-c
For the sake of completeness I have modified @PhiLho's example to show that you don't have to pass any values.
Thank you all for the amazing help and knowledge. This forum almost has more info than the javadoc and processing website does!