[HD Pg 0, Sec. 3.4.2 - creating-wrapper-objects]

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
amannur
Posts: 3
Joined: Sat Dec 21, 2019 1:10 pm
Contact:

[HD Pg 0, Sec. 3.4.2 - creating-wrapper-objects]

Post by amannur »

Let's say :

Line 1 : int i = 100;
Line 2 : Integer i1 = i;
Line 3 : Integer i2 = 100;

On line 2, will the object be created in heap, with value 100? and this both i1 and i2 will point to the same object?

Thanks
Aniruddha

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 0, Sec. 3.4.2 - creating-wrapper-objects]

Post by admin »

1. Objects are always created on heap.
2. Reference variable can only point to objects (not to primitives). (Go through Object and References section from the book carefully. Both these points are explained there in detail.)

Since i1 is a reference variable, it can only point to an object (or be null). Therefore, an Integer object containing 100 will be created due to autoboxing on the heap at line 2.
3. Due to caching of Integer objects, i1 and i2 will point to the same object.

amannur
Posts: 3
Joined: Sat Dec 21, 2019 1:10 pm
Contact:

Re: [HD Pg 0, Sec. 3.4.2 - creating-wrapper-objects]

Post by amannur »

Thank you. It makes sense.

Post Reply

Who is online

Users browsing this forum: No registered users and 21 guests