Tag Info

New answers tagged

0

The statement Fruit x = new Fruit(); creates a new object belonging to the class Fruit and x stores a reference to that object. More specifically, when the computer executes this statement, it allocates memory to hold a new object of type Fruit. It calls a constructor, which can initialize the instance variable of the object as well as perform other ...


5

You are absolutely correct. new operator reserves space in memory for a new object of Type Fruit. Assignment = assigns the reference to your variable x. If you were to write Fruit y = x;, you would only create a new reference, so both y and x would still be pointing to single location on memory. For more in-depth knowledge, check out this article.


5

If you need to ask the question, you don't need to know the answer. If you had a profiler, profiled your code, and found this to be a hotspot, you could trivially find out the answer yourself by simply changing the code and profiling again. Since you haven't done that, we can readily deduce that in fact, you have no such hotspot and no such profiler, and ...


1

tl;dr The author seems to think all objects are value objects, but this is not true. Value objects should be immutable because their existence represents a specific value out of some value space. For example, an object that represents a pixel color value could be made immutable. The property of immutability can be used to reason about the problem more ...


1

Without performance overheads, can such implementations(class LinkedList<E>) be introduced by instantiating as immutable objects? Yes, it can. In fact, a linked list is the prime example of a persistent immutable collection. (I assume by "without performance overheads" you are talking about persistence?) public class LinkedList<E> ...


8

As per this link Seeing as rule #1 is horrible, I would caution against taking the rest of the blog post as law. Though really, no blog post should be taken as law. And the quote itself is rather contradictory: A good object should never change his encapsulated state. Be aware that immutability doesn't mean that all methods always return the ...


1

I think it's easier to reason about an object that doesn't change state once initialized. For example value objects are pretty easy to reason about. Assign a value and query for properties. This is not always possible and I don't think it's a big deal, but it adds a level of complexity. Your methods might rely on a consistent internal state which is ...



Top 50 recent answers are included