c++dtor^py finalizer^java finalizer

This blog has enough posts on c# finalizers. See also AutoCloseable^Closeable #java

python finalizer is a special class method object.__del__(self), invoked when reference count drops to zero, and garbage-collected. As such, it’s not useful for resource management, which is better done with context manager, a popular python idiom and the best-known “protocol” in python.

— Java finalizer is an important QQ topic

https://stackoverflow.com/questions/2506488/when-is-the-finalize-method-called-in-java has some highly voted summaries.

The finalize() method can be at any time after it has become eligible for garbage collection, possibly never.

The finalize() method should only be written for cleanup of (usually non-Java) resources like closing files. [[effJava]] says avoid it.

https://www.infoq.com/articles/Finalize-Exiting-Java compares

  1. c++RAII
  2. java finalize()
  3. java 7 try-with-resources

##java8 features

  • method references? Supposed to be an important feature in the core language/syntax. related to lambda syntax, presumably a syntactic sugar coating.
  • java.util.Optional<T> introduced to reduce NPE
  • CompletableFuture<T> to promote async, event-driven programming
  • PermGen disappears

Cloneable, Object.clone(), Pen.Clone() #java

A few learning points.

The Object.clone() implementation is not that important, because I should always override it in my class like Pen, but here are some observations about this Object.clone():

  • shallow copy, not deep copy, not very useful.
  • this protected method is mostly meant to be invoked by a subclass:
    • If your variable points to some Pen object that’s no Clonable, and you call it from either the same package or a subclass, then you hit CloneNoSupported.
    • if your Pen class implements Clonable, then it should override the clone() method
    • [1] The only way the default Object.clone() gets picked by compiler is when a Cat class implements Clonable but doesn’t override clone(), and you call it from either the same package or a subclass

Clonable is a special marker interface. It could trigger the CloneNotSupported exception, but if you override clone() then this exception may not hit. It’s an obscure detail.

  • I think you can override clone() without implementing Clonable, but this is tricky and non-standard.
  • You could also implement Clonable without overriding clone() .. see [1]

java access levels: protected^package-private..

https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html (java8) shows two nice tables:

  • There’s no more “private protected
  • default access level is better known as “package-private” — strictly more private more restrictive than Protected . (Protected is more like Public). The 2nd table shows that
    • a “package-private” member of Alpha is accessible by Beta (same package) only, whereas
    • a “protected” member of Alpha is accessible by Beta and Alphasub
    • Therefore, “neighbors are more trusted than children”

I find it hard to remember so here are some sound bytes

  1. “protected” keyword only increases visibility never decreases it.
  2. So a protected field is more accessible than default (package-private)
    • As an example, without “protected” label on my field1, my subclasses outside the package cannot see field1.
  3. same-package neighbors are trusted more than children outside the package

For a “protected” field1, a non-subclass in the same package can see it just as it can see a default-accessible field2

Not mentioned in the article, but when we say “class Beta can access a member x of Alpha”, it means that the compiler allows you to write, inside Beta methods, code that mentions x. It could be myAlpha.x or it could be Alpha.x for a static member.

AutoCloseable^Closeable]java #^other lang

See also c++dtor^py finalizer^java finalizer^AutoCloseable

The newer (java 7?) AutoCloseable interface is used in try-with-resource. The older Closeable interface isn’t so relevant here.

  1. c# has a similar feature — the “using” keyword + IDisposable interface
  2. Python has a similar feature. The “with” keyword requires a context manager (a protocol) …
  3. c++ doesn’t offer a comparable feature. RAII is a more time-honored technique.

I believe all of them are exception-safe.

RMI could send more than simple VO

[[java the good parts]], written by a RMI authority, is a thin book with a concise but in-depth chapter on RMI. One of the things pointed out was —

In a distributed system like RPC or RMI, sometimes the object sent over network are more than monolithic POJO “value objects” with trivial methods. For example, you could send a special FXDeal object having specialized virtual reval() method. Interface would declare a BaseDeal data type for it. Obviously, receiving end should NOT “slice” this FXDeal object down to BaseDeal and lose the specialized reval().

Not sure how CORBA handles it, but RMI was designed to support it. Basically the serialization stream of the object includes the location to download the FXDeal class bytecode.

## java 7 features – my picks

(Java 8 and java 5 are major changes compared to java 7)

