0

I keep seeing the following when I look up why use wrapper classes:

Objects are needed if we wish to modify the arguments passed into a method (because primitive types are passed by value).

  1. if we set the parameter to the return value we can modify the primitive
  2. everything in java is passed by value

What does the statement actually mean? Can someone provide an example?

I did a search on why use wrapper classes and came up with the following:

  1. https://www.tutorialspoint.com/why-do-we-need-a-wrapper-class-in-java

  2. https://www.geeksforgeeks.org/need-of-wrapper-classes-in-java/

  3. https://www.javatpoint.com/wrapper-class-in-java

They all say the same thing. Is it just plain wrong or are they trying to say something else?

DCR
  • 14,737
  • 12
  • 52
  • 115
  • 2
    I think it's trying to say that you can pass a mutable object into a method as a way of getting information out, but it is not saying it very well. – khelwood Oct 08 '21 at 16:56
  • 2
    This statement without context makes no sense or can mean anything. – PeterMmm Oct 08 '21 at 16:57
  • @progman why would you think that reference answers the question? – DCR Oct 08 '21 at 16:58
  • @PeterMmm, why do you think the question makes no sense? Look up why use wrapper classes and in most cases it starts off with that statement – DCR Oct 08 '21 at 16:59
  • @khelwood, thank you- that may be right although it's pretty amazing that at least three different answers to why use wrapper classes have that exact language – DCR Oct 08 '21 at 17:01
  • I mean the statement that you quote. The question probably makes sense if you give us more details or code where you found a problem and starts to look for "wrappers". – PeterMmm Oct 08 '21 at 17:02
  • 2
    Please reference the quote or add the context in which it was stated. If "wrapper" here refers to `Integer`, `Character`, etc., then it's wrong. Those are all immutable. If `arguments` refers to the variables, it's also wrong, re-assigning a parameter will never affect the argument the caller passed in (if it was evaluated from a variable). If `arguments` refers to an object referenced by a parameter, then it might be right, but, again, not for the wrapper types. – Sotirios Delimanolis Oct 08 '21 at 17:06
  • @Sotirios Delimanolis I've referenced the above quote, please vote to reopen – DCR Oct 08 '21 at 17:16
  • Now I see the quote in context, it's just wrong. It's talking about the standard wrapper classes, which are immutable. – khelwood Oct 08 '21 at 21:00

2 Answers2

1

Quoted from https://www.tutorialspoint.com/why-do-we-need-a-wrapper-class-in-java:

The objects are necessary if we wish to modify the arguments passed into the method (because primitive types are passed by value).

Quoted from https://www.geeksforgeeks.org/need-of-wrapper-classes-in-java/:

Objects are needed if we wish to modify the arguments passed into a method (because primitive types are passed by value).

Quoted from https://www.javatpoint.com/wrapper-class-in-java:

But, if we convert the primitive value in an object, it will change the original value.

They are indeed just plain wrong in the context of wrappers. The wrapper classes for the primitive types are all immutable, the actual (primitive type) value inside the wrapper cannot be changed once the wrapper object has been created (excluding reflection of course). So even if you have the following code blocks:

Integer outside = Integer.valueOf(42);
someMethod(outside);

and

public static void someMethod(Integer inside) {
}

and the variables outside and inside will reference the same object created by Integer.valueOf(), it will not help you to change the reference value of the outside variable itself or the object referenced by outside.

Progman
  • 16,827
  • 6
  • 33
  • 48
0

The caller method & the called method use the same reference variable for the parameter. The value if changed in called method will reflect in caller’s method. But in case of primitives, the caller & the called have 2 independent variables with same value. If we modify the variable in called, then it is not reflected in caller.

Avik Dey
  • 11
  • 5