Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (521)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (589)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Pass optional parameters to Methods?  (Read 1909 times)
0 Members and 1 Guest are viewing this topic.
Offline elias4444

Junior Devvie





« Posted 2004-03-02 20:05:30 »

I know there's got to be an easy way to do this, but I can't find it for the life of me.

When I build a new method, I want to be able to make it so that the first parameter is always required, but the second is optional. i.e.:

public void calculate(int firstnum, Object objpass) {
   ....do something with firstnum....
   .....do something with objpass as pertaining to firstnum calculation if objpass exists....
}

Any way to do this? If I specify the the second parameter in the method parameter list, it requires it when called. Help?

Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #1 - Posted 2004-03-02 20:09:10 »

Just write a second calculate that only accepts an int, and have it delagate to your actual calculate method to do the actual heavy lifting (with the default arg). Slightly more bulky code, but as far as I'm aware the only way to get C++ style default args.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline elias4444

Junior Devvie





« Reply #2 - Posted 2004-03-02 20:11:45 »

Murphy's Law strikes again....right after I posted, I found my answer! So, if anything, thank you all for being subjected to a truly Murphy-ized Newbie.  Tongue

For those wondering...when calling the method, you just have to specify somemethod(integerofsomesort, null);

Thank you for those who were so speedy to reply (I've already gotten a reply while composing this message). You folks are fantastic!

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #3 - Posted 2004-03-02 20:23:03 »

...or wait until 1.5 goes gold (it supports method signatures with variable numbers of parameters).

malloc will be first against the wall when the revolution comes...
Offline elias4444

Junior Devvie





« Reply #4 - Posted 2004-03-02 20:27:37 »

Quote
...or wait until 1.5 goes gold (it supports method signatures with variable numbers of parameters).


How will THAT work?

Offline rreyelts

Junior Devvie




There is nothing Nu under the sun


« Reply #5 - Posted 2004-03-03 00:48:15 »

Quote

How will THAT work?

The term they're using for that feature is "varargs". You can look up the spec for it at jcp.org. It works by letting you annotate a method as accepting a variable number of arguments. The compiler automatically boxes arguments to any method that is tagged with a VARARGS access modifier.

Personally, I preferred concise object array literals, but it's looking too late for that change to take place.

God bless,
-Toby Reyelts

About me: http://jroller.com/page/rreyelts
Jace - Easier JNI: http://jace.reyelts.com/jace
Retroweaver - Compile on JDK1.5, and deploy on 1.4: http://retroweaver.sf.net.
Offline sma

Junior Devvie





« Reply #6 - Posted 2004-03-08 06:33:45 »

Quote


How will THAT work?


You can define a method method(type... name) which is equivalent to method(type[] name) and then call it with method(a, b, c) instead of using method(new type[]{a, b, c}). Because Java 1.5 will automatically convert primitive data into instances of Wrapper classes, stuff looks much nicer, for example
1  
format("%1 = %2", "3+4", 3+4);
instead of
1  
format("%1 = %2", new Object[]{"3+4", new Integer(3+4)});

.: Truth Until Paradox!
Offline z.e.r.o

Junior Devvie




Java games rock!


« Reply #7 - Posted 2004-03-09 03:49:31 »

Shocked

That is neat

Matteo Anelli
.brain - http://www.dot-brain.com
Offline Abuse

JGO Knight


Medals: 14


falling into the abyss of reality


« Reply #8 - Posted 2004-03-10 05:25:01 »

More feature rich = less noob friendly

The explicitness (is that a word???) of Java was, IMHO 1 of its benefits.
All the added syntactic sugar is only going to further confuse newbies to the language.

Think about how many Java noobies actually understand what is happening behind the scenes with string concatenation,
And how often the question, 'my String concatenation is realy slow!' is asked...

Variable argument lists  & especially Autoboxing is going to multiply that problem 100fold.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xFryIx (58 views)
2014-11-13 12:34:49

digdugdiggy (37 views)
2014-11-12 21:11:50

digdugdiggy (30 views)
2014-11-12 21:10:15

digdugdiggy (26 views)
2014-11-12 21:09:33

kovacsa (48 views)
2014-11-07 19:57:14

TehJavaDev (51 views)
2014-11-03 22:04:50

BurntPizza (51 views)
2014-11-03 18:54:52

moogie (66 views)
2014-11-03 06:22:04

CopyableCougar4 (65 views)
2014-11-01 23:36:41

DarkCart (150 views)
2014-11-01 14:51:03
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!