#1) [advanced] fork and join
#2) [advanced] File change notifications
#3) new filesystem io API
#4) try( FileOutputStream fos = newFileOutputStream(“movies.txt”); DataOutputStream dos = newDataOutputStream(fos) ) {
dos.writeUTF(“Java 7 Block Buster”);
} catch(IOException e) {
// log the exception

# catch(ExceptionOne | ExceptionTwo | ExceptionThree e) {
# diamond operator. List> my = new List<> ();

[[practical api design]] java

Written by the founder and initial architect of NetBeans, this book has a java root. Freely available in pdf, I find this ebook rather detailed, not a lot of high-level (vague) concepts like most design/architecture books including the classics. It seems to deal with some real world coding and design decisions. I would say these are not life-and-death decisions but still everyday decisions. (In contrast those other books address decisions I don’t encounter or care at all — seem to belong to another level.) Therefore this book is closer to my life as a developer.

There’s a chapter against “extreme” (unhealthy) advice. Unconventional critique:
** an api must be beautiful
** an api must be correct
** an api must be simple
** an api must have good performance
** an api must be 100% compatible
** an api must be symmetrical

chapter on creating a threadsafe API, to be used by clueless and untrained programmers.

chapter on supervising the development of an API

Section on overcoming the fear of committing to a stable API

Section on Factory being better than constructor

Section on making everything final

java singleton – y nested class implementation important

http://en.wikipedia.org/wiki/Initialization_on_demand_holder_idiom is the recommended solution. My friend Rohan pointed out why this solution is sometimes necessary.

Say you can’t eagerly init the static field because it must wait for some service object.

Say you also don’t want to incur the static synchronization every time. Though un-contended lock acquisition is cheap, it’s never free.

Spring self-quiz 2

Q1: spring bean scopes? prototype/singleton/request/session…
A1: http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-factory-scopes

Q3(common): why use spring?
%A3: JMS, MVC, tx, Spring security, AOP,  hibernate,

Q4: how do u use spring tx management
A4: xml or annotation

Q: what's so special about DI, if we can easily use method/constructor injection?

Q: auto wiring and @Inject …?:

Q: singleton bean?

biggest changes af JDK 1.4

Xia Rong,

(Another blog.)

We once discussed the common interview question “what changes do you consider significant in 1.5”. Now i feel the concurrency changes are significant, perhaps second only to generics. Generics are a fundamental change affecting many parts of the core language.

On to concurrency. Reading a few threading books, a pattern seems to be there — since the late 90's, multi-threading developer had been “rolling their own” concurrency tools  because the JDK threading tools are too basic. To my surprise, a lot of these home-made components are very similar

* thread pool
* dispatcher thread
* worker threads
* task queue
* blocking queue
* semaphore….
* condition variables

After 1.5, every java trading system i know uses thread pools and concurrent collections. I don't know anyone still using wait/notify — too low-level.

Personally, i would point out the importance of Lock interface and atomic variables (esp the Compare-And-Set ie CAS functionality). CAS relies on *native* support just as wait/notify. Some people claim that the new concurrent collections derive huge performance gains partly due to atomic variables.

Why are threading improvements so important? I feel the biggest reason is the multiprocessor machines. Chip makers spent decades increasing clock speed but they reached a plateau. The way to add more power in a machine is going multi-core. Multiprocessor machines are hard to fully utilize until applications are multi-threaded.

constructors called from strategic places (super, subclasses etc

* You can call a constructor C() from an instance method in C.java. This means an existing C instance can create a “friend” C instance. Often parent-child nodes

* Call C() from its base class B?

* Call C() from its derived class D? Actually every D constructor has to call a C constructor.

* If C object has a pointer to a J object, does it make sense to make J.java can call C() constructor? Yes but we can easily add into J object a pointer back to the original C object, so the link becomes bi-directional and tight-coupling.

These unusual ways to call constructor are the basis of many OO designs.

java serialization – a few tips

See link — covers serialization in RMI.

* serialization = writing a *graph* of objects into a stream of data

* How to Make a Class Serializable
1. Implement the Serializable interface.
2. Make sure that instance-level, locally defined state is serialized properly.
3. Make sure that superclass state is serialized properly.
4. Override equals( ) and hashCode( ).

Making a class serializable rarely involves significant changes to its functionality and shouldn’t result in any changes to method implementations. This means that it’s fairly easy to retrofit serialization onto an existing object hierarchy. The hardest part is usually implementing equals( ) and hashCode( ).

on (2) above, i personally try to compose my objects with primitives, strings and simple arrays. What if there’s a map field? I guess i have to customize its serialization.

unchecked exceptions

based on [[ hardcore java ]] and http://java.sun.com/docs/books/tutorial/essential/exceptions/runtime.html

Runtime exceptions represent problems that are the result of a *programming* problem. Such problems include arithmetic exceptions, such as dividing by zero; pointer exceptions, such as trying to access an object through a null reference; and indexing exceptions, such as attempting to access an array element through an index that is too large or too small.

For example, a method can check if one of its arguments is incorrectly null. If an argument is null, the method might throw a NullPointerException.

Here’s the bottom line guideline: If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.

USP@ejb @@

— USP ] %% xp
#1 CMR/CMP to automatically load/store related database tablesssss just like hibernate/JDO. THE real value of ejb in the eyes of Union Bank of Switzerland.

[[ ejb ]] book obviously treat CMP as the heart of ejb technology

#2 biz-tier cluster, behind the web tier cluster
THE real reason for oceanlake

— USP ] theory
#1 XA. according to both [[ ejb ]] and Rod Johnson — XA ie (declarative?) dist tx. Other solutions support tx or remoting(J2SE RMI) but not both.

#2 distributed objects?
dotnet remoting?

