Java Notes

Java Notes

General Documentation

JSP and TagLibs



Ant Tools


Build Visualization

These all use Graphviz to render graphics from generated .dot files.

Code Analyzers

Design Patterns

VM Memory Management

Questions and Topics

Q. Is Java Pass by Value or Pass by Reference? (See also:

With primitives it is simple pass by value.  
Objects references are passed by value. 

Q. Composition vs. Aggregation

Aggregation is a specialization of association, specifying a whole-part relationship between two objects. Composition is a stronger form of
aggregation where the whole and parts have coincident lifetimes, and it is very common for the whole to manage the lifecycle of its parts.

Q. What's the difference between using synchronized on a method or in a block?

The main difference is that if you use a synchronized block you may lock on an object other than "this" which is a more flexible approach.

Q. Threads

Q. What is the Java volatile keyword?

Essentially, volatile is used to indicate that a variable's value will be modified by different threads.
Declaring a volatile Java variable means:
- The value of this variable will never be cached thread-locally: all reads and writes will go straight to "main memory";
- Access to the variable acts as though it is enclosed in a synchronized block, synchronized on itself. 

Q. When would you use non-public modifiers on a class?

To make an inner class private.

Q. Set vs. List vs. Map

A Set is a collection that has no duplicate elements.
A List is a collection that has an order associated with its elements.
A Map is a way of storing key/value pairs. A Map is similar to two-column table.

Q. What are the benefits of immutable objects? (From Brian Goetz: To mutate or not to mutate?)

Immutable classes, when used properly, can greatly simplify programming. They can only be in one state, so as long as they are properly 
constructed, they can never get into an inconsistent state. You can freely share and cache references to immutable objects without having 
to copy or clone them; you can cache their fields or the results of their methods without worrying about the values becoming stale or 
inconsistent with the rest of the object's state. Immutable classes generally make the best map keys. And they are inherently thread-safe, 
so you don't have to synchronize access to them across threads.

Q. How do you make a class immutable? (See also...)

> Ensure the class cannot be overridden - make the class final, or use static factories and keep constructors private.
> Make the fields private and final.
> Force callers to construct an object completely in a single step, instead of using a no-argument constructor combined with subsequent calls to setXXX methods (that is, avoid the Java Beans convention).
> Do not provide any methods which can change the state of the object in any way - not just setXXX methods, but any method which can change state.
> If the class has any mutable object fields, then they must be defensively copied when passed between the class and its caller.

Q. Statements vs. PreparedStatements

Prepared statements make formatting a non-issue, adds some security against SQL injection, and are pre-compiled.

Q. Comparators and Sorting

Q. SAX (push), DOM, StAX (pull)

java_notes.txt · Last modified: 06/06/2022 00:34 by Daniel Kirkdorffer
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki