Can I pass a primitive type by reference in Java?

2020-05-27 11:04发布

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
  • short
  • int
  • long

The way I would like to do this is by "overloading" the method (I think that is the correct term?):

public void getValue(byte theByte) {...}
public void getValue(short theShort) {...}
... etc ...

... but that would mean that I would have to pass the primitive type in by reference... similar to C++ where the method has external effect, where it can modify the variable outside its scope.

Is there a way to do this without creating new classes or using the Object versions of the primitive types? If not, any suggestions on alternative strategies?

Let me know if I should further explain to clear up any confusion.


UPDATE

What I'm actually trying to do is construct the primitive type from a set of bits. So if I'm dealing with the byte version of the method, I want to pretty much do my work to get 8 bits and return the byte (since I can't pass by reference).

The reason I'm asking this question is because the work I do with bits is very repetitive and I don't want to have the same code in different methods. So I want to find a way for my ONE method to KNOW how many bits I'm talking about... if I'm working with a byte, then 8 bits, if I'm working with a short, 16 bits, etc...

9条回答
何必那么认真
2楼-- · 2020-05-27 11:44

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?

查看更多
成全新的幸福
3楼-- · 2020-05-27 11:47

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() { ... }
查看更多
叛逆
4楼-- · 2020-05-27 11:50

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.

查看更多
地球回转人心会变
5楼-- · 2020-05-27 11:55

The object types of primitive types in Java (Double, Integer, Boolean, etc) are, if I remember correctly, immutable. This means that you cannot change the original value inside a method they are passed into.

There are two solutions to this. One is to make a wrapper type that holds the value. If all you are attempting to do is change the value or get a calculation from the value, you could have the method return the result for you. To take your examples:

public byte getValue(byte theByte) {...}
public short getValue(short theShort) {...}

And you would call them by the following:

Short s = 0;
s = foo.getValue(s);

or something similar. This allows you to mutate or change the value, and return the mutated value, which would allow something like the following:

Short s = foo.getValue(10);

Hope that helps.

查看更多
爷、活的狠高调
6楼-- · 2020-05-27 11:55

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());

  }

}
查看更多
倾城 Initia
7楼-- · 2020-05-27 11:57

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

查看更多
登录 后发表回答