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

前端 未结 6 1730
故里飘歌
故里飘歌 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:03

    There are plenty of hackish ways to accomplish this, one way would be to return an Object[], but then you've got indices to worry about, and null pointer checks, it just gets nasty. Another way is to return a String, but then you've got to parse it, and it gets nasty.

    I think the real question is why?

    Here's the rub - If I were working on a project with you, and I saw this type of behavior, I'd rewrite it so you could see how it should be handled. If you provide a code sample, I'll rewrite it to illustrate.

    Write your methods with a single responsibility, if they need to return more data than they have the ability to, you should likely either use an object, or break it into smaller methods.

提交回复
热议问题