Can I pass a primitive type by reference in Java?

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

    Primitives are not passed by references (or objects for that matter) so no you cannot.

    int i = 1;
    moo(i);
    public void moo(int bah)
    {
       bah = 3;
    }
    System.out.println(i);
    

    Prints out 1

提交回复
热议问题