About Question enthuware.ocajp.i.v7.2.1114 :
Moderator: admin
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1114 :
Correct. Heap memory is consumed only when you create any object. stack memory is consumed when you create local variables. Neither is happening here.
(If you are not clear about the difference between Heap and Stack, you might want to go through a good book. Section 1.6 of Hanumant's book explains this nicely. )
(If you are not clear about the difference between Heap and Stack, you might want to go through a good book. Section 1.6 of Hanumant's book explains this nicely. )
If you like our products and services, please help us by posting your review here.
-
- Posts: 221
- Joined: Mon Nov 26, 2018 2:43 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1114 :
Section 1.6 really brought together how JVM stores local variables and objects.
One thing was not explicitly mentioned though, where field variables are stored?
Also, since arrays are objects, they will be stored on the heap whether they are field or local arrays, correct?
One thing was not explicitly mentioned though, where field variables are stored?
Also, since arrays are objects, they will be stored on the heap whether they are field or local arrays, correct?
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1114 :
It is mentioned quite clearly in point 5 of Points to remember pg 117:
Only temporary variables i.e. variable created in a method (also known as local variables and automatic variables) are created on the stack space. Everything else is created on the heap space. If you have any doubt, ask yourself this question - is this a temporary variable created in a method? Yes? Then it is created on the stack. No? Then it is on the heap.
The same point 5 also says, "Actual objects are ALWAYS created on the heap.". So, there shouldn't be any confusion about where arrays are stored.
If you like our products and services, please help us by posting your review here.
-
- Posts: 221
- Joined: Mon Nov 26, 2018 2:43 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1114 :
So that's what 5 was about. Well now I don't have to ask that question Thanks
-
- Posts: 2
- Joined: Sat Feb 16, 2019 6:29 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1114 :
sorry...but shouldn't it print 10 in case of removing the local variable X from looper method? cause instance variable X would be assigned M, which woths 10 at the first and last iteration. why zero??
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1114 :
Why do you think m is 10 at the last iteration?
If you like our products and services, please help us by posting your review here.
-
- Posts: 14
- Joined: Fri Sep 09, 2022 5:03 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1114 :
Hello,
The answer explanation says: "If you remove int x = 0; from looper(), it will print 0 and end." Shouldn't it be var x = 0 to be consistent with the question?
The answer explanation says: "If you remove int x = 0; from looper(), it will print 0 and end." Shouldn't it be var x = 0 to be consistent with the question?
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1114 :
Yes, that would be better. Fixed.
thank you for your feedback!
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: Google [Bot] and 27 guests