Can I pass a primitive type by reference in Java?

后端 未结 9 1744
粉色の甜心
粉色の甜心 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() { ... }
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-02-05 23:16

    Sounds like you have a set of bits that you're parsing through. You should have it wrapped in an object, lets call that object a BitSet. You're iterating through the bits, so you'll have something like an Iterator<Bit>, and as you go you want to parse out bytes, ints, longs, etc... Right?

    Then you'll have your class Parser, and it has methods on it like:

    public byte readByte(Iterator<Bit> bitit) {
      //reads 8 bits, which moves the iterator forward 8 places, creates the byte, and returns it
    }
    public int readInt(Iterator<Bit> bitit) {
      //reads 32 bits, which moves the iterator forward 32 places, creates the int, and returns it
    }
    

    etc...

    So after you call whichever method you need, you've extracted the value you want in a typesafe way (different return types for different methods), and the Iterator has been moved forward the correct number of positions, based on the type.

    Is that what you're looking for?

    0 讨论(0)
  • 2021-02-05 23:19

    Java is always pass-by-value. There is no pass-by-reference in Java. It's written in the specs!

    0 讨论(0)
  • 2021-02-05 23:24

    Yes, please be more specific about what you want to achieve. From your description I suggest you have a look at Java generics where you could write something like this:

    class SomeClass <GenericType> {
      GenericType val;  
    
      void setValue(GenericType val) {
         this.val = val;
      }
    
      GenericType getValue() {
         return val;
      }
    
      public static void main(String[] args) {
        SomeClass<Integer> myObj = new SomeClass<Integer>();
        myObj.setValue(5);
        System.out.println(myObj.getValue());
    
        SomeClass<String> myObj2 = new SomeClass<String>();
        myObj2.setValue("hello?!");
        System.out.println(myObj2.getValue());
    
      }
    
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题