Questions tagged [boxing]

Boxing is when a value type is wrapped in a reference-type wrapper for the purposes of using it when polymorphism (conversion to Object or an interface) is required.

In Java and some similar languages there is a differentiation between primitives (or value types) and reference types. The primitive types work as simple values and, when used as a parameter to methods, do not change the original operand. Reference types on the other hand can be modified when passed into methods and have associated classes that define objects of that type (whereas primitives are more "built-in"). When viewed from the perspective of language such as C or C++, instances of reference types can be thought of as pointers that are automatically de-referenced.

Boxing occurs when a primitive value type is used in place of a corresponding reference type. In Java, for example, if a value of type int is used where an Integer is expected, the compiler will automatically wrap the value in an Integer object.

588 questions
-1
votes
1 answer

Is it boxing or unboxing?

I often get confused with boxing and unboxing. I mean I understand that both of them mean casting from System.Object to a value type which inherits from it (e.g. System.Int32 or System.Double) or contrariwise. And casting from System.Object to a…
user20311258
-1
votes
1 answer

After boxing, cannot change the object value by passing the object as an argument of a function

I am boxing an integer as an object (System.Object). Then if I assign a new value to the object within the same scope, the value of the object is getting updated. But, if I try to pass the object as an argument in another function and try to assign…
Nafiz
  • 43
  • 4
-1
votes
1 answer

Why does this code give a null pointer Exception? I thought Character class could handle null being assigned?

public class Playground { public static void main(String[] args) { String s = "blah"; Character lclfs = s.contains("/") ? '/' : s.contains("\\") ? '\\' : null; } } What am I missing (using Java 1.8)?
JGFMK
  • 8,425
  • 4
  • 58
  • 92
-1
votes
1 answer

How to convert an arbitrary-dimension array of primitives into it's boxed counterpart

If I have an array such as int[][][] myArr = new int[][][] {{{1}, {1, 2, 3}}, {}}; how can I convert it it's boxed counterpart, i.e. Integer[][][] myArrBoxed = ...
Rubydesic
  • 3,386
  • 12
  • 27
-1
votes
1 answer

Is each element of a List boxed?

A boxed T can only be unboxed to T. For example, this is not working: object o1 = 5; double n3 = (double)o1; Using the Linq's Cast() method on a List throws a similar exception: List _numbers = new List() { 1, 2, 3, 4 }; // this is…
A.B.
  • 2,374
  • 3
  • 24
  • 40
-1
votes
2 answers

Cannot convert ArrayList> to ArrayList>

I have a function foo which accept a parameter ArrayList>. When I tried to call the function by passing a variable with type of ArrayList>, the compiler shows error message that says : incompatible types:…
LEE Hau Chon
  • 435
  • 3
  • 12
-1
votes
1 answer

Cache Boxed Value

Consider the following classes; Getter and Caster where Getter retrieves a value T. Caster is a wrapper for Getter that casts the retrieved value and is a Getter itself. public abstract class Getter { public abstract T Get(); } public class…
Martin
  • 131
  • 2
  • 8
-1
votes
3 answers

Why does type of string behave like value type?

I extremely get counfused about types of reference. I searched and saw that the string type is a reference type. Is not ? My problem is : I copied a string variable to another and I changed the value of first one however the second one has still…
Ahmet Furkan
  • 84
  • 11
-1
votes
1 answer

Boxing and Unboxing Java

Integer x1 = 3, x2; // boxing int y1 = x1; // unboxing x2 = x1 + (x1 / 2); Let's suppose we have the following code. Does the 3rd line need 2 unboxings for x1 or we just say its 1 unboxing for x1 and one boxing for the whole result to be assigned…
Prestyy
  • 93
  • 1
  • 8
-1
votes
2 answers

boxing through casting interfaces

If I have an interface A and inherited class B: interface A { } class B : A { } and write this: B sth = new B(); A aaa = B; B bbb = (B)aaa; will there occur any boxing? Of course, A and B are not empty.
user8011403
-1
votes
2 answers

What's the point of autoboxing primitives in Java?

What's the point of having Integer, Boolean etc and call them "...boxing" if they don't behave like boxed objects that you can pass "by ref" and do unboxing to change their value? Here's an example of "unboxing" that I actually found out isn't…
shinzou
  • 5,850
  • 10
  • 60
  • 124
-1
votes
2 answers

Make value type behave as reference

What is the best practice to be able to assign values to value type via "reference"? In my case I need this to give possibility to assign values to ints, floats etc through out the in-app console. I dont want to give a reference to an object…
Jerry Switalski
  • 2,690
  • 1
  • 18
  • 36
-1
votes
1 answer

Interfaces, structs and boxing is this code doing what I think?

I was reading through this answer: https://stackoverflow.com/a/15209464/1073672 Here is the code, copied for completeness and slightly simplified. using System; namespace Test { interface IFoo { int Foobar{get;set;} } struct…
user10607
  • 3,011
  • 4
  • 24
  • 31
-1
votes
3 answers

How the comparisons are taking place in the code below

public class Application { public static void main(String[] args) { Integer a = new Integer(10); Integer b = new Integer(10); int x = new Integer(10); int y = new Integer(10); int p = 10; int q =…
Eddy
  • 19
  • 2
-1
votes
1 answer

Object Data type in c#

I am facing a problem related to variable scope please suggest me a good solution for this problem. Error: temp dose not exist in current context. if (cause_list_type_fk == 1) { Regularcause temp = (Regularcause) obj; …
wasipeer
  • 1,015
  • 11
  • 23