You are on page 1of 3

 Memory management is the ability to assign a new value to a

reference variable, which can lead to objects no longer being


referenced and becoming orphaned.
 Java programs tend to create a large number of objects, but only
need a small number at any given time, so mechanisms are needed
to allocate and free memory when needed.
 Memory management is easier for primitive types because all
information needed for allocation is known at compile time.
 Memory management for objects is more complicated and can be
done through garbage collection.
 Java's automatic garbage collection feature frees programmers from
managing memory by keeping track of orphaned objects and
returning the memory they use to a pool of free memory.
 The policy that references cannot be modified in Java enables
efficient automatic garbage collection.
 There is debate on whether the overhead of automatic garbage
collection justifies the convenience of not having to worry about
memory management.
 Immutability refers to the property of an object where its value
never changes once constructed.
 Mutable data types, such as Counter or Accumulator, can manipulate
object values that are intended to change.
 Java's support for enforcing immutability is the final modifier, where
declaring a variable as final means it can only be assigned a value
once.
 Using the final modifier serves as documentation that the value does
not change, prevents accidental changes, and makes programs easier
to debug.
 Immutability is an important design decision that depends on the
application at hand.
 Immutable data types such as Date are easier to use and harder to
misuse than mutable data types because the scope of code that can
change their values is smaller.
 Immutable data types are easier to debug because it is easier to
guarantee that variables in client code that uses them remain in a
consistent state.
 The downside of immutability is that a new object must be created
for every value, but this expense is typically manageable due to Java
garbage collectors.
 Unfortunately, final guarantees immutability only when instance
variables are primitive types, not reference types.
 If an instance variable of a reference type has the final modifier, the
reference to an object will never change but the value of the object
itself can change.
 Design by contract is a programming model that involves expressing
preconditions, postconditions, and side effects for a data type.
 Java has two mechanisms for verifying assumptions about a program
while it is running: exceptions and assertions.
 Exceptions are used to handle unforeseen errors outside of our
control, while assertions are used to verify assumptions made within
code we develop.
 Liberal use of both exceptions and assertions is considered good
programming practice.
 The "fail fast" programming practice suggests that exceptions should
be thrown as soon as an error is discovered, rather than ignoring it
and deferring the exception.
 Assertions are boolean expressions that are expected to be true at a
certain point in the program. If the expression is false, the program
will terminate and report an error message.
 Assertions are used to gain confidence in the correctness of
programs and to document intent.
 By default, assertions are disabled and can be enabled from the
command line using the -enableassertions flag.
 Assertions are for debugging and should not be relied on for normal
operation, as they may be disabled.
 The design-by-contract model is used to express preconditions,
postconditions, and side effects of a data type, and can be tested
with assertions during development.
 Effective data-type design leads to nontrivial issues that experts are
still debating. It is important to use widely available languages and
extensive libraries, and to build abstractions that can easily transport
to other languages.
 it is important to note that while exceptions are meant to handle
unforeseen errors, it is best practice to design your code in a way
that minimizes the need for exceptions to be thrown. This can be
achieved by using proper input validation and ensuring that methods
are only called with valid arguments.
 Another important aspect of using exceptions and assertions is to
ensure that they are properly documented. This includes providing
clear and concise error messages and describing the expected
behavior in the event of an exception or assertion failure.
 It is also important to consider the performance impact of using
exceptions and assertions. Throwing and handling exceptions can be
computationally expensive and should be used with care. Assertions,
on the other hand, are typically used for debugging and are not
meant to be enabled in production environments.
 the use of exceptions and assertions can greatly improve the quality
and reliability of your code, but it is important to use them
judiciously and in accordance with best practices.

You might also like