0

If we use

Boolean boolean = new Boolean(true);

and

Integer integer = new Integer(10);

then how many objects are created and where do they reside?

  • 2
    2 Objects get created they reside in the addresses pointed to by `boolean` and `integer` – MrCodingB May 16 '21 at 08:19
  • 1
    *Trick question:* **1 and 0.** The first line creates 1 object, the second line doesn't run, because it doesn't compile, so it cannot create anything. *Objects* are always created on the heap. – Andreas May 16 '21 at 08:21
  • Sorry @Andreas. I'll edit the question. – Astha Srivastava May 16 '21 at 08:23
  • So you mean there is not difference between using a wrapper class to create a primitive datatype and directly assigning value? – Astha Srivastava May 16 '21 at 08:25
  • Like new Integer(20) is same as int x = 20? – Astha Srivastava May 16 '21 at 08:26
  • There is a difference: see the link in my answer – MrCodingB May 16 '21 at 08:29
  • Of course there is a difference. `Integer integer = new Integer(10)` create a new object instances on the heap and stores the reference to it in the `integer` *reference* variable. `int x = 20` doesn't create any object instances but simply stores the value 20 in the `x` *primitive* variable. – Andreas May 16 '21 at 08:29
  • Also note that there is a cache for `Integer` in the range of -128 to 127 and for `Boolean`. Therefore, auto-boxing and `valueOf(...)` won't necessarily create new object instances but just refer to already existing ones. – Matt May 16 '21 at 08:48
  • @Matt - However, the OP is not using auto-boxing. When an instance is created using `new`, you will always get a new one. At least, that is true for all versions of Java so far released. – Stephen C May 16 '21 at 09:19
  • @StephenC - Yes, you will always get a new instance by using the constructor and the OP is not using auto-boxing in the question. But OP dives deeper in the comments and generally asked about wrapper-classes and assignment. Additionally, @Andreas mentioned the primitive case (`int x = 20`). In this context, IMHO, my comment adds another important aspect to the discussion since `Integer x = 20` behaves differently than `Integer x = 2000`. – Matt May 16 '21 at 09:28

3 Answers3

1

In your code you would end up with 1 instance of Integer which is referred to by integer and 1 instance of Boolean which is referred to by boolean. The instances will be created on the heap.

See: int vs Integer

MrCodingB
  • 2,284
  • 1
  • 9
  • 22
  • Object instances don't have names, and don't reside on the stack, they reside on the heap. The *reference value* that is referring to the instance has a name and resides on the stack or heap, depending on where that code is located. – Andreas May 16 '21 at 08:25
  • Reformulated my answer a bit – MrCodingB May 16 '21 at 08:28
1

How many objects are created and where do they reside?

  • Two objects created. Each new will create a new object.

  • All Java objects reside in the heap ... so they reside in the heap.

In the other hand, if you use int instead of Integer and boolean instead of Boolean then:

  • No objects are created.

  • The values true and 10 will be in the variables ... and will therefore reside wherever the variables reside. That will depend on what kind of variables they are.

Finally, consider this version:

Boolean boolean = true
Integer integer = 10;

The difference between this and your original version is that two values are produced by auto-boxing. This will EITHER create new objects OR reuse objects that have been created before, and have been cached. (It depends on the type, the JVM implementation, and the in some cases the cache size configured by JVM options.)

So the answers will be:

  • Zero, one or two objects will be created, depending on the above factors.
  • The objects will be in the heap (as before).

Note: auto-boxing obtains the objects by calling the wrapper classes valueOf method. You can call it explicitly in your own code as an alternative to autoboxing. It is better to do this than to use new.

Integer i1 = new Integer(10);      // bad - generates an unnecessary object
Integer i2 = 10;                   // good - auto-boxing uses (may use) cached object
Integer i3 = Integer.valueOf(10);  // equivalent to auto-boxing
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

2 Objects will be created .. one boolean object and one integer object residing in the heap memory.

Bhanu
  • 21
  • 5