Can I pass a primitive type by reference in Java?

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

  • boolean
  • byte
  • short
  • INT
  • a long

The way I would like to do this is to “overload” the method (I think this is the right 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 by reference ... similar to C ++, where the method has an external effect, where it can modify a variable outside its scope.

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

Let me know if I will explain in order to eliminate any confusion.


UPDATE

What I'm actually trying to do is build a primitive type from a set of bits. So, if I am dealing with a byte version of the method, I want to pretty much do my job to get 8 bits and return the byte (since I cannot follow the link).

The reason I ask this question is because the work done 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 say ... if I work with bytes, and then 8 bits if I work with short 16 bits, etc ...

+7
java pass-by-reference primitive-types method-overloading
source share
9 answers

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

From a piece of code you are trying to return a value of different types. Since return types are not part of the method signature, you cannot overload different types of return data. Therefore, the usual approach:

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

If this is actually a conversion, the standard naming convention is

 int toInt() { ...} byte toByte() { ... } 
+8
source share

Java is always passed by value . There is no end-to-end link in Java. It is written in the specifications !

+8
source share

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

You will need to create a shell type.

+5
source share

Primitives are not passed by reference (or objects in this regard), so you cannot.

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

Prints 1

+1
source share

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

There are two solutions. One of them is to create a shell type that contains a value. If all you are trying to do is change the value or get a calculation from the value, you could return a method for the result. To give your examples:

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

And you would call them the following:

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

or something similar. This allows you to mutate or change the value and return the changed value, which allows you to do something like the following:

 Short s = foo.getValue(10); 

Hope this helps.

+1
source share

I would say that an alternative strategy, if you want to work with primitives, is to do what the Java libraries do. Just suck it and use several methods.

For example, ObjectInputStream has readDouble() , readByte() , etc.

You do not type anything by sharing the implementation of the function, and the clients of your function do not receive anything according to the variants of your function that have the same name.

UPDATE

Given your update, I don't find it necessary to duplicate too much code. It depends on your coding strategy, but I would suggest that you could do something like this:

 private byte get8Bits(); public byte getByte() { return get8Bits(); } public int getInt() { return (get8Bits() << 24) | (get8Bits() << 16) | (get8Bits() << 8) | get8Bits(); } 

Anything that divides the code more than this is probably too complicated.

An alternative could be

 private long getBits(int numBits); public byte getByte() { return (byte)getBits(8); } public int getInt() { return (int)getBits(32); } 

i.e. I don’t think it makes sense to expose your library users to anything other than the primitive types themselves.

If you really wanted to, then you could do one way to access as follows:

 @SuppressWarnings("unchecked") public static <T> T getValue(Class<T> clazz) { if ( clazz == byte.class ) { return (T)Byte.valueOf((byte)getBits(8)); } else if ( clazz == int.class ) { return (T)Integer.valueOf((int)getBits(32)); } throw new UnsupportedOperationException(clazz.toString()); } //... byte b = getValue(byte.class); int i = getValue(int.class); 

But I do not see how this is less cumbersome for the clients of your library.

+1
source share

It looks like you have a set of bits that you parse. You need it to be wrapped in an object, allows you to call this object a BitSet. You repeat the bit, so you will have something like Iterator <bit>, and along the way you want to parse bytes, ints, longs, etc. Correctly?

Then you will have your own Parser class, and there are methods on it:

 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 choose any method for you, you retrieved the value you want using the typeafe type (different return types for different methods), and Iterator was moved forward by the correct number of positions depending on type.

Is this what you are looking for?

+1
source share

Only by creating your own value storage types.

0
source share

Yes, please specify what you want to achieve. From your description, I suggest you take a look at Java generators, 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
source share

All Articles