Can I pass a primitive type by reference in Java?

后端 未结 9 1767
粉色の甜心
粉色の甜心 2021-02-05 22:58

I would like to call a method which could potentially take on different versions, i.e. the same method for input parameters that are of type:

  • boolean
  • byte
9条回答
  •  臣服心动
    2021-02-05 23:10

    While Java supports overloading, all parameters are passed by value, i.e. assigning a method argument is not visible to the caller.

    From your code snippet, you are trying to return a value of different types. Since return types are not part of a method's signature, you can not overload with different return types. Therefore, the usual approach is:

    int getIntValue() { ... }
    byte getByteValue() { ... }
    

    If this is actually a conversion, the standard naming is

    int toInt() { ...}
    byte toByte() { ... }
    

提交回复
热议问题