About Question enthuware.ocajp.i.v8.2.1096 :

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

Moderator: admin

Post Reply
mcberenguer
Posts: 4
Joined: Thu Mar 02, 2017 12:00 pm
Contact:

About Question enthuware.ocajp.i.v8.2.1096 :

Post by mcberenguer »

Are cached wrapper objects the same as pooled Strings?

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

Re: About Question enthuware.ocajp.i.v8.2.1096 :

Post by admin »

Yes, they behave the same way.
If you like our products and services, please help us by posting your review here.

bari2009
Posts: 3
Joined: Sat Jul 15, 2017 1:22 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1096 :

Post by bari2009 »

Code: Select all

class TestClass{
  
    public static void main(String[] args){
        Integer i = Integer.parseInt(args[0]);
        Integer j = i; // j points to same object as i, so j==i no matter how you change i, right?
        i--;
        i++;
        System.out.println((i==j));
        
    }
}
I don't understand why (j==i) isn't always true, since the code sets j=i. Doesn't this mean that j is always pointing to the same object that i is point to? So, why does it matter how you change i? They are both still pointing to that object and therefore (j==i) is true, right?

Thank you so much for the help! Taking my test tomorrow!

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

Re: About Question enthuware.ocajp.i.v8.2.1096 :

Post by admin »

There are multiple fundamental concepts involved here -
1. The first is that when you do j = i; you make j point to the same object as the one i is pointing to. But after this assignment, if you change i, that doesn't impact j.

It's like this - let's say you have a TV (TV1) and a remote (i). You now buy another remote(j) and set this remote to point to the same TV1 as your previous remote by doing j = i. So now you have two remotes pointing to the same TV.
Now, you change your first remote(i) to point to another TV(TV2).
What happens to the second remote(j)? Nothing. The second remote will still be pointing to the same TV1 that it was set to point to.

This is how reference variables work in Java.

2. The second concept is about how Integer wrappers are different from int primitives.
Follow this trail : https://docs.oracle.com/javase/tutorial ... asses.html

3. The third concept is about auto boxing and unboxing
Follow this trail https://docs.oracle.com/javase/tutorial ... oxing.html

4. The fourth concept is about how Integer wrapper objects from -128 to 127 are cached.
Read this: https://stackoverflow.com/questions/208 ... 128-to-127

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

bari2009
Posts: 3
Joined: Sat Jul 15, 2017 1:22 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1096 :

Post by bari2009 »

Thank you, Paul. Your reply is very helpful and I appreciate your time and effort!

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1096 :

Post by crazymind »

Code: Select all

For what command line arguments will the following program print true?

class TestClass{
  
    public static void main(String[] args){
        Integer i = Integer.parseInt(args[0]);
        Integer j = i;
        i--;
        i++;
        System.out.println((i==j));
        
    }
}

Does j changes as well when i--; or i++; get executes? j should also changes if it works like a string pool, right?

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

Re: About Question enthuware.ocajp.i.v8.2.1096 :

Post by admin »

crazymind wrote:
Thu Jan 10, 2019 5:37 pm
Does j changes as well when i--; or i++; get executes? j should also changes if it works like a string pool, right?
No, because i-- is basically same as i = i - 1 and a new Integer object containing the value of i-1 is assigned to i.
If you like our products and services, please help us by posting your review here.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1096 :

Post by crazymind »

admin wrote:
Thu Jan 10, 2019 11:39 pm
crazymind wrote:
Thu Jan 10, 2019 5:37 pm
Does j changes as well when i--; or i++; get executes? j should also changes if it works like a string pool, right?
No, because i-- is basically same as i = i - 1 and a new Integer object containing the value of i-1 is assigned to i.
Thanks. I mean Integer j = i; (they point to same object) then if you do i++, I think j should change as well.

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

Re: About Question enthuware.ocajp.i.v8.2.1096 :

Post by admin »

crazymind wrote:
Fri Jan 11, 2019 12:28 am
admin wrote:
Thu Jan 10, 2019 11:39 pm
crazymind wrote:
Thu Jan 10, 2019 5:37 pm
Does j changes as well when i--; or i++; get executes? j should also changes if it works like a string pool, right?
No, because i-- is basically same as i = i - 1 and a new Integer object containing the value of i-1 is assigned to i.
Thanks. I mean Integer j = i; (they point to same object) then if you do i++, I think j should change as well.
Your question tells me that you are not clear on the fundamentals of object and reference. i and j are references. when you do i++, i starts pointing to another object. Changing one reference to point to a different object doesn't change other reference.

