Return multiple values from a Java method: why no n-tuple objects?

前端 未结 6 1700
故里飘歌
故里飘歌 2020-11-28 08:36

Why isn\'t there a (standard, Java certified) solution, as part of the Java language itself, to return multiple values from a Java method, rather than developers having to u

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 09:11

    Because returning multiple value from a method is not a recommended practice ( in Java ).

    If you need unrelated values from a method you need a different datastructure like an object containing those values. If you need multiple instances of the same class ( ie several Strings ) you need to return either an array, or some collection depending on your needs.

    Returning multiple values in other languages ( Go for instance ) are used for instance to return an eror code, but Java was designed differently using exceptions.

提交回复
热议问题