r/programming • • Feb 22 '15

Java Makes Programmers Want To Do Absolutely Anything Else With Their Time

http://www.forbes.com/sites/quora/2015/02/21/java-makes-programmers-want-to-do-absolutely-anything-else-with-their-time/
35 Upvotes

636 comments sorted by

View all comments

Show parent comments

2

u/[deleted] Feb 22 '15

[removed] — view removed comment

1

u/nqd26 Feb 22 '15

Are you talking about "growability" of collections? If so, it can be solved by preallocating the necessary number of elements (if known), e.g. new ArrayList(1000) or even new ArrayList(otherColl.size())

2

u/[deleted] Feb 22 '15

[removed] — view removed comment

0

u/nqd26 Feb 22 '15

It can't restructure an array so that it can hold the elements directly instead of just pointers to the objects.

I'm not sure why you'd want it to.

public void something() {
	List list = new ArrayList();
	list.add(new Member());
	
	// do something with the objects
}

In this example both collection and member can be put on the stack.

public Member something() {
	Member member = new Member();
	
	List list = new ArrayList();
	list.add(member);
	
	// do something with the objects
	
	return member;
}

In this example member obviously escapes so needs to be on the heap. But list can be on the stack. Everything seems to be fine.

Inheritance is the problem here, as you don't know ahead of time how large each object is going to be.

Can you elaborate on this? When JVM start creating an object it already has all the classes from which the instantiated class is inheriting, so JVM also knows the size of the object (it must know so that it can allocate proper amount of memory).

1

u/[deleted] Feb 22 '15

[removed] — view removed comment

1

u/nqd26 Feb 22 '15

That's a separate problem though. EA and stack vs. heap is about cost of allocation and deallocation (GC) of memory, not about extra cost of chasing pointers and CPU cache ...