• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why ArrayList capacity is not incremented according to it's formula in source code?  RSS feed

 
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my program which I check in debug mode. I found capacity of ArrayList increasing in following manner mentioned in comments. I checked source code, where I found this formula
int newCapacity = (oldCapacity * 3)/2 + 1; in method public void ensureCapacity(int minCapacity) but why it is not incrementing according to this formula.

I found this link which says Till Java 6 int newCapacity = (oldCapacity * 3)/2 + 1; and (Update) From Java 7 int newCapacity = oldCapacity + (oldCapacity >> 1); but I'm using Java 8 still have this formula int newCapacity = (oldCapacity * 3)/2 + 1 is that website giving wrong information? or again they changed to older formula in Java 8? or I'm using older src.zip in Java 8 to get source code ? Below is code I used to check how it increases it's capacity why it is increasing in this strange manner?
 
Saloon Keeper
Posts: 4038
94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to its source code, Java 8 uses the same formula as Java 7, which would result in the sequence 10, 15, 22, 33 etc. that you observed. So it would seem that all is as it should be, no?
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That shows the hazard of trying too hard to find out the internals of a class; you should leave implementation details well alone.
Does that mean that a 1‑element List is never enlarged? I tried it and the 1‑element List does seem to be enlarged.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:According to its source code, Java 8 uses the same formula as Java 7
but on my system I have Java 8 but when clicked on myArrayListOne..ensureCapacity() method I get this am I using older src.zip ? Because I got that from my colleague. This is the information found in comment at top @author Josh Bloch
* @author Neal Gafter
* @version 1.56, 04/21/06
* @see Collection
* @see List
* @see LinkedList
* @see Vector
* @since 1.2
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the date is given as April 2006, that would appear to be the Java6 version.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Since the date is given as April 2006, that would appear to be the Java6 version.
See that's why I really need this forum. Even my senior was baffled why we are not getting correct answer. No one knew we were using older src.zip. Can you please tell me where can I get this source file of Java 8 version from?
Campbell Ritchie wrote:you should leave implementation details well alone.
yes I do understand same way I had left JVM internal architecture but later again my seniors asked me to read it so I read almost thorough artima article which way too old plus little information from JVM specification 8 for method area etc. etc. Anyway at least this new formula made me understand shift operator's working which I abandoned studying.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:So it would seem that all is as it should be, no?
yes It is using same formula as 7 i.e. int newCapacity = oldCapacity + (oldCapacity >> 1) so all answers in comments are correct. I was looking in older source code.
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote:See that's why I really need this forum.


Although if you asked me to rank this problem on a scale of importance from 1 to 10, I would give it at most a zero.
 
Ranch Foreman
Posts: 3071
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote:am I using older src.zip ? Because I got that from my colleague. This is the information found in comment at top @author Josh Bloch


Unless it's changed, but your JDK download should come with a src.zip.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:but your JDK download should come with a src.zip.
I have jdk 1.8.0_40 where can I find that src.zip I found javafx-src.zip but not src.zip ?
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Paul Clapham Agreed
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote:. . . I have jdk 1.8.0_40 where can I find that src.zip I found javafx-src.zip but not src.zip ?
You should be using JDK8u91/92 not 40. Update to 91/92. Look in your Java® installation folder for src.zip. Make sure to check its date before you unzip it.
This is what you will find:-
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Campbell Ritchie Yes installed JDK8u91, also configured src.zip. Thank you so much
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!