#x MDB is the finest breed of EJB (Rod Johnson?) [[ ejb ]] author also singled out MDB as a top 5 j4EJB. Unique? not sure

#x ejb security? declarative? ejb fits like a glove if your biz logic are well-represented by security roles
[[ ejb ]] frequently mentions “transactional and security features” in one breath

====Well, almost Unique. U can call them “valuable features@ejb”

simple(@@) topics ] java

db connection obj
db connection set-up
thread-safe collections
jdbc conn pool

4 scopes


simple? Is shell scripting simple?

These tend to be interview questions, because
* most of us are expected to have used them or know them well
* => yardstick to measure and compare candidates
* familiar to most interviewers

jruby — briefly

jruby implements most of the ruby features.
Support for interacting with and defining java classes from within ruby.

Q: j4 jruby
A: using Ruby for tools and tasks where Java only incurs overhead is a
very compelling for Java developers

Q: How do I use it if i already know how to program and run a ruby program?
A: you should be able to just use your Ruby scripts with JRuby

rule@try-catch-finally execution order: af catch block

— In the absence of finally —
[ try3 = try block in unfinished method #3 on call stack ]

When catch3a (among catch3b, 3c) is triggered by a *propagated* exception pe,

* we know (since it’s propagated) that try3 must have called a try4 which threw pe, and
* we know that upon catch3a completion, the statements following catch3a run, not those following catch4

What if there’s a finally? Finally can easily derail this rule. Let’s hope that finally doesn’t use exit-type or throw.

rule@try-catch-finally execution order — cover-up

-> catch?
-> finally
-> propagate
-> catch?
-> finally
-> propagate

Upon throw, jvm tests the innermost catches. Before propagating, the innermost finally runs. See P475 [[ thinking in java ]].

Propagating = searching for a catch at the next level.

In fact, finally has the capability to cover up the uncaught and to-be-propagated exception. See P477 [[ thinking in java ]]. Similar to a county communist official coverying up a murder case scheduled for escalation. This is the only guy we know with the power to cover up a murder case.

Q: if a finally says “return”, will the method return normally, leaving the uncaught exception covered up?
A: I think so. see earlier blogs.

rule#55@try-catch-finally execution order–suspension

“exit-type statements (exit, return, break or continue…. ) in a catch{} are /suspended/ until finally runs its course.”

think of catch{} as a naughty kid. jvm will let catch{} run its course right before catch{} attempts to exit the try-catch-finally. At that point, finally{} takes over. As a good care-taker, finally{} usually [1] keeps the “exit-plan” of catch{}.

[1] see other posts on this execution order.

Q: How about a throw instead of an exit-type?
A: “If a try block throws an exception and the catch block propagates the exception (throws it again), the finally clause will still execute. If the finally clause executes a return statement, it overides (covers up) a thrown exception so the exception will not be thrown; instead the return will occur.”

A: look at the java precisely example 90

Q: what if finally /attempts to/ override that exit-type statement?
A: it will prevail.

A: look at the java precisely example 90

in java, "inaccessible" means

q: a private (static/non-static) member priv2 in class P is “inaccessible” in a client class C. Exactly what does it mean?

A(short): “class C can’t mention priv2 “

A: “a statement in class C will trigger a compile-time error if it mentions priv2 “

I think for each method call the compiler identifies
* the target-object (“target-class” for static) [1]
* the source-object of the caller method — upstream on the call stack

[1] but the target object is determined at run time by dynamic binding (– polymorphism) even if you prefix a variable (“remote-control”) in front of the method name. OK you are right about
* compile-time access checking
* run-time binding.

Here’s another case for IS-A and method-overriding rules. The long explanation is complicated. Let’s try a short explanation. What the compiler says actually holds at runtime, because the overriding version (that actually runs [2]) is as accessible as the overriden version.

[2] that binds to the method name at run time. “runtime binding”

IV: what are java "class objects"

One of the wall street interviews asked “what are class objects” and I said objects are instances of a class, so class object is imprecise phrase.

Now I think “class object” refer that special object instantiated when a class is loaded by a java classloader. 2 classloaders can instantiate 2 such objects for the same myClass.java? Yes.

One possible reason for this iview question might be mem-leak. See posts on [[ root objects ]].

java exception q&&a – nontrivial

A minimum custom exception : “class e1 extends Exception {}”
Q: Where’s the constructor? where’s is the e1(String) constructor?
a: a constructor is needed for the exception obj’s “new” operation

q3: when an exception is thrown, control passes to…?

a1: wrap the exception-prone statements in a try-catch

A: constructor is the invisible e1(){super();} There’s no e1(String)
constructor in this case, since u didn’t define it.

q: is there a constructor for each exception obj?
A: i think so, including the no-arg

a4: on the heap

q1: how do u keep your method “alive” if u know an exception can be thrown
prior to a normal return?

a2: inside the exception obj and the exception type (ie the full class
name), but often there’s nothing inside the obj

q4: is exception obj created with new()? on the heap?

A3: exception handler for this particular (type of) exception

q2: there is specific information about each exception. who needs this
info? where is this info stored?