Please go through this topic from a book before attempting mock exams.
If you like our products and services, please help us by posting your review here.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1096 :

Post by crazymind »

admin wrote:
Fri Jan 11, 2019 12:35 am
crazymind wrote:
Fri Jan 11, 2019 12:28 am
admin wrote:
Thu Jan 10, 2019 11:39 pm

No, because i-- is basically same as i = i - 1 and a new Integer object containing the value of i-1 is assigned to i.
Thanks. I mean Integer j = i; (they point to same object) then if you do i++, I think j should change as well.
Your question tells me that you are not clear on the fundamentals of object and reference. i and j are references. when you do i++, i starts pointing to another object. Changing one reference to point to a different object doesn't change other reference.

Please go through this topic from a book before attempting mock exams.
Sorry, did not realize j is a reference, no new appears there (no object create for j). Thanks.

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

Re: About Question enthuware.ocajp.i.v8.2.1096 :

Post by admin »

Presence of "new" doesn't determine whether something is a reference or not. The type declaration does. The type declaration of i and j is Integer (and not int), that means i and j are references of type Integer and not primitive ints. Again, this is very basic stuff. Our suggestion to you is to go through a book carefully before attempting mock exams.

We sent you an email on your emailid that you used to register on this forum. Please check it. No reply is needed.
If you like our products and services, please help us by posting your review here.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1096 :

Post by crazymind »

admin wrote:
Fri Jan 11, 2019 12:35 am
crazymind wrote:
Fri Jan 11, 2019 12:28 am
admin wrote:
Thu Jan 10, 2019 11:39 pm

No, because i-- is basically same as i = i - 1 and a new Integer object containing the value of i-1 is assigned to i.
Thanks. I mean Integer j = i; (they point to same object) then if you do i++, I think j should change as well.
Your question tells me that you are not clear on the fundamentals of object and reference. i and j are references. when you do i++, i starts pointing to another object. Changing one reference to point to a different object doesn't change other reference.

Please go through this topic from a book before attempting mock exams.
Hi, I rethink again. j point to the original object that i. points to. i. points to new object after increment since i = Integer.valueOf( i.intValue()  + 1);
Thanks!

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1096 :

Post by flex567 »

I am not sure if this is the correct thinking process for this program:

Code: Select all

    public static void main(String[] args){ //for argument '0'

        Integer i = Integer.parseInt(args[0]); // new object with value of 0 is created and cached 
        Integer j = i; // the reference of new object is assigned to j
        i--; // a new object is created of value -1 and cached
        i++; // value is 0, the cached object of that value is found(from line 1). Existing(cached) object is assigned to i.
        System.out.println((i==j)); //returns true
        
    }

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

Re: About Question enthuware.ocajp.i.v8.2.1096 :

Post by admin »

flex567 wrote:
Sat Feb 02, 2019 3:03 pm
I am not sure if this is the correct thinking process for this program:
Your understanding is correct. :thumbup:
If you like our products and services, please help us by posting your review here.

Tester
Posts: 34
Joined: Mon Oct 30, 2023 11:55 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1096 :

Post by Tester »

Are you sure that these kind of questions may be on the exam?
The constructor Integer(int) has been deprecated since version 9 and marked for removal

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

Re: About Question enthuware.ocajp.i.v8.2.1096 :

Post by admin »

Although this question does not use the Integer constructor you are referring to, you do need to know about them.
If you like our products and services, please help us by posting your review here.

Badem48
Posts: 26
Joined: Thu Aug 24, 2023 4:33 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1096 :

Post by Badem48 »

Hi,

I wanted to mention that this is my first time encountering this concept. It's surprising to me that new things still come up even after years of working with Java.

Are you certain this is included in the exam objectives? I couldn't find it in Scott Selikoff & Jeanne Boyarsky's book or Hanumant Deshmukh's book; maybe I might have missed it?

Additionally, the reason why -256 and 256 return false is not very clear. It could be mentioned separately that this is due to the Integer caching range being between -128 and 127.

Thanks,

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

Re: About Question enthuware.ocajp.i.v8.2.1096 :

Post by admin »

Caching of integral wrapper objects between -128 to 127 is quite fundamental to the wrappers. I am not sure about Boyarsky's book but Deshmukh's book for 1z0-815 mentions this in section 3.4.2 on page 64 (although this book is not suitable for 1z0-819 anymore because it was written for 1z0-815 exam). His 1z0-808 book also mentions it in the section titled "Creating wrapper objects". I don't have that book right now so I am not able to quote the section and page number.

Will add explanation for the options you mentioned to make it clear.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 35 guests