About Question enthuware.ocajp.i.v8.2.1096 :
Moderator: admin
-
- Posts: 4
- Joined: Thu Mar 02, 2017 12:00 pm
- Contact:
About Question enthuware.ocajp.i.v8.2.1096 :
Are cached wrapper objects the same as pooled Strings?
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1096 :
Yes, they behave the same way.
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Sat Jul 15, 2017 1:22 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1096 :
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));
}
}
Thank you so much for the help! Taking my test tomorrow!
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1096 :
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.
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.
-
- Posts: 3
- Joined: Sat Jul 15, 2017 1:22 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1096 :
Thank you, Paul. Your reply is very helpful and I appreciate your time and effort!
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1096 :
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?
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1096 :
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.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1096 :
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1096 :
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.crazymind wrote: ↑Fri Jan 11, 2019 12:28 amThanks. I mean Integer j = i; (they point to same object) then if you do i++, I think j should change as well.
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.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1096 :
Sorry, did not realize j is a reference, no new appears there (no object create for j). Thanks.admin wrote: ↑Fri Jan 11, 2019 12:35 amYour 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.
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1096 :
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.
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.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1096 :
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);admin wrote: ↑Fri Jan 11, 2019 12:35 amYour 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.
Thanks!
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1096 :
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
}
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1096 :
Your understanding is correct.
If you like our products and services, please help us by posting your review here.
-
- Posts: 34
- Joined: Mon Oct 30, 2023 11:55 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1096 :
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
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1096 :
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.
-
- Posts: 27
- Joined: Thu Aug 24, 2023 4:33 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1096 :
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,
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,
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1096 :
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!
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.
Who is online
Users browsing this forum: No registered users and 31 guests