Can I pass a primitive type by reference in Java?

后端 未结 9 1769
粉色の甜心
粉色の甜心 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:25

    You can't. In Java parameters are always passed by value. If the parameter is a reference type, the reference is passed by value and you can modify it inside the method while with primitive types this is not possible.

    You will need to create a wrapper type.

提交回复
热议问题