Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Java can take multiple parameters but can return only a single object?

Why was java defined such that methods may take as input multiple parameters,
but may only return a single object (or void)?

Did it make the language somehow easier to implement or use?

like image 775
BlessedKey Avatar asked Mar 11 '10 17:03

BlessedKey


3 Answers

Probably because this is the way that C and C++ do it, and the Java language syntax is very similar to (and probably based on) those languages.

In fact, according to this article, Gosling started by extending the C++ compiler, so it makes sense that he would follow much of the same syntax:

To make development a more platform-neutral process (and thus accommodate the consumer market's demand for CPU flexibility), Gosling began by extending the C++ compiler.

like image 136
Justin Ethier Avatar answered Nov 06 '22 22:11

Justin Ethier


Maybe the intention was that multiple return values be encapsulated in an object?

like image 29
Joel Avatar answered Nov 06 '22 23:11

Joel


I don't know for sure, but I imagine that Java is executed like any other stack-based runtime. Which means that passing items as parameters into a method is done easily by simply pushing them onto the stack before transferring control to the method. Return values are probably handled in the VM like C and C++ do - the return value is always placed in a register, which is by nature single-valued.

It's not a big problem, though, because with generics, returning multiple values can be handled in a type-safe way by returning an instance of something like Tuple<type1, type2, type3>, which isn't too great a burden to bear in most cases.

like image 45
Dathan Avatar answered Nov 06 '22 22:11

Dathan