Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Basically something like

public void jumpToRoom(String roomName = Rooms.DEFAULT_ROOM)

would be convenient.

Many languages nowadays seem to support it (Ruby, PHP, even C#).

What is Java's reasoning for not allowing it?

share|improve this question
Key things - look at the timeframe that the java spec was written and the languages that could influence to have default parameters (Java class libs where developed 4 years prior to Ruby or PHP showing up). Going into the "reasoning for not allowing it?", unless one can find a document by Gosling saying one way or the other nearly any answer will be speculation. – MichaelT Jan 15 at 20:05
1  
@MichaelT Java was extended since then though, and C# successfully added default parameters atop of an even larger language. – delnan Jan 15 at 20:12
@MichaelT: Default argument values weren't invented by Ruby or PHP. FORTRAN had optional arguments, and C++ had default arguments a decade prior to Java. The latter had a very heavy influence on the design of Oak, which was intended to be a better C++. Oak eventually morphed into Java. – Blrfl Jan 15 at 20:19

closed as not constructive by ChrisF Jan 15 at 20:49

As it currently stands, this question is not a good fit for our Q&A; format. We expect answers to be supported by facts, references, or specific expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, see the FAQ for guidance.

1 Answer

One good reason is to simplify overloading. For example if void foo(String a); and void foo(String a,String b default: "bar")

both exist what does foo("bar") call?

share|improve this answer
Neither, it generates a compiler error. Possibly already at one of the two definitions. – delnan Jan 15 at 20:11
C# supports overloading and optional arguments – K2xL Jan 15 at 20:22
1  
To be more specific, C# has sensible rules for overload resolution when optional arguments are employed. Generally speaking, the most specific overload match wins. – Robert Harvey Jan 15 at 20:23
"both exist what does foo("bar") call?": You can define proper rules to resolve the ambiguity but it makes the language's semantics more complex. Java tends to favour minimalism. – Giorgio Apr 2 at 10:58
I would hope it wouldn't generate a compiler error because that may break backward compatibility. – Brandon May 10 at 12:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.